-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Github Issue #9: Show list of recent files
- Loading branch information
Showing
17 changed files
with
685 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ x.y.z | |
===== | ||
|
||
New features: | ||
|
||
* Fix Github Issue #9: Show list of recent files | ||
|
||
Bug fixes: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// This file is part of Heimer. | ||
// Copyright (C) 2020 Jussi Lind <jussi.lind@iki.fi> | ||
// | ||
// Heimer is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// Heimer is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Heimer. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "recent_files_manager.hpp" | ||
#include "constants.hpp" | ||
#include "contrib/SimpleLogger/src/simple_logger.hpp" | ||
|
||
#include <stdexcept> | ||
|
||
#include <QSettings> | ||
|
||
std::unique_ptr<RecentFilesManager> RecentFilesManager::m_instance; | ||
|
||
RecentFilesManager::RecentFilesManager() | ||
{ | ||
if (RecentFilesManager::m_instance) { | ||
throw std::runtime_error("RecentFilesManager already instantiated!"); | ||
} | ||
|
||
QSettings settings; | ||
const int size = settings.beginReadArray(Constants::RecentFiles::QSETTINGS_ARRAY_KEY); | ||
for (int i = 0; i < size; i++) { | ||
settings.setArrayIndex(i); | ||
m_recentFiles.push_back(settings.value(Constants::RecentFiles::QSETTINGS_FILE_PATH_KEY).toString()); | ||
} | ||
settings.endArray(); | ||
} | ||
|
||
RecentFilesManager & RecentFilesManager::instance() | ||
{ | ||
if (!RecentFilesManager::m_instance) { | ||
RecentFilesManager::m_instance.reset(new RecentFilesManager); | ||
juzzlin::L().debug() << "RecentFilesManager created"; | ||
} | ||
|
||
return *RecentFilesManager::m_instance; | ||
} | ||
|
||
void RecentFilesManager::addRecentFile(QString filePath) | ||
{ | ||
m_recentFiles.removeAll(filePath); | ||
m_recentFiles.push_front(filePath); | ||
|
||
while (m_recentFiles.size() > Constants::RecentFiles::MAX_FILES) { | ||
m_recentFiles.pop_back(); | ||
} | ||
|
||
juzzlin::L().debug() << "Added recent file: " << filePath.toStdString(); | ||
juzzlin::L().debug() << "Recent file count: " << m_recentFiles.size(); | ||
|
||
QSettings settings; | ||
settings.beginWriteArray(Constants::RecentFiles::QSETTINGS_ARRAY_KEY); | ||
for (int i = 0; i < m_recentFiles.size(); i++) { | ||
settings.setArrayIndex(i); | ||
settings.setValue(Constants::RecentFiles::QSETTINGS_FILE_PATH_KEY, m_recentFiles.at(i)); | ||
} | ||
settings.endArray(); | ||
} | ||
|
||
const QList<QString> & RecentFilesManager::getRecentFiles() const | ||
{ | ||
return m_recentFiles; | ||
} | ||
|
||
bool RecentFilesManager::hasRecentFiles() const | ||
{ | ||
return !m_recentFiles.empty(); | ||
} | ||
|
||
void RecentFilesManager::setSelectedFile(QString selectedFile) | ||
{ | ||
juzzlin::L().debug() << "Recent file selected: " << selectedFile.toStdString(); | ||
|
||
m_selectedFile = selectedFile; | ||
} | ||
|
||
QString RecentFilesManager::selectedFile() const | ||
{ | ||
return m_selectedFile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This file is part of Heimer. | ||
// Copyright (C) 2020 Jussi Lind <jussi.lind@iki.fi> | ||
// | ||
// Heimer is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// Heimer is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Heimer. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef RECENT_FILES_MANAGER_HPP | ||
#define RECENT_FILES_MANAGER_HPP | ||
|
||
#include <QList> | ||
#include <QObject> | ||
#include <QString> | ||
|
||
#include <memory> | ||
|
||
class RecentFilesManager : public QObject | ||
{ | ||
public: | ||
RecentFilesManager(); | ||
|
||
static RecentFilesManager & instance(); | ||
|
||
const QList<QString> & getRecentFiles() const; | ||
|
||
bool hasRecentFiles() const; | ||
|
||
QString selectedFile() const; | ||
|
||
public slots: | ||
void addRecentFile(QString filePath); | ||
|
||
void setSelectedFile(QString filePath); | ||
|
||
private: | ||
static std::unique_ptr<RecentFilesManager> m_instance; | ||
|
||
QList<QString> m_recentFiles; | ||
|
||
QString m_selectedFile; | ||
}; | ||
|
||
#endif // RECENT_FILES_MANAGER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This file is part of Heimer. | ||
// Copyright (C) 2020 Jussi Lind <jussi.lind@iki.fi> | ||
// | ||
// Heimer is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// Heimer is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Heimer. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "recent_files_menu.hpp" | ||
#include "recent_files_manager.hpp" | ||
|
||
#include <functional> | ||
|
||
RecentFilesMenu::RecentFilesMenu(QWidget * parent) | ||
: QMenu(parent) | ||
{ | ||
connect(this, &QMenu::aboutToShow, [this]() { | ||
for (auto && action : actions()) { | ||
removeAction(action); | ||
} | ||
// clang-format off | ||
for (auto && filePath : RecentFilesManager::instance().getRecentFiles()) { | ||
const auto action = addAction(filePath); | ||
const auto handler = std::bind([this](QString filePath) { | ||
RecentFilesManager::instance().setSelectedFile(filePath); | ||
this->fileSelected(filePath); | ||
}, action->text()); | ||
connect(action, &QAction::triggered, handler); | ||
} | ||
// clang-format on | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// This file is part of Heimer. | ||
// Copyright (C) 2020 Jussi Lind <jussi.lind@iki.fi> | ||
// | ||
// Heimer is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// Heimer is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Heimer. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef RECENT_FILES_MENU_HPP | ||
#define RECENT_FILES_MENU_HPP | ||
|
||
#include <QMenu> | ||
#include <QString> | ||
|
||
#include <list> | ||
|
||
class QAction; | ||
|
||
class RecentFilesMenu : public QMenu | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit RecentFilesMenu(QWidget * parent = nullptr); | ||
|
||
signals: | ||
void fileSelected(QString filePath); | ||
|
||
private: | ||
std::list<QAction *> m_actions; | ||
|
||
QString m_selectedFile; | ||
}; | ||
|
||
#endif // RECENT_FILES_MENU_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.