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
88  extra.format.setForeground(QBrush(color));
89  extra.format.setBackground(mBackgroundColor);
90  extra.cursor = mTextEdit->textCursor();
91  extraSelections.append(extra);
92  }
93  }
94  else
95  {
96  while (mTextEdit->find(string, options))
97  {
98 
99  qInfo() << "found";
100  found = true; // just return if something is found, position doesnt matter
102  extra.format.setForeground(QBrush(color));
103  extra.format.setBackground(mBackgroundColor);
104  extra.cursor = mTextEdit->textCursor();
105  extraSelections.append(extra);
106  }
107  }
108  mTextEdit->setExtraSelections(extraSelections);
109  return found;
110  }
111 
113  {
114  return mEntry;
115  }
116 
117  void CommentItem::init()
118  {
119  // extract property information
120  style()->unpolish(this);
121  style()->polish(this);
122  QSize iconSize(20, 20);
123 
124  mEntry = nullptr;
125  // TODO: replace fixed sizes (put them in stylesheet / compute them?)
126  mLayout = new QVBoxLayout(this);
127  mLayout->setSpacing(0);
128  mLayout->setMargin(0);
129 
130  // top part
131  mTopWidget = new QWidget(this);
132  mTopWidget->setFixedHeight(iconSize.height()+5); // just some spacing
133  mTopLayout = new QHBoxLayout(mTopWidget);
134  mTopLayout->setSpacing(0);
135  mTopLayout->setMargin(0);
136 
137  mHeader = new SearchableLabel(this);
138  mHeader->setStyleSheet("font-weight: bold;");
139  mCreationDate = new QLabel(this);
140  mCreationDate->setStyleSheet("font-size: 12px;");
141  mModifyButton = new QToolButton(this);
142  mModifyButton->setIcon(gui_utility::getStyledSvgIcon(mModifyCommentIconStyle, mModifyCommentIconPath));
143  mModifyButton->setIconSize(iconSize);
144  mModifyButton->setAutoRaise(true);
145  mDeleteButton = new QToolButton(this);
146  mDeleteButton->setIcon(gui_utility::getStyledSvgIcon(mDeleteCommentIconStyle, mDeleteCommentIconPath));
147  mDeleteButton->setIconSize(iconSize);
148  mDeleteButton->setAutoRaise(true);
149 
150  //perhaps with alignments (header left, date normal, small spacer, fixed buttons without stretch)?
151  mTopLayout->addWidget(mHeader);
152  mTopLayout->addWidget(mCreationDate);
153  mTopLayout->addWidget(mModifyButton);
154  mTopLayout->addWidget(mDeleteButton);
156 
157  // actual comment
158  mTextEdit = new QTextEdit(this);
159  mTextEdit->setReadOnly(true);
160 
161  mLayout->addWidget(mTopWidget);
162  //mLayout->addWidget(mText);
163  mLayout->addWidget(mTextEdit);
164  mLayout->addStretch();
165 
166  // connections / logic
167  connect(mDeleteButton, &QAbstractButton::clicked, this, &CommentItem::handleDeleteButtonTriggered);
168  connect(mModifyButton, &QAbstractButton::clicked, this, &CommentItem::handleModifyButtonTriggered);
169  }
170 
171  void CommentItem::handleDeleteButtonTriggered()
172  {
173  Q_EMIT delete_requested(this);
174  }
175 
176  void CommentItem::handleModifyButtonTriggered()
177  {
178  CommentDialog dialog("Modify Comment", mEntry);
179  if(dialog.exec() == QDialog::Accepted)
180  {
181  mEntry->setHeader(dialog.getHeader());
182  mEntry->setText(dialog.getText());
183  // relay modification through manager, let comment-widget call this function
184  // updateCurrentEntry();
186  }
187 
188  dialog.close();
189  }
190 }
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
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)
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)