Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
RecentFilesDialog.cpp
1/*
2 * Marvelous OTF2 Traces Interactive Visualizer (MOTIV)
3 * Copyright (C) 2023 Florian Gallrein, Björn Gehrke
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "RecentFilesDialog.hpp"
19
20#include <QPushButton>
21#include <QFileDialog>
22#include <QVBoxLayout>
23#include <QSettings>
24#include <QListView>
25#include <QStringListModel>
26#include <QLabel>
27
28#include "src/models/AppSettings.hpp"
29#include "Otf2FileDialog.hpp"
30
31RecentFilesDialog::RecentFilesDialog(QString *dest) : dest(dest) {
32 auto layout = new QVBoxLayout;
33 layout->setAlignment(Qt::AlignTop);
34 this->setLayout(layout);
35
36 auto pushButton = new QPushButton;
37 pushButton->setText(tr("&Open"));
38 layout->addWidget(pushButton);
39
40 auto fileDialog = new Otf2FileDialog(this);
41
42 QObject::connect(pushButton, &QPushButton::clicked, fileDialog, &Otf2FileDialog::exec);
43 QObject::connect(fileDialog, &QFileDialog::fileSelected, [this](const QString &selectedFile){
44 if (!selectedFile.isEmpty()) {
45 AppSettings::getInstance().recentlyOpenedFilesPush(selectedFile);
46 *this->dest = selectedFile;
47 this->accept();
48 }
49 });
50
51 auto label = new QLabel(QObject::tr("Recently opened files:"));
52 layout->addWidget(label);
53
54 auto recentlyOpenedFiles = AppSettings::getInstance().recentlyOpenedFiles();
55
56 if(recentlyOpenedFiles.isEmpty()) {
57 auto listEmptyLabel = new QLabel(QObject::tr("No recent files"));
58 listEmptyLabel->setStyleSheet("QLabel { color : gray; }");
59 layout->addWidget(listEmptyLabel);
60 } else {
61 auto listView = new QListView;
62 listView->setEditTriggers(QListView::NoEditTriggers);
63 layout->addWidget(listView);
64
65 auto stringListModel = new QStringListModel;
66 stringListModel->setStringList(recentlyOpenedFiles);
67 listView->setModel(stringListModel);
68
69 connect(listView, &QAbstractItemView::doubleClicked, [this](const QModelIndex &idx){
70 *this->dest = idx.data().toString();
71 this->accept();
72 });
73
74// auto clearButton = new QPushButton(tr("&Clear"));
75// layout->addWidget(clearButton);
76// connect(clearButton, &QPushButton::clicked, [] {
77// AppSettings::getInstance().recentlyOpenedFilesClear();
78// });
79 }
80}
static AppSettings & getInstance()
Definition: AppSettings.hpp:36
const QStringList & recentlyOpenedFiles() const
Returns the recently opened files.
Definition: AppSettings.cpp:28
Wrapper around QFileDialog that displays files with a otf2-file ending only.
RecentFilesDialog(QString *dest)