HAL  v4.5.0-133-g64838ea8d
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
recent_file_item.cpp
Go to the documentation of this file.
2 
6 
7 #include <QEvent>
8 #include <QFileInfo>
9 #include <QHBoxLayout>
10 #include <QLabel>
11 #include <QPropertyAnimation>
12 #include <QResizeEvent>
13 #include <QStyle>
14 #include <QVBoxLayout>
15 #include <QToolButton>
16 #include <QEnterEvent>
17 
18 namespace hal
19 {
21  : QFrame(parent), mWidget(new QWidget()), mHorizontalLayout(new QHBoxLayout()), mIconLabel(new QLabel()), mVerticalLayout(new QVBoxLayout()), mNameLabel(new QLabel()),
22  mPathLabel(new QLabel()), mAnimation(new QPropertyAnimation()), mRemoveButton(new QToolButton(this)), mHover(false), mMissing(false),
23  mMethod(ActionOpenNetlistFile::Undefined)
24  {
25  mHorizontalLayout->setContentsMargins(0, 0, 0, 0);
26  mHorizontalLayout->setSpacing(0);
27 
28  mIconLabel->setObjectName("icon-label");
30 
31  mVerticalLayout->setContentsMargins(0, 0, 0, 0);
32  mVerticalLayout->setSpacing(0);
33 
34  mNameLabel->setObjectName("name-label");
35  mPathLabel->setObjectName("path-label");
36 
37 
38  mRemoveButton->setText("x");
39  connect(mRemoveButton, &QToolButton::clicked, this, &RecentFileItem::handleCloseRequested);
40 
41  setLayout(mHorizontalLayout);
42  mHorizontalLayout->addWidget(mIconLabel);
43  mHorizontalLayout->setAlignment(mIconLabel, Qt::AlignTop);
44  mHorizontalLayout->addLayout(mVerticalLayout);
45  mHorizontalLayout->addWidget(mRemoveButton);
46  mVerticalLayout->addWidget(mNameLabel);
47  mVerticalLayout->addWidget(mPathLabel);
48 
49  mFile = file;
50  QFileInfo info(file);
52  mNameLabel->setText(info.fileName());
53  //mPath = info.canonicalPath();
54  mPath = info.absolutePath();
55 
56  mPathLabel->ensurePolished();
57  int width = mPathLabel->width();
58  QFontMetrics font_metrics = mPathLabel->fontMetrics();
59  mPathLabel->setText(font_metrics.elidedText(mPath, Qt::TextElideMode::ElideLeft, width));
60 
61  if (!info.exists())
62  setMissing(true);
63 
64  repolish();
65  }
66 
68  {
69  Q_UNUSED(event)
70  if(mMissing)
71  return;
72 
73  mHover = true;
74  repolish();
75  }
76 
78  {
79  Q_UNUSED(event)
80  if(mMissing)
81  return;
82 
83  mHover = false;
84  repolish();
85  }
86 
88  {
89  Q_UNUSED(event)
90 
91  if(mMissing)
92  return;
93 
94  if(event->button() == Qt::MouseButton::LeftButton)
95  {
96  ActionOpenNetlistFile* actOpenfile = new ActionOpenNetlistFile(mMethod,mFile);
97  actOpenfile->exec();
98  event->accept();
99  }
100  }
101 
103  {
104  return mWidget->sizeHint();
105  }
106 
108  {
109  if (object == mPathLabel && event->type() == QEvent::Resize)
110  {
111  QResizeEvent* resize_event = static_cast<QResizeEvent*>(event);
112  int width = resize_event->size().width();
113 
114  mPathLabel->ensurePolished();
115  QFontMetrics font_metrics = mPathLabel->fontMetrics();
116  mPathLabel->setText(font_metrics.elidedText(mPath, Qt::TextElideMode::ElideLeft, width));
117  }
118 
119  return false;
120  }
121 
123  {
124  return mFile;
125  }
126 
128  {
129  QStyle* s = style();
130 
131  s->unpolish(this);
132  s->polish(this);
133 
134  s->unpolish(mIconLabel);
135  s->polish(mIconLabel);
136 
137  s->unpolish(mNameLabel);
138  s->polish(mNameLabel);
139 
140  s->unpolish(mPathLabel);
141  s->polish(mPathLabel);
142 
143  if (!mIconPath.isEmpty())
144  mIconLabel->setPixmap(gui_utility::getStyledSvgIcon(mIconStyle, mIconPath).pixmap(QSize(17, 17)));
145  }
146 
148  {
149  return mHover;
150  }
151 
153  {
154  return mMissing;
155  }
156 
158  {
159  return mMethod == ActionOpenNetlistFile::OpenProject;
160  }
161 
163  {
164  return mIconPath;
165  }
166 
168  {
169  return mIconStyle;
170  }
171 
173  {
174  mHover = active;
175  }
176 
178  {
179  if (mMissing == miss) return;
180  mMissing = miss;
181  if (mMissing)
182  {
183  mNameLabel->setText(mNameLabel->text().append(" [Missing]"));
184  mHover = false;
185  }
186  else
187  {
188  QString txt = mNameLabel->text();
189  if (txt.endsWith(" [Missing]"))
190  mNameLabel->setText(txt.left(txt.size()-10));
191  mHover = true;
192  }
193  }
194 
196  {
197  mIconPath = path;
198  }
199 
201  {
202  mIconStyle = style;
203  }
204 
205  void RecentFileItem::handleCloseRequested()
206  {
207  Q_EMIT removeRequested(this);
208  }
209 }
void mousePressEvent(QMouseEvent *event) override
void setIconStyle(const QString &style)
void setHoverActive(bool active)
void removeRequested(RecentFileItem *item)
RecentFileItem(const QString &file, QWidget *parent=nullptr)
virtual QSize sizeHint() const override
void setMissing(bool miss)
virtual bool eventFilter(QObject *object, QEvent *event) override
void enterEvent(QEnterEvent *event) override
void setIconPath(const QString &path)
void leaveEvent(QEvent *event) override
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
void clicked(bool checked)
void setText(const QString &text)
void addLayout(QLayout *layout, int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setSpacing(int spacing)
QString absolutePath() const const
bool exists() const const
QString fileName() const const
bool isDir() const const
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const const
virtual bool event(QEvent *e) override
void setPixmap(const QPixmap &)
void setText(const QString &)
bool setAlignment(QWidget *w, Qt::Alignment alignment)
void setContentsMargins(int left, int top, int right, int bottom)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setObjectName(const QString &name)
const QSize & size() const const
int width() const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(int n) const const
int size() const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
AlignTop
void ensurePolished() const const
QFontMetrics fontMetrics() const const
void setLayout(QLayout *layout)
void setSizePolicy(QSizePolicy)
QStyle * style() const const