HAL
project_dir_dialog.cpp
Go to the documentation of this file.
3 #include <QDialogButtonBox>
4 #include <QEvent>
5 #include <QMessageBox>
6 #include <QGridLayout>
7 #include <QLabel>
8 #include <QIcon>
9 #include <QStyle>
10 #include <QHBoxLayout>
11 
12 namespace hal {
13  // credits to icon designer https://www.flaticon.com/authors/ian-june
14  QPixmap* ProjectDirDialogStatus::sCheckMark = nullptr;
15 
16  // credits to icon designer https://www.flaticon.com/authors/amonrat-rungreangfangsai
17  QPixmap* ProjectDirDialogStatus::sAttention = nullptr;
18 
20  : QFrame(parent)
21  {
23  setLineWidth(2);
24  QHBoxLayout* lay = new QHBoxLayout(this);
25  lay->addWidget(mIcon = new QLabel(this));
26  lay->addWidget(mText = new QLabel(this));
27  lay->addStretch(0);
28  }
29 
30  QPixmap ProjectDirDialogStatus::getPixmap(bool ok)
31  {
32  if (ok)
33  {
34  if (!sCheckMark) sCheckMark = new QPixmap(":/icons/check-mark", "PNG");
35  return *sCheckMark;
36  }
37  if (!sAttention) sAttention = new QPixmap(":/icons/attention", "PNG");
38  return *sAttention;
39  }
40 
42  {
43  mIcon->setPixmap(getPixmap(stat == FileManager::ProjectDirectory));
44  mText->setText(path + "\n" + FileManager::directoryStatusText(stat));
45  }
46 
47  ProjectDirDialog::ProjectDirDialog(const QString &title, const QString &defaultDir, QWidget* parent)
48  : QFileDialog(parent), mChooseButton(nullptr), mStatus(nullptr),
49  mSelectedDirectoryStatus(FileManager::OtherDirectory)
50  {
51  setWindowTitle(title);
56  setDirectory(defaultDir);
57  connect(this, &QFileDialog::currentChanged, this, &ProjectDirDialog::handleCurrentChanged);
58  connect(this, &QFileDialog::directoryEntered, this, &ProjectDirDialog::handleCurrentChanged);
59 
60  QGridLayout* glay = findChild<QGridLayout*>("gridLayout");
61  if (glay)
62  {
63  mStatus = new ProjectDirDialogStatus(this);
64  glay->addWidget(mStatus,4,0,1,3);
65  }
66 
67  QDialogButtonBox* bbox = findChild<QDialogButtonBox*>("buttonBox");
68  if (bbox)
69  {
70 // dumpObjectTree();
71  for (QAbstractButton* but : bbox->buttons())
72  {
73  if (but->text() == "&Choose" || but->text() == "&Open")
74  {
75  mChooseButton = but;
76  break;
77  }
78  }
79  }
80  if (mChooseButton)
81  mChooseButton->installEventFilter(this);
82  handleCurrentChanged(defaultDir);
83  }
84 
85  bool ProjectDirDialog::isSelectable() const
86  {
87  return mSelectedDirectoryStatus == FileManager::ProjectDirectory;
88  }
89 
91  {
92  if (obj == mChooseButton)
93  {
94  if (event->type() == QEvent::EnabledChange)
95  {
96  if (isSelectable())
97  {
98  if (mChooseButton->isEnabled())
99  {
100  return false;
101  }
102  else
103  {
104  mChooseButton->setEnabled(true);
105  mChooseButton->update();
106  return true;
107  }
108  }
109  else
110  {
111  if (mChooseButton->isEnabled())
112  {
113  mChooseButton->setEnabled(false);
114  mChooseButton->update();
115  return true;
116  }
117  else
118  {
119  return false;
120  }
121  }
122  }
123  }
124  return QFileDialog::eventFilter(obj, event);
125  }
126 
128  {
129  if (isSelectable())
130  {
131  QDialog::accept();
132  }
133  else
134  {
135  QMessageBox::warning(this,"Warning","Selected item is not a HAL project:\n"
136  + FileManager::directoryStatusText(mSelectedDirectoryStatus));
137  QDialog::reject();
138  }
139  }
140 
141  void ProjectDirDialog::handleCurrentChanged(const QString& path)
142  {
143  QString testPath = QFileInfo(path).isAbsolute()
144  ? path
145  : QDir(directory()).absoluteFilePath(path);
146 
147  mSelectedDirectoryStatus = FileManager::directoryStatus(testPath);
148 
149  if (mChooseButton)
150  mChooseButton->setEnabled(isSelectable());
151  if (mStatus)
152  mStatus->setMessage(testPath, mSelectedDirectoryStatus);
153  }
154 
155 }
Stores information about the currently opened netlist.
Definition: file_manager.h:48
static DirectoryStatus directoryStatus(const QString &pathname)
static QString directoryStatusText(DirectoryStatus stat)
bool eventFilter(QObject *obj, QEvent *event) override
ProjectDirDialog(const QString &title, const QString &defaultDir, QWidget *parent=nullptr)
ProjectDirDialogStatus(QWidget *parent=nullptr)
void setMessage(const QString &path, FileManager::DirectoryStatus stat)
void addStretch(int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void accept()
virtual bool eventFilter(QObject *o, QEvent *e) override
virtual void reject()
QList< QAbstractButton * > buttons() const const
QString absoluteFilePath(const QString &fileName) const const
void currentChanged(const QString &path)
QDir directory() const const
void directoryEntered(const QString &directory)
void setFileMode(QFileDialog::FileMode mode)
void setDirectory(const QString &directory)
void setFilter(QDir::Filters filters)
void setOption(QFileDialog::Option option, bool on)
bool isAbsolute() const const
void setLineWidth(int)
void setFrameStyle(int style)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setPixmap(const QPixmap &)
void setText(const QString &)
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void installEventFilter(QObject *filterObj)
bool isEnabled() const const
virtual bool event(QEvent *event) override
void update()
void setWindowTitle(const QString &)