KTextEditor

kateviewhelpers.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
4 SPDX-FileCopyrightText: 2001 Anders Lund <anders@alweb.dk>
5 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
6 SPDX-FileCopyrightText: 2017-2018 Friedrich W. H. Kossebau <kossebau@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-only
9*/
10
11#ifndef KATE_VIEW_HELPERS_H
12#define KATE_VIEW_HELPERS_H
13
14#include <KActionMenu>
15#include <KLineEdit>
16#include <KSelectAction>
17
18#include <QColor>
19#include <QHash>
20#include <QLabel>
21#include <QLayout>
22#include <QPixmap>
23#include <QPointer>
24#include <QScrollBar>
25#include <QTimer>
26
27#include "katetextline.h"
28#include <ktexteditor/cursor.h>
29#include <ktexteditor/message.h>
30
31namespace KTextEditor
32{
33class ViewPrivate;
34class DocumentPrivate;
35class Command;
36class AnnotationModel;
37class MovingRange;
38class AbstractAnnotationItemDelegate;
39class StyleOptionAnnotationItem;
40}
41
42class KateViewInternal;
43class KateTextLayout;
44
45#define MAXFOLDINGCOLORS 16
46
47class KateLineInfo;
48class KateTextPreview;
49
50namespace Kate
51{
52class TextRange;
53}
54
55class QTimer;
56class QVBoxLayout;
57class QStackedWidget;
58class QLabel;
59
60/**
61 * Class to layout KTextEditor::Message%s in KateView. Only the floating
62 * positions TopInView, CenterInView, and BottomInView are supported.
63 * AboveView and BelowView are not supported and ASSERT.
64 */
66{
67public:
69 ~KateMessageLayout() override;
70
72 int count() const override;
73 QLayoutItem *itemAt(int index) const override;
74 void setGeometry(const QRect &rect) override;
75 QSize sizeHint() const override;
76 QLayoutItem *takeAt(int index) override;
77
79
80private:
81 void addItem(QLayoutItem *item) override; // never called publically
82
83 struct ItemWrapper {
84 ItemWrapper() = default;
86 : item(i)
87 , position(pos)
88 {
89 }
90
91 QLayoutItem *item = nullptr;
93 };
94
95 QList<ItemWrapper> m_items;
96};
97
98/**
99 * This class is required because QScrollBar's sliderMoved() signal is
100 * really supposed to be a sliderDragged() signal... so this way we can capture
101 * MMB slider moves as well
102 *
103 * Also, it adds some useful indicators on the scrollbar.
104 */
106{
108
109public:
110 KateScrollBar(Qt::Orientation orientation, class KateViewInternal *parent);
111 ~KateScrollBar() override;
112 QSize sizeHint() const override;
113
114 void showEvent(QShowEvent *event) override;
115
116 inline bool showMarks() const
117 {
118 return m_showMarks;
119 }
120 inline void setShowMarks(bool b)
121 {
122 m_showMarks = b;
123 update();
124 }
125
126 inline bool showMiniMap() const
127 {
128 return m_showMiniMap;
129 }
130 void setShowMiniMap(bool b);
131
132 inline bool miniMapAll() const
133 {
134 return m_miniMapAll;
135 }
136 inline void setMiniMapAll(bool b)
137 {
138 m_miniMapAll = b;
140 update();
141 }
142
143 inline bool miniMapWidth() const
144 {
145 return m_miniMapWidth;
146 }
147 inline void setMiniMapWidth(int width)
148 {
149 m_miniMapWidth = width;
151 update();
152 }
153
154 inline void queuePixmapUpdate()
155 {
156 m_updateTimer.start();
157 }
158
160 void sliderMMBMoved(int value);
161
162protected:
163 void mousePressEvent(QMouseEvent *e) override;
164 void mouseReleaseEvent(QMouseEvent *e) override;
165 void mouseMoveEvent(QMouseEvent *e) override;
166 void leaveEvent(QEvent *event) override;
167 bool eventFilter(QObject *object, QEvent *event) override;
168 void paintEvent(QPaintEvent *e) override;
169 void resizeEvent(QResizeEvent *) override;
170 void sliderChange(SliderChange change) override;
171
172protected Q_SLOTS:
173 void sliderMaybeMoved(int value);
174 void marksChanged();
175
176public Q_SLOTS:
177 void updatePixmap();
178
179private Q_SLOTS:
180 void showTextPreview();
181
182private:
183 void showTextPreviewDelayed();
184 void hideTextPreview();
185
186 void redrawMarks();
187 void recomputeMarksPositions();
188
189 void miniMapPaintEvent(QPaintEvent *e);
190 void normalPaintEvent(QPaintEvent *e);
191
192 int minimapYToStdY(int y);
193
194 struct ColumnRangeWithColor {
195 int penIndex = -1;
196 int startColumn;
197 int endColumn;
198 };
199 void getCharColorRanges(const QList<Kate::TextLine::Attribute> &attributes,
200 const QList<Kate::TextRange *> &decorations,
201 const QString &text,
203 QVarLengthArray<std::pair<QRgb, QPen>, 20> &penCache);
204
205 bool m_middleMouseDown;
206 bool m_leftMouseDown;
207
208 KTextEditor::ViewPrivate *m_view;
210 class KateViewInternal *m_viewInternal;
211 QPointer<KateTextPreview> m_textPreview;
212 QTimer m_delayTextPreviewTimer;
213
214 QHash<int, QColor> m_lines;
215
216 bool m_showMarks;
217 bool m_showMiniMap;
218 bool m_miniMapAll;
219 bool m_needsUpdateOnShow;
220 int m_miniMapWidth;
221
222 QPixmap m_pixmap;
223 int m_grooveHeight;
224 QRect m_stdGroveRect;
225 QRect m_mapGroveRect;
226 QRect m_sliderRect;
227 QTimer m_updateTimer;
228 QPoint m_toolTipPos;
229 QLabel m_tooltipLineNoInfo;
230
231 // lists of lines added/removed recently to avoid scrollbar flickering
232 QHash<int, int> m_linesAdded;
233
234 static const unsigned char characterOpacity[256];
235};
236
237class KateIconBorder : public QWidget
238{
240
241public:
242 KateIconBorder(KateViewInternal *internalView, QWidget *parent);
243 ~KateIconBorder() override;
244 // VERY IMPORTANT ;)
245 QSize sizeHint() const override;
246
247 void updateFont();
248 int lineNumberWidth() const;
249
250 void setIconBorderOn(bool enable);
251 void setLineNumbersOn(bool enable);
252 void setRelLineNumbersOn(bool enable);
253 void setAnnotationBorderOn(bool enable);
254 void setDynWrapIndicators(int state);
255 int dynWrapIndicators() const
256 {
257 return m_dynWrapIndicators;
258 }
259 bool dynWrapIndicatorsOn() const
260 {
261 return m_dynWrapIndicatorsOn;
262 }
263 void setFoldingMarkersOn(bool enable);
264 void toggleIconBorder()
265 {
266 setIconBorderOn(!iconBorderOn());
267 }
268 void toggleLineNumbers()
269 {
270 setLineNumbersOn(!lineNumbersOn());
271 }
272 void toggleFoldingMarkers()
273 {
274 setFoldingMarkersOn(!foldingMarkersOn());
275 }
276 inline bool iconBorderOn() const
277 {
278 return m_iconBorderOn;
279 }
280 inline bool lineNumbersOn() const
281 {
282 return m_lineNumbersOn;
283 }
284 inline bool viRelNumbersOn() const
285 {
286 return m_relLineNumbersOn;
287 }
288 inline bool foldingMarkersOn() const
289 {
290 return m_foldingMarkersOn;
291 }
292 inline bool annotationBorderOn() const
293 {
294 return m_annotationBorderOn;
295 }
296
297 void updateForCursorLineChange();
298
299 enum BorderArea {
300 None,
301 LineNumbers,
302 IconBorder,
303 FoldingMarkers,
304 AnnotationBorder,
305 ModificationBorder
306 };
307 BorderArea positionToArea(const QPoint &) const;
308
309 KTextEditor::AbstractAnnotationItemDelegate *annotationItemDelegate() const;
310 void setAnnotationItemDelegate(KTextEditor::AbstractAnnotationItemDelegate *delegate);
311 inline bool uniformAnnotationItemSizes() const
312 {
313 return m_hasUniformAnnotationItemSizes;
314 }
315 inline void setAnnotationUniformItemSizes(bool enable)
316 {
317 m_hasUniformAnnotationItemSizes = enable;
318 }
319
320public Q_SLOTS:
321 void updateAnnotationBorderWidth();
322 void updateAnnotationLine(int line);
323 void annotationModelChanged(KTextEditor::AnnotationModel *oldmodel, KTextEditor::AnnotationModel *newmodel);
324 void displayRangeChanged();
325
326private:
327 void dragEnterEvent(QDragEnterEvent *) override;
328 void dragMoveEvent(QDragMoveEvent *event) override;
329 void dropEvent(QDropEvent *event) override;
330 void paintEvent(QPaintEvent *) override;
331 void paintBorder(int x, int y, int width, int height);
332
333 void mousePressEvent(QMouseEvent *) override;
334 void mouseMoveEvent(QMouseEvent *) override;
335 void mouseReleaseEvent(QMouseEvent *) override;
336 void mouseDoubleClickEvent(QMouseEvent *) override;
337 void contextMenuEvent(QContextMenuEvent *e) override;
338 void leaveEvent(QEvent *event) override;
339 void wheelEvent(QWheelEvent *e) override;
340
341 void enterEvent(QEnterEvent *e) override;
342
343 void showMarkMenu(uint line, const QPoint &pos);
344
345 void hideAnnotationTooltip();
346 void removeAnnotationHovering();
347 void showAnnotationMenu(int line, const QPoint &pos);
348 void calcAnnotationBorderWidth();
349
350 void initStyleOption(KTextEditor::StyleOptionAnnotationItem *styleOption) const;
351 void setStyleOptionLineData(KTextEditor::StyleOptionAnnotationItem *styleOption,
352 int y,
353 int realLine,
354 const KTextEditor::AnnotationModel *model,
355 const QString &annotationGroupIdentifier) const;
356 QRect annotationLineRectInView(int line) const;
357
358private:
359 KTextEditor::ViewPrivate *m_view;
361 KateViewInternal *m_viewInternal;
362
363 bool m_iconBorderOn : 1;
364 bool m_lineNumbersOn : 1;
365 bool m_relLineNumbersOn : 1;
366 bool m_updateRelLineNumbers : 1;
367 bool m_foldingMarkersOn : 1;
368 bool m_dynWrapIndicatorsOn : 1;
369 bool m_annotationBorderOn : 1;
370 bool m_updatePositionToArea : 1;
371 bool m_mouseOver = false;
372
373 typedef QPair<int, KateIconBorder::BorderArea> AreaPosition;
374 std::vector<AreaPosition> m_positionToArea;
375
376 const int m_separatorWidth = 2;
377 const int m_modAreaWidth = 3;
378 qreal m_maxCharWidth = 0.0;
379 int m_lineNumberAreaWidth = 0;
380 int m_iconAreaWidth = 0;
381 int m_foldingAreaWidth = 0;
382 int m_annotationAreaWidth = 0;
383 const QChar m_dynWrapIndicatorChar = QChar(0x21AA);
384 int m_dynWrapIndicators = 0;
385 int m_lastClickedLine = -1;
386
387 KTextEditor::AbstractAnnotationItemDelegate *m_annotationItemDelegate;
388 bool m_hasUniformAnnotationItemSizes = false;
389 bool m_isDefaultAnnotationItemDelegate = true;
390
391 QPointer<KateTextPreview> m_foldingPreview;
392 KTextEditor::MovingRange *m_foldingRange = nullptr;
393 int m_currentLine = -1;
394 QTimer m_antiFlickerTimer;
395 void highlightFoldingDelayed(int line);
396 void hideFolding();
397
398private Q_SLOTS:
399 void highlightFolding();
400 void handleDestroyedAnnotationItemDelegate();
401 void delayedUpdateOfSizeWithRepaint();
402
403private:
404 QString m_hoveredAnnotationGroupIdentifier;
405};
406
407class KateViewEncodingAction : public KSelectAction
408{
409public:
410 KateViewEncodingAction(KTextEditor::DocumentPrivate *_doc, KTextEditor::ViewPrivate *_view, const QString &text, QObject *parent, bool saveAsMode = false);
411
412 bool setCurrentCodec(const QString &codec);
413
414private:
415 void init();
416 void subActionTriggered(QAction *);
417
419 KTextEditor::ViewPrivate *view;
420 QAction *currentSubAction;
421 const bool m_saveAsMode;
422
423private:
424 void setEncoding(const QString &e);
425 void slotAboutToShow();
426};
427
428class KateViewBar;
429
430class KateViewBarWidget : public QWidget
431{
433 friend class KateViewBar;
434
435public:
436 explicit KateViewBarWidget(bool addCloseButton, QWidget *parent = nullptr);
437
438 virtual void closed()
439 {
440 }
441
442 /// returns the currently associated KateViewBar and 0, if it is not associated
443 KateViewBar *viewBar()
444 {
445 return m_viewBar;
446 }
447
448protected:
449 /**
450 * @return widget that should be used to add controls to bar widget
451 */
452 QWidget *centralWidget()
453 {
454 return m_centralWidget;
455 }
456
457 /**
458 * @return close button, if there
459 */
460 QToolButton *closeButton()
461 {
462 return m_closeButton;
463 }
464
466 void hideMe();
467
468 // for friend class KateViewBar
469private:
470 void setAssociatedViewBar(KateViewBar *bar)
471 {
472 m_viewBar = bar;
473 }
474
475private:
476 QWidget *m_centralWidget = nullptr;
477 KateViewBar *m_viewBar = nullptr; // 0-pointer, if not added to a view bar
478 QToolButton *m_closeButton = nullptr;
479};
480
481class KateViewBar : public QWidget
482{
484
485public:
486 KateViewBar(bool external, QWidget *parent, KTextEditor::ViewPrivate *view);
487
488 /**
489 * Adds a widget to this viewbar.
490 * Widget is initially invisible, you should call showBarWidget, to show it.
491 * Several widgets can be added to the bar, but only one can be visible
492 */
493 void addBarWidget(KateViewBarWidget *newBarWidget);
494
495 /**
496 * Removes a widget from this viewbar.
497 * Removing a widget makes sense if it takes a lot of space vertically,
498 * because we use a QStackedWidget to maintain the same height for all
499 * widgets in the viewbar.
500 */
501 void removeBarWidget(KateViewBarWidget *barWidget);
502
503 /**
504 * @return if viewbar has widget @p barWidget
505 */
506 bool hasBarWidget(KateViewBarWidget *barWidget) const;
507
508 /**
509 * Shows barWidget that was previously added with addBarWidget.
510 * @see hideCurrentBarWidget
511 */
512 void showBarWidget(KateViewBarWidget *barWidget);
513
514 /**
515 * Adds widget that will be always shown in the viewbar.
516 * After adding permanent widget viewbar is immediately shown.
517 * ViewBar with permanent widget won't hide itself
518 * until permanent widget is removed.
519 * OTOH showing/hiding regular barWidgets will work as usual
520 * (they will be shown above permanent widget)
521 *
522 * If permanent widget already exists, asserts!
523 */
524 void addPermanentBarWidget(KateViewBarWidget *barWidget);
525
526 /**
527 * Removes permanent bar widget from viewbar.
528 * If no other viewbar widgets are shown, viewbar gets hidden.
529 *
530 * barWidget is not deleted, caller must do it if it wishes
531 */
532 void removePermanentBarWidget(KateViewBarWidget *barWidget);
533
534 /**
535 * @return true if the a KateViewBar is visible*/
536 bool barWidgetVisible() const;
537
538public Q_SLOTS:
539 /**
540 * Hides currently shown bar widget
541 */
542 void hideCurrentBarWidget();
543
544protected:
545 void keyPressEvent(QKeyEvent *event) override;
546 void hideEvent(QHideEvent *event) override;
547
548private:
549 /**
550 * Shows or hides whole viewbar
551 */
552 void setViewBarVisible(bool visible);
553
554 bool m_external;
555
556private:
557 KTextEditor::ViewPrivate *m_view;
558 QStackedWidget *m_stack;
559 KateViewBarWidget *m_permanentBarWidget;
560 QVBoxLayout *m_layout;
561};
562
563class KateCommandLineBar : public KateViewBarWidget
564{
565public:
566 explicit KateCommandLineBar(KTextEditor::ViewPrivate *view, QWidget *parent = nullptr);
567 ~KateCommandLineBar() override;
568
569 void setText(const QString &text, bool selected = true);
570 void execute(const QString &text);
571
572public:
573 static void showHelpPage();
574
575private:
576 class KateCmdLineEdit *m_lineEdit;
577};
578
579class KateCmdLineEdit : public KLineEdit
580{
582
583public:
584 KateCmdLineEdit(KateCommandLineBar *bar, KTextEditor::ViewPrivate *view);
585 bool event(QEvent *e) override;
586
587 void hideEvent(QHideEvent *e) override;
588
590 void hideRequested();
591
592public Q_SLOTS:
593 void slotReturnPressed(const QString &cmd);
594
595private Q_SLOTS:
596 void hideLineEdit();
597
598protected:
599 void focusInEvent(QFocusEvent *ev) override;
600 void keyPressEvent(QKeyEvent *ev) override;
601
602private:
603 /**
604 * Parse an expression denoting a position in the document.
605 * Return the position as an integer.
606 * Examples of expressions are "10" (the 10th line),
607 * "$" (the last line), "." (the current line),
608 * "'a" (the mark 'a), "/foo/" (a forwards search for "foo"),
609 * and "?bar?" (a backwards search for "bar").
610 * @param string the expression to parse
611 * @return the position, an integer
612 */
613 void fromHistory(bool up);
614 QString helptext(const QPoint &) const;
615
616 KTextEditor::ViewPrivate *m_view;
617 KateCommandLineBar *m_bar;
618 bool m_msgMode;
619 QString m_oldText;
620 uint m_histpos; ///< position in the history
621 uint m_cmdend; ///< the point where a command ends in the text, if we have a valid one.
622 KTextEditor::Command *m_command; ///< For completing flags/args and interactiveness
623 class KateCmdLnWhatsThis *m_help;
624
625 QTimer *m_hideTimer;
626};
627
628class KateViewSchemaAction : public KActionMenu
629{
630public:
631 KateViewSchemaAction(const QString &text, QObject *parent)
633 {
634 init();
636 }
637
638 void updateMenu(KTextEditor::ViewPrivate *view);
639
640private:
641 void init();
642
644 QStringList names;
645 QActionGroup *m_group;
646 int last;
647
648public:
649 void slotAboutToShow();
650
651private:
652 void setSchema();
653};
654
655#endif
void setPopupMode(QToolButton::ToolButtonPopupMode popupMode)
A delegate for rendering line annotation information and handling events.
An model for providing line annotation information.
An Editor command line command.
Backend of KTextEditor::Document related public KTextEditor interfaces.
MessagePosition
Message position used to place the message either above or below of the KTextEditor::View.
Definition message.h:117
@ AboveView
show message above view.
Definition message.h:119
A range that is bound to a specific Document, and maintains its position.
The style option set for an annotation item, as painted by AbstractAnnotationItemDelegate.
Class to layout KTextEditor::Messages in KateView.
This class is required because QScrollBar's sliderMoved() signal is really supposed to be a sliderDra...
This class represents one visible line of text; with dynamic wrapping, many KateTextLayouts can be ne...
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
virtual QWidget * widget() const const
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
virtual bool event(QEvent *event) override
Orientation
void start()
virtual bool event(QEvent *event) override
void update()
void updateGeometry()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.