Warning, file /multimedia/kid3/src/app/qt/recentfilesmenu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file recentfilesmenu.cpp
0003  * Menu to open recent files.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 15 Aug 2010
0008  *
0009  * Copyright (C) 2010-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "recentfilesmenu.h"
0028 #include <QAction>
0029 #include <QDir>
0030 #include "isettings.h"
0031 
0032 namespace {
0033 
0034 constexpr int MAX_RECENT_FILES = 10;
0035 
0036 }
0037 
0038 /**
0039  * Constructor.
0040  *
0041  * @param parent parent widget
0042  */
0043 RecentFilesMenu::RecentFilesMenu(QWidget* parent) : QMenu(parent)
0044 {
0045   setObjectName(QLatin1String("RecentFilesMenu"));
0046 }
0047 
0048 /**
0049  * Add directory to list of recent files.
0050  *
0051  * @param dir path to directory
0052  */
0053 void RecentFilesMenu::addDirectory(const QString& dir)
0054 {
0055   QString path = QDir(dir).canonicalPath();
0056   if (path.isNull())
0057     return;
0058 
0059   // first remove the path if it already exists
0060   if (int pathIdx = m_files.indexOf(path); pathIdx != -1) {
0061     m_files.removeAt(pathIdx);
0062   }
0063 
0064   m_files.prepend(path);
0065   if (m_files.size() > MAX_RECENT_FILES) {
0066     m_files.removeLast();
0067   }
0068 
0069   updateRecentFileActions();
0070 }
0071 
0072 /**
0073  * Saves the current recent files entries to a given configuration.
0074  *
0075  * @param config configuration settings
0076  */
0077 void RecentFilesMenu::saveEntries(ISettings* config)
0078 {
0079   config->beginGroup(QLatin1String("RecentFiles"), true);
0080   config->setValue(QLatin1String("Files"), QVariant(m_files));
0081   config->endGroup();
0082 }
0083 
0084 /**
0085  * Loads the recent files entries from a given configuration.
0086  *
0087  * @param config configuration settings
0088  */
0089 void RecentFilesMenu::loadEntries(ISettings* config)
0090 {
0091   config->beginGroup(QLatin1String("RecentFiles"), true);
0092   m_files = config->value(QLatin1String("Files"), m_files).toStringList();
0093   config->endGroup();
0094 
0095   while (m_files.size() > MAX_RECENT_FILES) {
0096     m_files.removeLast();
0097   }
0098 
0099   updateRecentFileActions();
0100 }
0101 
0102 /**
0103  * Update the recent file actions.
0104  */
0105 void RecentFilesMenu::updateRecentFileActions()
0106 {
0107   int i = 0;
0108   clear();
0109   for (auto it = m_files.constBegin(); it != m_files.constEnd(); ++it) {
0110     auto act = new QAction(this);
0111     act->setText(QString(QLatin1String("&%1 %2")).arg(++i).arg(*it));
0112     act->setData(*it);
0113     connect(act, &QAction::triggered, this, &RecentFilesMenu::openRecentFile);
0114     this->addAction(act);
0115   }
0116   if (i > 0) {
0117     addSeparator();
0118     auto clearListAction = new QAction(this);
0119     clearListAction->setText(tr("&Clear List"));
0120     connect(clearListAction, &QAction::triggered, this, &RecentFilesMenu::clearList);
0121     this->addAction(clearListAction);
0122     setEnabled(true);
0123   } else {
0124     setEnabled(false);
0125   }
0126 }
0127 
0128 /**
0129  * Emit a load file signal when a recent file has to be loaded.
0130  */
0131 void RecentFilesMenu::openRecentFile()
0132 {
0133   auto action = qobject_cast<QAction*>(sender());
0134   if (action) {
0135     emit loadFile(action->data().toString());
0136   }
0137 }
0138 
0139 /**
0140  * Clear the list of recent files.
0141  */
0142 void RecentFilesMenu::clearList()
0143 {
0144   m_files.clear();
0145   updateRecentFileActions();
0146 }