PDF4QT/Pdf4QtLibGui/pdfrecentfilemanager.cpp

107 lines
3.1 KiB
C++
Raw Normal View History

2022-01-30 18:23:25 +01:00
// Copyright (C) 2020-2022 Jakub Melka
2021-09-27 11:14:20 +02:00
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#include "pdfrecentfilemanager.h"
2022-01-30 18:23:25 +01:00
#include "pdfdbgheap.h"
2021-09-27 11:14:20 +02:00
namespace pdfviewer
{
PDFRecentFileManager::PDFRecentFileManager(QObject* parent) :
BaseClass(parent),
m_recentFilesLimit(DEFAULT_RECENT_FILES),
m_actions()
{
// Initialize actions
int index = 0;
for (auto it = m_actions.begin(); it != m_actions.end(); ++it)
{
QAction* recentFileAction = new QAction(this);
recentFileAction->setObjectName(QString("actionRecentFile%1").arg(++index));
recentFileAction->setVisible(false);
connect(recentFileAction, &QAction::triggered, this, &PDFRecentFileManager::onRecentFileActionTriggered);
*it = recentFileAction;
}
}
void PDFRecentFileManager::addRecentFile(QString fileName)
{
m_recentFiles.removeAll(fileName);
m_recentFiles.push_front(fileName);
update();
}
void PDFRecentFileManager::setRecentFiles(QStringList recentFiles)
{
if (m_recentFiles != recentFiles)
{
m_recentFiles = qMove(recentFiles);
update();
}
}
void PDFRecentFileManager::setRecentFilesLimit(int recentFilesLimit)
{
recentFilesLimit = qBound(getMinimumRecentFiles(), recentFilesLimit, getMaximumRecentFiles());
if (m_recentFilesLimit != recentFilesLimit)
{
m_recentFilesLimit = recentFilesLimit;
update();
}
}
void PDFRecentFileManager::update()
{
while (m_recentFiles.size() > m_recentFilesLimit)
{
m_recentFiles.pop_back();
}
for (int i = 0; i < static_cast<int>(m_actions.size()); ++i)
2021-09-27 11:14:20 +02:00
{
QAction* recentFileAction = m_actions[i];
if (i < m_recentFiles.size())
{
recentFileAction->setData(m_recentFiles[i]);
recentFileAction->setText(tr("(&%1) %2").arg(i + 1).arg(m_recentFiles[i]));
2021-09-27 11:14:20 +02:00
recentFileAction->setVisible(true);
}
else
{
recentFileAction->setData(QVariant());
recentFileAction->setText(tr("Recent file dummy &%1").arg(i + 1));
2021-09-27 11:14:20 +02:00
recentFileAction->setVisible(false);
}
}
}
void PDFRecentFileManager::onRecentFileActionTriggered()
{
QAction* action = qobject_cast<QAction*>(sender());
Q_ASSERT(action);
QVariant data = action->data();
if (data.typeId() == QMetaType::QString)
2021-09-27 11:14:20 +02:00
{
2022-09-18 13:08:21 +02:00
Q_EMIT fileOpenRequest(data.toString());
2021-09-27 11:14:20 +02:00
}
}
} // namespace pdfviewer