ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvRecentFiles.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ecvRecentFiles.h"
9 
10 #include "MainWindow.h"
11 #include "ecvPersistentSettings.h"
12 #include "ecvSettingManager.h"
13 
14 // QT
15 #include <QAction>
16 #include <QDir>
17 #include <QFile>
18 #include <QMenu>
19 #include <QString>
20 #include <QStringList>
21 
22 QString ecvRecentFiles::s_settingKey("RecentFiles");
23 // const QString SEPERATOR = ";";
24 
25 ecvRecentFiles::ecvRecentFiles(QWidget *parent) : QObject(parent) {
26  m_menu = new QMenu(tr("Open Recent..."), parent);
27  m_menu->setIcon(QIcon(":/Resources/images/svg/pqRecentFile.png"));
28  m_actionClearMenu = new QAction(tr("Clear Menu"), this);
29 
30  connect(m_actionClearMenu, &QAction::triggered, this, [this]() {
32 
33  updateMenu();
34  });
35 
36  updateMenu();
37 }
38 
39 QMenu *ecvRecentFiles::menu() { return m_menu; }
40 
41 void ecvRecentFiles::addFilePath(const QString &filePath) {
42  QStringList list =
44  .toStringList();
45 
46  list.removeAll(filePath);
47  list.prepend(filePath);
48 
49  // only save the last ten files
50  if (list.count() > 10) {
51  list = list.mid(0, 10);
52  }
53  ecvSettingManager::setValue(ecvPS::LoadFile(), s_settingKey, list);
54 
55  updateMenu();
56 }
57 
58 void ecvRecentFiles::updateMenu() {
59  m_menu->clear();
60 
61  const QStringList recentList = listRecent();
62 
63  for (const QString &recentFile : recentList) {
64  QAction *recentAction = new QAction(contractFilePath(recentFile), this);
65 
66  recentAction->setData(recentFile);
67 
68  connect(recentAction, &QAction::triggered, this,
69  &ecvRecentFiles::openFileFromAction);
70 
71  m_menu->addAction(recentAction);
72  }
73 
74  if (!m_menu->actions().isEmpty()) {
75  m_menu->addSeparator();
76  m_menu->addAction(m_actionClearMenu);
77  }
78 
79  m_menu->setEnabled(!m_menu->actions().isEmpty());
80 }
81 
82 void ecvRecentFiles::openFileFromAction() {
83  QAction *action = qobject_cast<QAction *>(sender());
84 
85  Q_ASSERT(action);
86 
87  QString fileName = action->data().toString();
88 
89  if (!QFile::exists(fileName)) {
90  return;
91  }
92 
93  QStringList fileListOfOne{fileName};
94 
95  MainWindow::TheInstance()->addToDB(fileListOfOne);
96 }
97 
98 QStringList ecvRecentFiles::listRecent() {
99  QStringList list =
101  .toStringList();
102 
103  QStringList::iterator iter = list.begin();
104 
105  while (iter != list.end()) {
106  const QString filePath = *iter;
107 
108  if (!QFile::exists(filePath)) {
109  iter = list.erase(iter);
110  continue;
111  }
112 
113  ++iter;
114  }
115 
116  return list;
117 }
118 
119 QString ecvRecentFiles::contractFilePath(const QString &filePath) {
120  QString homePath = QDir::toNativeSeparators(QDir::homePath());
121  QString newPath = QDir::toNativeSeparators(filePath);
122 
123  if (newPath.startsWith(homePath)) {
124  return newPath.replace(0, QDir::homePath().length(), '~');
125  }
126 
127  return filePath;
128 }
129 
130 QString ecvRecentFiles::expandFilePath(const QString &filePath) {
131  QString newPath = QDir::toNativeSeparators(filePath);
132 
133  if (newPath.startsWith('~')) {
134  QString homePath = QDir::toNativeSeparators(QDir::homePath());
135 
136  return newPath.replace(0, 1, homePath);
137  }
138 
139  return filePath;
140 }
static MainWindow * TheInstance()
Returns the unique instance of this object.
void addToDB(const QStringList &filenames, QString fileFilter=QString(), bool displayDialog=true)
static const QString LoadFile()
QMenu * menu()
Returns a "most recently used file" menu.
ecvRecentFiles(QWidget *parent)
void addFilePath(const QString &filePath)
Adds a file path to the recently used menu.
static void removeKey(const QString &section, const QString &key)
static QVariant getValue(const QString &section, const QString &key, const QVariant &defaultValue=QVariant())
static void setValue(const QString &section, const QString &key, const QVariant &value)
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162