21 #include <QHBoxLayout>
22 #include <QVBoxLayout>
23 #include <QListWidget>
24 #include <QDialogButtonBox>
27 #include <QGuiApplication>
28 #include <QPushButton>
29 #include <QHeaderView>
30 #include <QToolButton>
31 #include <QMessageBox>
39 QDialog(0, Qt::Window),
45 QVBoxLayout *layout =
new QVBoxLayout;
48 QDialogButtonBox *buttons =
new QDialogButtonBox;
51 buttons->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Open);
52 title = buttons->button(QDialogButtonBox::Open)->text();
54 buttons->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Save);
55 title = buttons->button(QDialogButtonBox::Save)->text();
59 layout->addWidget(
new QLabel(QString(
"<h1>%1</h1>").arg(title)));
64 mFilesList->header()->setSectionResizeMode(0, QHeaderView::Stretch);
65 mFilesList->header()->setSectionResizeMode(1, QHeaderView::Fixed);
68 QStringList files = dir.entryList(QDir::Files | QDir::Writable);
69 int itemSize =
mFilesList->fontMetrics().height();
70 for (QString file : files) {
71 if (file.endsWith(
".ept") ==
false) {
74 file = file.left(file.size() - 4);
75 QTreeWidgetItem *item =
new QTreeWidgetItem(
mFilesList, QStringList() << file);
76 item->setSizeHint(0, QSize(0, itemSize * 2));
77 QToolButton *tb =
new QToolButton;
78 tb->setAutoRaise(
true);
79 tb->setIcon(QIcon(
":/media/icons/edit-delete.png"));
80 tb->setIconSize(QSize(itemSize, itemSize) * 1.7);
81 QObject::connect(tb, SIGNAL(clicked(
bool)),
this, SLOT(
onDeleteFile()));
87 mFilesList->header()->setStretchLastSection(
false);
88 mFilesList->header()->resizeSection(1, itemSize * 2);
89 mFilesList->header()->resizeSection(0, QHeaderView::Stretch);
91 QHBoxLayout *nameLayout =
new QHBoxLayout(
this);
92 nameLayout->addWidget(
new QLabel(tr(
"Name:")));
94 QLineEdit *nameEdit =
new QLineEdit;
96 nameLayout->addWidget(nameEdit);
99 layout->addLayout(nameLayout);
102 layout->addWidget(buttons);
105 QObject::connect(buttons, SIGNAL(accepted()),
this, SLOT(
accept()));
106 QObject::connect(buttons, SIGNAL(rejected()),
this, SLOT(reject()));
107 QObject::connect(
mFilesList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
this, SLOT(
accept()));
108 QObject::connect(
mFilesList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
this, SLOT(
onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
109 QObject::connect(
this, SIGNAL(
fileSelectionChanged(QString)), nameEdit, SLOT(setText(QString)));
126 if (dialog.exec() == QDialog::Rejected) {
135 if (dialog.exec() == QDialog::Rejected) {
147 if (QMessageBox::warning(
this,
149 tr(
"A file with the given name already exists at %1. Do you want to overwrite it?").arg(fi.absoluteFilePath()),
150 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
156 if (fi.baseName().isEmpty()) {
157 QMessageBox::information(
this, tr(
"Invalid filename"), tr(
"Please provide a valid filename."));
164 EptAssert(sender(),
"This is a slot and has to be sent by a QPushButton.");
166 QTreeWidgetItem *fileItem =
nullptr;
167 for (
int i = 0; i <
mFilesList->topLevelItemCount(); ++i) {
168 QTreeWidgetItem *item =
mFilesList->topLevelItem(i);
169 if (sender() ==
mFilesList->itemWidget(item, 1)) {
175 EptAssert(fileItem,
"Item not found that contains the sender in column 1.");
178 if (QMessageBox::question(
this, tr(
"Remove file"),
179 tr(
"Are you sure that you really want to delete the file \"%1\"?").arg(fileItem->text(0)),
180 QMessageBox::Yes | QMessageBox::Cancel) == QMessageBox::Cancel) {
185 QFile file(
mDir.absoluteFilePath(fileItem->text(0) +
".ept"));
187 EptAssert(file.exists(),
"File has to exists.");
189 if (!file.remove()) {
190 LogW(
"File '%s' could not be removed.", file.fileName().toStdString().c_str());
void fileSelectionChanged(QString)
static QString getSaveFile(QDir dir)
SimpleFileDialog(Mode mode, QDir dir)
static QString getOpenFile(QDir dir)
void onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
QString getFileName() const