HAL
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 
17 namespace hal
18 {
20  : QFrame(parent), mWidget(new QWidget()), mHorizontalLayout(new QHBoxLayout()), mIconLabel(new QLabel()), mVerticalLayout(new QVBoxLayout()), mNameLabel(new QLabel()),
21  mPathLabel(new QLabel()), mAnimation(new QPropertyAnimation()), mRemoveButton(new QToolButton(this)), mHover(false), mMissing(false),
22  mMethod(ActionOpenNetlistFile::Undefined)
23  {
24  mHorizontalLayout->setContentsMargins(0, 0, 0, 0);
25  mHorizontalLayout->setSpacing(0);
26 
27  mIconLabel->setObjectName("icon-label");
29 
30  mVerticalLayout->setContentsMargins(0, 0, 0, 0);
31  mVerticalLayout->setSpacing(0);
32 
33  mNameLabel->setObjectName("name-label");
34  mPathLabel->setObjectName("path-label");
35 
36 
37  mRemoveButton->setText("x");
38  connect(mRemoveButton, &QToolButton::clicked, this, &RecentFileItem::handleCloseRequested);
39 
40  setLayout(mHorizontalLayout);
41  mHorizontalLayout->addWidget(mIconLabel);
42  mHorizontalLayout->setAlignment(mIconLabel, Qt::AlignTop);
43  mHorizontalLayout->addLayout(mVerticalLayout);
44  mHorizontalLayout->addWidget(mRemoveButton);
45  mVerticalLayout->addWidget(mNameLabel);
46  mVerticalLayout->addWidget(mPathLabel);
47 
48  mFile = file;
49  QFileInfo info(file);
51  mNameLabel->setText(info.fileName());
52  //mPath = info.canonicalPath();
53  mPath = info.absolutePath();
54 
55  mPathLabel->ensurePolished();
56  int width = mPathLabel->width();
57  QFontMetrics font_metrics = mPathLabel->fontMetrics();
58  mPathLabel->setText(font_metrics.elidedText(mPath, Qt::TextElideMode::ElideLeft, width));
59 
60  if (!info.exists())
61  setMissing(true);
62 
63  repolish();
64  }
65 
67  {
68  Q_UNUSED(event)
69  if(mMissing)
70  return;
71 
72  mHover = true;
73  repolish();
74  }
75 
77  {
78  Q_UNUSED(event)
79  if(mMissing)
80  return;
81 
82  mHover = false;
83  repolish();
84  }
85 
87  {
88  Q_UNUSED(event)
89 
90  if(mMissing)
91  return;
92 
93  if(event->button() == Qt::MouseButton::LeftButton)
94  {
95  ActionOpenNetlistFile* actOpenfile = new ActionOpenNetlistFile(mMethod,mFile);
96  actOpenfile->exec();
97  event->accept();
98  }
99  }
100 
102  {
103  return mWidget->sizeHint();
104  }
105 
107  {
108  if (object == mPathLabel && event->type() == QEvent::Resize)
109  {
110  QResizeEvent* resize_event = static_cast<QResizeEvent*>(event);
111  int width = resize_event->size().width();
112 
113  mPathLabel->ensurePolished();
114  QFontMetrics font_metrics = mPathLabel->fontMetrics();
115  mPathLabel->setText(font_metrics.elidedText(mPath, Qt::TextElideMode::ElideLeft, width));
116  }
117 
118  return false;
119  }
120 
122  {
123  return mFile;
124  }
125 
127  {
128  QStyle* s = style();
129 
130  s->unpolish(this);
131  s->polish(this);
132 
133  s->unpolish(mIconLabel);
134  s->polish(mIconLabel);
135 
136  s->unpolish(mNameLabel);
137  s->polish(mNameLabel);
138 
139  s->unpolish(mPathLabel);
140  s->polish(mPathLabel);
141 
142  if (!mIconPath.isEmpty())
143  mIconLabel->setPixmap(gui_utility::getStyledSvgIcon(mIconStyle, mIconPath).pixmap(QSize(17, 17)));
144  }
145 
147  {
148  return mHover;
149  }
150 
152  {
153  return mMissing;
154  }
155 
157  {
158  return mMethod == ActionOpenNetlistFile::OpenProject;
159  }
160 
162  {
163  return mIconPath;
164  }
165 
167  {
168  return mIconStyle;
169  }
170 
172  {
173  mHover = active;
174  }
175 
177  {
178  if (mMissing == miss) return;
179  mMissing = miss;
180  if (mMissing)
181  {
182  mNameLabel->setText(mNameLabel->text().append(" [Missing]"));
183  mHover = false;
184  }
185  else
186  {
187  QString txt = mNameLabel->text();
188  if (txt.endsWith(" [Missing]"))
189  mNameLabel->setText(txt.left(txt.size()-10));
190  mHover = true;
191  }
192  }
193 
195  {
196  mIconPath = path;
197  }
198 
200  {
201  mIconStyle = style;
202  }
203 
204  void RecentFileItem::handleCloseRequested()
205  {
206  Q_EMIT removeRequested(this);
207  }
208 }
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
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 enterEvent(QEvent *event) override
void setMissing(bool miss)
virtual bool eventFilter(QObject *object, QEvent *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
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