HAL
comment_widget.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
6 #include <QDebug>
7 #include <QVBoxLayout>
8 #include <QScrollArea>
9 #include <QHBoxLayout>
10 #include <QPushButton>
11 #include <QToolButton>
12 #include <QAction>
13 #include <QIcon>
14 #include <QSpacerItem>
15 #include <QToolBar>
16 #include "gui/toolbar/toolbar.h"
18 #include "gui/gui_utils/graphics.h"
19 
20 namespace hal
21 {
23  {
24  // take properties out of the stylesheet
25  style()->unpolish(this);
26  style()->polish(this);
27 
28  //mTopLayout = new QVBoxLayout(this);
29  mTopLayout = new QGridLayout(this);
30  mTopLayout->setMargin(0);
31  mTopLayout->setSpacing(0);
32  mSearchbar = new Searchbar();
33  mSearchbar->setEmitTextWithFlags(false);
34  mSearchbar->hide();
35 
36  QSize iconSize(20, 20);
37 
38  // top bar
39  // 1. Option
40  mHeaderLayout = new QHBoxLayout();
41  mNewCommentButton = new QToolButton();
42  mNewCommentButton->setIcon(gui_utility::getStyledSvgIcon(mNewCommentIconStyle, mNewCommentIconPath));
43  mNewCommentButton->setIconSize(iconSize);
44  mSearchButton = new QToolButton();
45  mSearchButton->setIcon(gui_utility::getStyledSvgIcon(mSearchIconStyle, mSearchIconPath));
46  mSearchButton->setIconSize(iconSize);
47  mHeaderLayout->addWidget(mNewCommentButton);// alignleft without spacer
48  //mHeaderLayout->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Preferred));
49  QWidget* spacerWidget = new QWidget; // spacer items are in the overlay transparent, widgets not
51  mHeaderLayout->addWidget(spacerWidget);
52  mHeaderLayout->addWidget(mSearchButton);// alignright without spacer
53  mHeaderLayout->addWidget(mSearchbar);
54 
55  // comment part
56  mCommentsLayout = new QVBoxLayout();
57  mCommentsLayout->setSpacing(0);
58  mCommentsLayout->setMargin(0);
59  mScrollArea = new QScrollArea();
60  mScrollArea->setWidgetResizable(true);//important as it seems so that the child widget will expandd if possible
61  mCommentsContainer = new QWidget();
62  mCommentsContainer->show();
63  mCommentsContainer->setLayout(mCommentsLayout);
65  mScrollArea->setWidget(mCommentsContainer);
66  mScrollArea->show();
67 
68  mTopLayout->addLayout(mHeaderLayout,0,0);
69  mTopLayout->addWidget(mScrollArea, 1, 0);
70  //mTopLayout->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding),2,0);
71  mTopLayout->setRowStretch(1, 0);
72 
73 
74  // testing, remove later
75  setMinimumWidth(350);
76  resize(350, 300);
78 
79  // connections / logic
80  connect(mSearchButton, &QAbstractButton::clicked, this, &CommentWidget::handleSearchbarTriggered);
81  connect(mSearchbar, &Searchbar::searchIconClicked, this, &CommentWidget::handleSearchbarTriggered);
82  connect(mSearchbar, &Searchbar::triggerNewSearch, this, &CommentWidget::handleSearchbarTextEdited);
83  connect(mNewCommentButton, &QAbstractButton::clicked, this, &CommentWidget::handleNewCommentTriggered);
84  connect(gCommentManager, &CommentManager::entryAboutToBeDeleted, this, &CommentWidget::handleCommentAboutToBeDeleted);
85  connect(gCommentManager, &CommentManager::entryAdded, this, &CommentWidget::handleCommentAdded);
86  connect(gCommentManager, &CommentManager::entryModified, this, &CommentWidget::handleEntryModified);
87  }
88 
90  {
91  }
92 
94  {
95  mCurrentNode = nd;
96  mScrollArea->setWidget(createAndFillCommentContainerFactory(nd));
97  }
98 
99  QWidget *CommentWidget::createAndFillCommentContainerFactory(const Node &nd)
100  {
101  mEntryItems.clear();
102  QWidget* container = new QWidget();
103  QVBoxLayout* containerLayout = new QVBoxLayout(container);
104  containerLayout->setSpacing(0);
105  containerLayout->setMargin(0);
106 
107  auto commentList = gCommentManager->getEntriesForNode(nd);
108 
109  // create new items
110  for(const auto& entry : commentList)
111  {
112  CommentItem* item = new CommentItem(entry, container);
113  connect(item, &CommentItem::delete_requested, this, &CommentWidget::handleCommentEntryDeleteRequest);
114  //item->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
115  containerLayout->addWidget(item);
116  item->show();
117  mEntryItems.append(item);
118  }
119 
120  containerLayout->addStretch();
121  return container;
122  }
123 
124  void CommentWidget::handleSearchbarTriggered()
125  {
126  if(mSearchbar->isHidden())
127  {
128  mSearchButton->hide();
129  mSearchbar->show();
130  }
131  else
132  {
133  mSearchbar->hide();;
134  mSearchButton->show();
135  }
136  }
137 
138  void CommentWidget::handleNewCommentTriggered()
139  {
140  CommentDialog commentDialog("New Comment");
141  if(commentDialog.exec() == QDialog::Accepted)
142  {
143  gCommentManager->addComment(new CommentEntry(mCurrentNode, commentDialog.getText(), commentDialog.getHeader()));
144  }
145  commentDialog.close();
146  }
147 
148  void CommentWidget::handleCommentEntryDeleteRequest(CommentItem *item)
149  {
150  // remove item from local list and layout
151  mEntryItems.removeOne(item);
152  mScrollArea->widget()->layout()->removeWidget(item);
153 
154  // notify global manager to delete the entry
155  gCommentManager->deleteComment(item->getEntry());
156 
157  // delete commentitem
158  item->deleteLater();
159  }
160 
161  void CommentWidget::handleCommentAboutToBeDeleted(CommentEntry *entry)
162  {
163  // search for corresponding commentitem
164  CommentItem* commentItem = nullptr;
165  for(const auto& item : mEntryItems)
166  {
167  if(item->getEntry() == entry)
168  {
169  commentItem = item;
170  break;
171  }
172  }
173 
174  if(commentItem == nullptr)
175  return;
176 
177  // remove and delete item
178  mEntryItems.removeOne(commentItem);
179  mScrollArea->widget()->layout()->removeWidget(commentItem);
180  commentItem->deleteLater();
181  }
182 
183  void CommentWidget::handleEntryModified(CommentEntry *entry)
184  {
185  if(entry->getNode() != mCurrentNode)
186  return;
187 
188  for(const auto& item : mEntryItems)
189  {
190  if(item->getEntry() == entry)
191  {
192  item->updateCurrentEntry();
193  break;
194  }
195  }
196  }
197 
198  void CommentWidget::handleCommentAdded(CommentEntry *entry)
199  {
200  if(entry->getNode() != mCurrentNode)
201  return;
202 
203  // 1. just update all...
204  nodeChanged(mCurrentNode);
205 
206  // 2. manually just create new commentitem and insert it
207 // CommentItem* item = new CommentItem(entry);
208 // mEntryItems.insert(0, item);
209 // auto layout = dynamic_cast<QBoxLayout*>(mScrollArea->widget()->layout());
210 // layout->insertWidget(0, item);
211  }
212 
213  void CommentWidget::handleSearchbarTextEdited(const QString &text, SearchOptions opts)
214  {
215  for(const auto &item : mEntryItems)
216  {
217  if(item->search(text, opts))
218  {
219  // jump to last location
220  // if one wants to jump to the first location and cicle through the hits with the enter-key,
221  // the keypressedevent event must be overriden and an index list with the indices corresponding
222  // to the commentitems must be saved here together with a currentIndex var
223  mScrollArea->ensureWidgetVisible(item);
224  }
225  }
226  }
227 
228 }
void delete_requested(CommentItem *item)
void addComment(CommentEntry *entry)
void entryAboutToBeDeleted(CommentEntry *entry)
QList< CommentEntry * > getEntriesForNode(const Node &nd) const
void entryAdded(CommentEntry *entry)
void deleteComment(CommentEntry *entry)
void entryModified(CommentEntry *entry)
CommentWidget(QWidget *parent=nullptr)
void nodeChanged(const Node &nd)
The Node class object represents a module or a gate.
Definition: gui_def.h:61
A QFrame with a QLineEdit that can be used to input a substring to search for.
Definition: searchbar.h:48
void setEmitTextWithFlags(bool)
Definition: searchbar.cpp:236
void searchIconClicked()
void triggerNewSearch(const QString &text, int searchOptions)
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)
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setRowStretch(int row, int stretch)
void setSpacing(int spacing)
void setMargin(int margin)
void removeWidget(QWidget *widget)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void ensureWidgetVisible(QWidget *childWidget, int xmargin, int ymargin)
void setWidget(QWidget *widget)
QWidget * widget() const const
void setWidgetResizable(bool resizable)
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
QWidget(QWidget *parent, Qt::WindowFlags f)
void hide()
bool isHidden() const const
QLayout * layout() const const
void setMinimumWidth(int minw)
void resize(int w, int h)
void setLayout(QLayout *layout)
void show()
void setSizePolicy(QSizePolicy)
QStyle * style() const const