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