KTextEditor

kateviewhelpers.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 2002 John Firebaugh <[email protected]>
3  Copyright (C) 2001 Anders Lund <[email protected]>
4  Copyright (C) 2001 Christoph Cullmann <[email protected]>
5  Copyright 2017-2018 Friedrich W. H. Kossebau <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License version 2 as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef KATE_VIEW_HELPERS_H
23 #define KATE_VIEW_HELPERS_H
24 
25 #include <KActionMenu>
26 #include <KLineEdit>
27 #include <KSelectAction>
28 
29 #include <QColor>
30 #include <QHash>
31 #include <QLayout>
32 #include <QMap>
33 #include <QPixmap>
34 #include <QPointer>
35 #include <QScrollBar>
36 #include <QStackedWidget>
37 #include <QTextLayout>
38 #include <QTimer>
39 
40 #include "katetextline.h"
41 #include <ktexteditor/cursor.h>
42 #include <ktexteditor/message.h>
43 #include <ktexteditor_export.h>
44 
45 namespace KTextEditor
46 {
47 class DocumentPrivate;
48 }
49 namespace KTextEditor
50 {
51 class ViewPrivate;
52 }
53 class KateViewInternal;
54 class KateTextLayout;
55 
56 #define MAXFOLDINGCOLORS 16
57 
58 class KateLineInfo;
59 class KateTextPreview;
60 
61 namespace KTextEditor
62 {
63 class Command;
64 class AnnotationModel;
65 class MovingRange;
66 class AbstractAnnotationItemDelegate;
67 class StyleOptionAnnotationItem;
68 }
69 
70 class QTimer;
71 class QVBoxLayout;
72 
78 class KateMessageLayout : public QLayout
79 {
80 public:
81  explicit KateMessageLayout(QWidget *parent);
82  ~KateMessageLayout() override;
83 
85  int count() const override;
86  QLayoutItem *itemAt(int index) const override;
87  void setGeometry(const QRect &rect) override;
88  QSize sizeHint() const override;
89  QLayoutItem *takeAt(int index) override;
90 
92 
93 private:
94  void addItem(QLayoutItem *item) override; // never called publically
95 
96  struct ItemWrapper {
98  : item(i)
99  , position(pos)
100  {
101  }
102 
103  QLayoutItem *item = nullptr;
105  };
106 
107  QVector<ItemWrapper *> m_items;
108 };
109 
117 class KateScrollBar : public QScrollBar
118 {
119  Q_OBJECT
120 
121 public:
122  KateScrollBar(Qt::Orientation orientation, class KateViewInternal *parent);
123  ~KateScrollBar() override;
124  QSize sizeHint() const override;
125 
126  void showEvent(QShowEvent* event) override;
127 
128  inline bool showMarks() const
129  {
130  return m_showMarks;
131  }
132  inline void setShowMarks(bool b)
133  {
134  m_showMarks = b;
135  update();
136  }
137 
138  inline bool showMiniMap() const
139  {
140  return m_showMiniMap;
141  }
142  void setShowMiniMap(bool b);
143 
144  inline bool miniMapAll() const
145  {
146  return m_miniMapAll;
147  }
148  inline void setMiniMapAll(bool b)
149  {
150  m_miniMapAll = b;
151  updateGeometry();
152  update();
153  }
154 
155  inline bool miniMapWidth() const
156  {
157  return m_miniMapWidth;
158  }
159  inline void setMiniMapWidth(int width)
160  {
161  m_miniMapWidth = width;
162  updateGeometry();
163  update();
164  }
165 
166  inline void queuePixmapUpdate()
167  {
168  m_updateTimer.start();
169  }
170 
171 Q_SIGNALS:
172  void sliderMMBMoved(int value);
173 
174 protected:
175  void mousePressEvent(QMouseEvent *e) override;
176  void mouseReleaseEvent(QMouseEvent *e) override;
177  void mouseMoveEvent(QMouseEvent *e) override;
178  void leaveEvent(QEvent *event) override;
179  bool eventFilter(QObject *object, QEvent *event) override;
180  void paintEvent(QPaintEvent *e) override;
181  void resizeEvent(QResizeEvent *) override;
182  void sliderChange(SliderChange change) override;
183 
184 protected Q_SLOTS:
185  void sliderMaybeMoved(int value);
186  void marksChanged();
187 
188 public Q_SLOTS:
189  void updatePixmap();
190 
191 private Q_SLOTS:
192  void showTextPreview();
193 
194 private:
195  void showTextPreviewDelayed();
196  void hideTextPreview();
197 
198  void redrawMarks();
199  void recomputeMarksPositions();
200 
201  void miniMapPaintEvent(QPaintEvent *e);
202  void normalPaintEvent(QPaintEvent *e);
203 
204  int minimapYToStdY(int y);
205 
206  const QColor charColor(const QVector<Kate::TextLineData::Attribute> &attributes, int &attributeIndex, const QVector<QTextLayout::FormatRange> &decorations, const QColor &defaultColor, int x, QChar ch);
207 
208  bool m_middleMouseDown;
209  bool m_leftMouseDown;
210 
211  KTextEditor::ViewPrivate *m_view;
212  KTextEditor::DocumentPrivate *m_doc;
213  class KateViewInternal *m_viewInternal;
214  QPointer<KateTextPreview> m_textPreview;
215  QTimer m_delayTextPreviewTimer;
216 
217  QHash<int, QColor> m_lines;
218 
219  bool m_showMarks;
220  bool m_showMiniMap;
221  bool m_miniMapAll;
222  bool m_needsUpdateOnShow;
223  int m_miniMapWidth;
224 
225  QPixmap m_pixmap;
226  int m_grooveHeight;
227  QRect m_stdGroveRect;
228  QRect m_mapGroveRect;
229  QTimer m_updateTimer;
230  QPoint m_toolTipPos;
231 
232  // lists of lines added/removed recently to avoid scrollbar flickering
233  QHash<int, int> m_linesAdded;
234  int m_linesModified;
235 
236  static const unsigned char characterOpacity[256];
237 };
238 
239 class KateIconBorder : public QWidget
240 {
241  Q_OBJECT
242 
243 public:
244  KateIconBorder(KateViewInternal *internalView, QWidget *parent);
245  ~KateIconBorder() override;
246  // VERY IMPORTANT ;)
247  QSize sizeHint() const override;
248 
249  void updateFont();
250  int lineNumberWidth() const;
251 
252  void setIconBorderOn(bool enable);
253  void setLineNumbersOn(bool enable);
254  void setRelLineNumbersOn(bool enable);
255  void setAnnotationBorderOn(bool enable);
256  void setDynWrapIndicators(int state);
257  int dynWrapIndicators() const
258  {
259  return m_dynWrapIndicators;
260  }
261  bool dynWrapIndicatorsOn() const
262  {
263  return m_dynWrapIndicatorsOn;
264  }
265  void setFoldingMarkersOn(bool enable);
266  void toggleIconBorder()
267  {
268  setIconBorderOn(!iconBorderOn());
269  }
270  void toggleLineNumbers()
271  {
272  setLineNumbersOn(!lineNumbersOn());
273  }
274  void toggleFoldingMarkers()
275  {
276  setFoldingMarkersOn(!foldingMarkersOn());
277  }
278  inline bool iconBorderOn() const
279  {
280  return m_iconBorderOn;
281  }
282  inline bool lineNumbersOn() const
283  {
284  return m_lineNumbersOn;
285  }
286  inline bool viRelNumbersOn() const
287  {
288  return m_relLineNumbersOn;
289  }
290  inline bool foldingMarkersOn() const
291  {
292  return m_foldingMarkersOn;
293  }
294  inline bool annotationBorderOn() const
295  {
296  return m_annotationBorderOn;
297  }
298 
299  void updateForCursorLineChange();
300 
301  enum BorderArea { None, LineNumbers, IconBorder, FoldingMarkers, AnnotationBorder, ModificationBorder };
302  BorderArea positionToArea(const QPoint &) const;
303 
304  KTextEditor::AbstractAnnotationItemDelegate *annotationItemDelegate() const;
305  void setAnnotationItemDelegate(KTextEditor::AbstractAnnotationItemDelegate *delegate);
306  inline bool uniformAnnotationItemSizes() const
307  {
308  return m_hasUniformAnnotationItemSizes;
309  }
310  inline void setAnnotationUniformItemSizes(bool enable)
311  {
312  m_hasUniformAnnotationItemSizes = enable;
313  }
314 
315 public Q_SLOTS:
316  void updateAnnotationBorderWidth();
317  void updateAnnotationLine(int line);
318  void annotationModelChanged(KTextEditor::AnnotationModel *oldmodel, KTextEditor::AnnotationModel *newmodel);
319  void displayRangeChanged();
320 
321 private:
322  void dragEnterEvent(QDragEnterEvent *) override;
323  void dragMoveEvent(QDragMoveEvent *event) override;
324  void dropEvent(QDropEvent *event) override;
325  void paintEvent(QPaintEvent *) override;
326  void paintBorder(int x, int y, int width, int height);
327 
328  void mousePressEvent(QMouseEvent *) override;
329  void mouseMoveEvent(QMouseEvent *) override;
330  void mouseReleaseEvent(QMouseEvent *) override;
331  void mouseDoubleClickEvent(QMouseEvent *) override;
332  void leaveEvent(QEvent *event) override;
333  void wheelEvent(QWheelEvent *e) override;
334 
335  void showMarkMenu(uint line, const QPoint &pos);
336 
337  void hideAnnotationTooltip();
338  void removeAnnotationHovering();
339  void showAnnotationMenu(int line, const QPoint &pos);
340  void calcAnnotationBorderWidth();
341 
342  void initStyleOption(KTextEditor::StyleOptionAnnotationItem *styleOption) const;
343  void setStyleOptionLineData(KTextEditor::StyleOptionAnnotationItem *styleOption, int y, int realLine, const KTextEditor::AnnotationModel *model, const QString &annotationGroupIdentifier) const;
344  QRect annotationLineRectInView(int line) const;
345 
346 private:
347  KTextEditor::ViewPrivate *m_view;
348  KTextEditor::DocumentPrivate *m_doc;
349  KateViewInternal *m_viewInternal;
350 
351  bool m_iconBorderOn : 1;
352  bool m_lineNumbersOn : 1;
353  bool m_relLineNumbersOn : 1;
354  bool m_updateRelLineNumbers : 1;
355  bool m_foldingMarkersOn : 1;
356  bool m_dynWrapIndicatorsOn : 1;
357  bool m_annotationBorderOn : 1;
358  bool m_updatePositionToArea : 1;
359 
360  typedef QPair<int, KateIconBorder::BorderArea> AreaPosition;
361  QVector<AreaPosition> m_positionToArea;
362 
363  const int m_separatorWidth = 2;
364  const int m_modAreaWidth = 3;
365  qreal m_maxCharWidth = 0.0;
366  int m_lineNumberAreaWidth = 0;
367  int m_iconAreaWidth = 0;
368  int m_foldingAreaWidth = 0;
369  int m_annotationAreaWidth = 0;
370  const QChar m_dynWrapIndicatorChar = QChar(0x21AA);
371  int m_dynWrapIndicators = 0;
372  int m_lastClickedLine = -1;
373 
374  KTextEditor::AbstractAnnotationItemDelegate *m_annotationItemDelegate;
375  bool m_hasUniformAnnotationItemSizes = false;
376  bool m_isDefaultAnnotationItemDelegate = true;
377 
378  QPointer<KateTextPreview> m_foldingPreview;
379  KTextEditor::MovingRange *m_foldingRange = nullptr;
380  int m_currentLine = -1;
381  QTimer m_antiFlickerTimer;
382  void highlightFoldingDelayed(int line);
383  void hideFolding();
384 
385 private Q_SLOTS:
386  void highlightFolding();
387  void handleDestroyedAnnotationItemDelegate();
388 
389 private:
390  QString m_hoveredAnnotationGroupIdentifier;
391 
392  void initializeFoldingColors();
393 };
394 
395 class KateViewEncodingAction : public KSelectAction
396 {
397  Q_OBJECT
398 
399  Q_PROPERTY(QString codecName READ currentCodecName WRITE setCurrentCodec)
400  Q_PROPERTY(int codecMib READ currentCodecMib)
401 
402 public:
403  KateViewEncodingAction(KTextEditor::DocumentPrivate *_doc, KTextEditor::ViewPrivate *_view, const QString &text, QObject *parent, bool saveAsMode = false);
404 
405  ~KateViewEncodingAction();
406 
407  int mibForName(const QString &codecName, bool *ok = nullptr) const;
408  QTextCodec *codecForMib(int mib) const;
409 
410  QTextCodec *currentCodec() const;
411  bool setCurrentCodec(QTextCodec *codec);
412 
413  QString currentCodecName() const;
414  bool setCurrentCodec(const QString &codecName);
415 
416  int currentCodecMib() const;
417  bool setCurrentCodec(int mib);
418 
419 Q_SIGNALS:
423  void triggered(QTextCodec *codec);
424 
425 private:
426  KTextEditor::DocumentPrivate *doc;
427  KTextEditor::ViewPrivate *view;
428 
429  class Private
430  {
431  public:
432  explicit Private(KateViewEncodingAction *parent)
433  : q(parent)
434  , currentSubAction(nullptr)
435  {
436  }
437 
438  void init();
439 
440  void _k_subActionTriggered(QAction *);
441 
442  KateViewEncodingAction *q;
443  QAction *currentSubAction;
444  };
445 
446  Private *const d;
447  Q_PRIVATE_SLOT(d, void _k_subActionTriggered(QAction *))
448 
449  const bool m_saveAsMode;
450 
451 private Q_SLOTS:
452  void setEncoding(const QString &e);
453  void slotAboutToShow();
454 };
455 
456 class KateViewBar;
457 
458 class KateViewBarWidget : public QWidget
459 {
460  Q_OBJECT
461  friend class KateViewBar;
462 
463 public:
464  explicit KateViewBarWidget(bool addCloseButton, QWidget *parent = nullptr);
465 
466  virtual void closed()
467  {
468  }
469 
471  KateViewBar *viewBar()
472  {
473  return m_viewBar;
474  }
475 
476 protected:
480  QWidget *centralWidget()
481  {
482  return m_centralWidget;
483  }
484 
485 Q_SIGNALS:
486  void hideMe();
487 
488  // for friend class KateViewBar
489 private:
490  void setAssociatedViewBar(KateViewBar *bar)
491  {
492  m_viewBar = bar;
493  }
494 
495 private:
496  QWidget *m_centralWidget;
497  KateViewBar *m_viewBar; // 0-pointer, if not added to a view bar
498 };
499 
500 class KateViewBar : public QWidget
501 {
502  Q_OBJECT
503 
504 public:
505  KateViewBar(bool external, QWidget *parent, KTextEditor::ViewPrivate *view);
506 
512  void addBarWidget(KateViewBarWidget *newBarWidget);
513 
520  void removeBarWidget(KateViewBarWidget *barWidget);
521 
525  bool hasBarWidget(KateViewBarWidget *barWidget) const;
526 
531  void showBarWidget(KateViewBarWidget *barWidget);
532 
543  void addPermanentBarWidget(KateViewBarWidget *barWidget);
544 
551  void removePermanentBarWidget(KateViewBarWidget *barWidget);
552 
556  bool hasPermanentWidget(KateViewBarWidget *barWidget) const;
557 
560  bool hiddenOrPermanent() const;
561 
562 public Q_SLOTS:
566  void hideCurrentBarWidget();
567 
568 protected:
569  void keyPressEvent(QKeyEvent *event) override;
570  void hideEvent(QHideEvent *event) override;
571 
572 private:
576  void setViewBarVisible(bool visible);
577 
578  bool m_external;
579 
580 private:
581  KTextEditor::ViewPrivate *m_view;
582  QStackedWidget *m_stack;
583  KateViewBarWidget *m_permanentBarWidget;
584  QVBoxLayout *m_layout;
585 };
586 
587 class KTEXTEDITOR_EXPORT KateCommandLineBar : public KateViewBarWidget
588 {
589  Q_OBJECT
590 
591 public:
592  explicit KateCommandLineBar(KTextEditor::ViewPrivate *view, QWidget *parent = nullptr);
593  ~KateCommandLineBar();
594 
595  void setText(const QString &text, bool selected = true);
596  void execute(const QString &text);
597 
598 public Q_SLOTS:
599  void showHelpPage();
600 
601 private:
602  class KateCmdLineEdit *m_lineEdit;
603 };
604 
605 class KateCmdLineEdit : public KLineEdit
606 {
607  Q_OBJECT
608 
609 public:
610  KateCmdLineEdit(KateCommandLineBar *bar, KTextEditor::ViewPrivate *view);
611  bool event(QEvent *e) override;
612 
613  void hideEvent(QHideEvent *e) override;
614 
615 Q_SIGNALS:
616  void hideRequested();
617 
618 public Q_SLOTS:
619  void slotReturnPressed(const QString &cmd);
620 
621 private Q_SLOTS:
622  void hideLineEdit();
623 
624 protected:
625  void focusInEvent(QFocusEvent *ev) override;
626  void keyPressEvent(QKeyEvent *ev) override;
627 
628 private:
639  int calculatePosition(QString string);
640  void fromHistory(bool up);
641  QString helptext(const QPoint &) const;
642 
643  KTextEditor::ViewPrivate *m_view;
644  KateCommandLineBar *m_bar;
645  bool m_msgMode;
646  QString m_oldText;
647  uint m_histpos;
648  uint m_cmdend;
649  KTextEditor::Command *m_command;
650  class KateCmdLnWhatsThis *m_help;
651 
652  QTimer *m_hideTimer;
653 };
654 
655 class KatePasteMenu : public KActionMenu
656 {
657  Q_OBJECT
658 
659 public:
660  KatePasteMenu(const QString &text, KTextEditor::ViewPrivate *view);
661 
662 private:
663  KTextEditor::ViewPrivate *m_view;
664 
665 private Q_SLOTS:
666  void slotAboutToShow();
667  void paste();
668 };
669 
670 #endif
The style option set for an annotation item, as painted by AbstractAnnotationItemDelegate.
A range that is bound to a specific Document, and maintains its position.
Definition: movingrange.h:157
A delegate for rendering line annotation information and handling events.
QWidget * widget() override
This class is required because QScrollBar&#39;s sliderMoved() signal is really supposed to be a sliderDra...
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
An model for providing line annotation information.
MessagePosition
Message position used to place the message either above or below of the KTextEditor::View.
Definition: message.h:131
An Editor command line command.
QCA_EXPORT void init()
This class represents one visible line of text; with dynamic wrapping, many KateTextLayouts can be ne...
Orientation
Class to layout KTextEditor::Messages in KateView.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:56:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.