HAL
comment_item.cpp
Go to the documentation of this file.
5 #include "gui/gui_globals.h"
6 
7 #include <QToolBar>
8 #include <QVBoxLayout>
9 #include <QHBoxLayout>
10 #include <QLabel>
11 #include <QAction>
12 #include <QToolButton>
13 #include <QDebug>
14 #include "gui/gui_utils/graphics.h"
15 #if QT_VERSION >= QT_VERSION_CHECK(5,13,0)
16  #include <QRegularExpression>
17 #else
18  #include <QRegExp>
19 #endif
20 
21 namespace hal
22 {
24  {
25  init();
26  }
27 
29  {
30  init();
31  mEntry = entry;
33  }
34 
36  {
37  }
38 
40  {
41  mEntry = entry;
43  }
44 
46  {
47  if(!mEntry) return;
48  mHeader->setText(mEntry->getHeader());
49  mCreationDate->setText(" " + mEntry->getLastModifiedTime().toLocalTime().toString(mEntry->getDateFormatString()));
50  mTextEdit->setHtml(mEntry->getText());
51  //setFixedHeight(mTopWidget->height()+mText->height());
52  }
53 
54  bool CommentItem::search(const QString &string, SearchOptions searchOpts)
55  {
56  mHeader->handleSearchChanged(string,searchOpts.toInt());
57  if (string.isEmpty())
58  {
60  // reset find marker
61  mTextEdit->moveCursor(QTextCursor::Start);
62  return false;
63  }
64  bool found = mHeader->hasMatch();
65  QList<QTextEdit::ExtraSelection> extraSelections;
66 
67  mTextEdit->moveCursor(QTextCursor::Start);
68  QColor color = QColor(12, 15, 19);
69  QColor mBackgroundColor = QColor(255, 255, 0);
70 
72  options.setFlag(QTextDocument::FindCaseSensitively, searchOpts.isCaseSensitive());
73  options.setFlag(QTextDocument::FindWholeWords, searchOpts.isExactMatch());
74  qInfo() << "search in commets";
75  if(searchOpts.isRegularExpression())
76  {
77  qInfo() << "is regex";
78 #if QT_VERSION >= QT_VERSION_CHECK(5,13,0)
80 #else
81  QRegExp regExp(string, searchOpts.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive);
82 #endif
83 
84  while (mTextEdit->find(regExp, options))
85  {
86  found = true; // just return if something is found, position doesnt matter
87  QTextCursor cur = mTextEdit->textCursor();
88  if (cur.anchor() == cur.position()) // zero-length match
89  {
91  break; // end of document reached
92  mTextEdit->setTextCursor(cur);
93  continue;
94  }
95 
97  extra.format.setForeground(QBrush(color));
98  extra.format.setBackground(mBackgroundColor);
99  extra.cursor = cur;
100  extraSelections.append(extra);
101  }
102  }
103  else
104  {
105  while (mTextEdit->find(string, options))
106  {
107 
108  qInfo() << "found";
109  found = true; // just return if something is found, position doesnt matter
111  extra.format.setForeground(QBrush(color));
112  extra.format.setBackground(mBackgroundColor);
113  extra.cursor = mTextEdit->textCursor();
114  extraSelections.append(extra);
115  }
116  }
117  mTextEdit->setExtraSelections(extraSelections);
118  return found;
119  }
120 
122  {
123  return mEntry;
124  }
125 
126  void CommentItem::init()
127  {
128  // extract property information
129  style()->unpolish(this);
130  style()->polish(this);
131  QSize iconSize(20, 20);
132 
133  mEntry = nullptr;
134  // TODO: replace fixed sizes (put them in stylesheet / compute them?)
135  mLayout = new QVBoxLayout(this);
136  mLayout->setSpacing(0);
137  mLayout->setMargin(0);
138 
139  // top part
140  mTopWidget = new QWidget(this);
141  mTopWidget->setFixedHeight(iconSize.height()+5); // just some spacing
142  mTopLayout = new QHBoxLayout(mTopWidget);
143  mTopLayout->setSpacing(0);
144  mTopLayout->setMargin(0);
145 
146  mHeader = new SearchableLabel(this);
147  mHeader->setStyleSheet("font-weight: bold;");
148  mCreationDate = new QLabel(this);
149  mCreationDate->setStyleSheet("font-size: 12px;");
150  mModifyButton = new QToolButton(this);
151  mModifyButton->setIcon(gui_utility::getStyledSvgIcon(mModifyCommentIconStyle, mModifyCommentIconPath));
152  mModifyButton->setIconSize(iconSize);
153  mModifyButton->setAutoRaise(true);
154  mDeleteButton = new QToolButton(this);
155  mDeleteButton->setIcon(gui_utility::getStyledSvgIcon(mDeleteCommentIconStyle, mDeleteCommentIconPath));
156  mDeleteButton->setIconSize(iconSize);
157  mDeleteButton->setAutoRaise(true);
158 
159  //perhaps with alignments (header left, date normal, small spacer, fixed buttons without stretch)?
160  mTopLayout->addWidget(mHeader);
161  mTopLayout->addWidget(mCreationDate);
162  mTopLayout->addWidget(mModifyButton);
163  mTopLayout->addWidget(mDeleteButton);
165 
166  // actual comment
167  mTextEdit = new QTextEdit(this);
168  mTextEdit->setReadOnly(true);
169 
170  mLayout->addWidget(mTopWidget);
171  //mLayout->addWidget(mText);
172  mLayout->addWidget(mTextEdit);
173  mLayout->addStretch();
174 
175  // connections / logic
176  connect(mDeleteButton, &QAbstractButton::clicked, this, &CommentItem::handleDeleteButtonTriggered);
177  connect(mModifyButton, &QAbstractButton::clicked, this, &CommentItem::handleModifyButtonTriggered);
178  }
179 
180  void CommentItem::handleDeleteButtonTriggered()
181  {
182  Q_EMIT delete_requested(this);
183  }
184 
185  void CommentItem::handleModifyButtonTriggered()
186  {
187  CommentDialog dialog("Modify Comment", mEntry);
188  if(dialog.exec() == QDialog::Accepted)
189  {
190  mEntry->setHeader(dialog.getHeader());
191  mEntry->setText(dialog.getText());
192  // relay modification through manager, let comment-widget call this function
193  // updateCurrentEntry();
195  }
196 
197  dialog.close();
198  }
199 }
The CommentEntry class encapsulated information related to a comment.
Definition: comment_entry.h:43
QDateTime getLastModifiedTime() const
QString getHeader() const
QString getText() const
void setText(QString newText)
QString getDateFormatString() const
void setHeader(QString newHeader)
void setComment(CommentEntry *entry)
void delete_requested(CommentItem *item)
bool search(const QString &string, SearchOptions searchOpts)
CommentItem(QWidget *parent=nullptr)
CommentEntry * getEntry()
void updateCurrentEntry()
void relayEntryModified(CommentEntry *entry)
bool isExactMatch() const
bool isCaseSensitive() const
bool isRegularExpression() const
void handleSearchChanged(const QString &string, int option)
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
CommentManager * gCommentManager
Definition: plugin_gui.cpp:87
void clicked(bool checked)
void setIcon(const QIcon &icon)
void setIconSize(const QSize &size)
void addStretch(int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setSpacing(int spacing)
QDateTime toLocalTime() const const
QString toString(Qt::DateFormat format) const const
void setText(const QString &)
void setMargin(int margin)
void append(const T &value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
CaseSensitive
int anchor() const const
bool movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n)
int position() const const
typedef FindFlags
bool find(const QString &exp, QTextDocument::FindFlags options)
void setHtml(const QString &text)
void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode)
void setReadOnly(bool ro)
void setExtraSelections(const QList< QTextEdit::ExtraSelection > &selections)
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
void setAutoRaise(bool enable)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setFixedHeight(int h)
void setSizePolicy(QSizePolicy)
QStyle * style() const const
void setStyleSheet(const QString &styleSheet)