diff --git a/libs/widgetutils/config/krecentfilesaction.cpp b/libs/widgetutils/config/krecentfilesaction.cpp index 40fdf93192..e67e99a6eb 100644 --- a/libs/widgetutils/config/krecentfilesaction.cpp +++ b/libs/widgetutils/config/krecentfilesaction.cpp @@ -1,345 +1,355 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Reginald Stadlbauer (C) 1999 Simon Hausmann (C) 2000 Nicolas Hadacek (C) 2000 Kurt Granroth (C) 2000 Michael Koch (C) 2001 Holger Freyther (C) 2002 Ellis Whitehead (C) 2002 Joseph Wenninger (C) 2003 Andras Mantia (C) 2005-2006 Hamish Rodda This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "krecentfilesaction.h" #include "krecentfilesaction_p.h" #include #include #include #include #include #include #include #include KRecentFilesAction::KRecentFilesAction(QObject *parent) : KSelectAction(parent), d_ptr(new KRecentFilesActionPrivate(this)) { Q_D(KRecentFilesAction); d->init(); } KRecentFilesAction::KRecentFilesAction(const QString &text, QObject *parent) : KSelectAction(parent), d_ptr(new KRecentFilesActionPrivate(this)) { Q_D(KRecentFilesAction); d->init(); // Want to keep the ampersands setText(text); } KRecentFilesAction::KRecentFilesAction(const QIcon &icon, const QString &text, QObject *parent) : KSelectAction(parent), d_ptr(new KRecentFilesActionPrivate(this)) { Q_D(KRecentFilesAction); d->init(); setIcon(icon); // Want to keep the ampersands setText(text); } void KRecentFilesActionPrivate::init() { Q_Q(KRecentFilesAction); delete q->menu(); q->setMenu(new QMenu()); q->setToolBarMode(KSelectAction::MenuMode); m_noEntriesAction = q->menu()->addAction(i18n("No Entries")); m_noEntriesAction->setObjectName(QLatin1String("no_entries")); m_noEntriesAction->setEnabled(false); clearSeparator = q->menu()->addSeparator(); clearSeparator->setVisible(false); clearSeparator->setObjectName(QLatin1String("separator")); clearAction = q->menu()->addAction(i18n("Clear List"), q, SLOT(clear())); clearAction->setObjectName(QLatin1String("clear_action")); clearAction->setVisible(false); q->setEnabled(false); q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*))); } KRecentFilesAction::~KRecentFilesAction() { delete d_ptr; } void KRecentFilesActionPrivate::_k_urlSelected(QAction *action) { Q_Q(KRecentFilesAction); emit q->urlSelected(m_urls[action]); } int KRecentFilesAction::maxItems() const { Q_D(const KRecentFilesAction); return d->m_maxItems; } void KRecentFilesAction::setMaxItems(int maxItems) { Q_D(KRecentFilesAction); // set new maxItems d->m_maxItems = maxItems; // remove all excess items while (selectableActionGroup()->actions().count() > maxItems) { delete removeAction(selectableActionGroup()->actions().last()); } } static QString titleWithSensibleWidth(const QString &nameValue, const QString &value) { // Calculate 3/4 of screen geometry, we do not want // action titles to be bigger than that // Since we do not know in which screen we are going to show // we choose the min of all the screens const QDesktopWidget desktopWidget; int maxWidthForTitles = INT_MAX; for (int i = 0; i < desktopWidget.screenCount(); ++i) { maxWidthForTitles = qMin(maxWidthForTitles, desktopWidget.availableGeometry(i).width() * 3 / 4); } const QFontMetrics fontMetrics = QFontMetrics(QFont()); QString title = nameValue + " [" + value + ']'; if (fontMetrics.width(title) > maxWidthForTitles) { // If it does not fit, try to cut only the whole path, though if the // name is too long (more than 3/4 of the whole text) we cut it a bit too const int nameValueMaxWidth = maxWidthForTitles * 3 / 4; const int nameWidth = fontMetrics.width(nameValue); QString cutNameValue, cutValue; if (nameWidth > nameValueMaxWidth) { cutNameValue = fontMetrics.elidedText(nameValue, Qt::ElideMiddle, nameValueMaxWidth); cutValue = fontMetrics.elidedText(value, Qt::ElideMiddle, maxWidthForTitles - nameValueMaxWidth); } else { cutNameValue = nameValue; cutValue = fontMetrics.elidedText(value, Qt::ElideMiddle, maxWidthForTitles - nameWidth); } title = cutNameValue + " [" + cutValue + ']'; } return title; } void KRecentFilesAction::addUrl(const QUrl &_url, const QString &name) { Q_D(KRecentFilesAction); /** * Create a deep copy here, because if _url is the parameter from * urlSelected() signal, we will delete it in the removeAction() call below. * but access it again in the addAction call... => crash */ const QUrl url(_url); if (url.isLocalFile() && url.toLocalFile().startsWith(QDir::tempPath())) { return; } const QString tmpName = name.isEmpty() ? url.fileName() : name; const QString pathOrUrl(url.toDisplayString(QUrl::PreferLocalFile)); #ifdef Q_OS_WIN const QString file = url.isLocalFile() ? QDir::toNativeSeparators(pathOrUrl) : pathOrUrl; #else const QString file = pathOrUrl; #endif // remove file if already in list foreach (QAction *action, selectableActionGroup()->actions()) { const QString urlStr = d->m_urls[action].toDisplayString(QUrl::PreferLocalFile); #ifdef Q_OS_WIN const QString tmpFileName = url.isLocalFile() ? QDir::toNativeSeparators(urlStr) : urlStr; if (tmpFileName.endsWith(file, Qt::CaseInsensitive)) #else if (urlStr.endsWith(file)) #endif { removeAction(action)->deleteLater(); break; } } // remove oldest item if already maxitems in list if (d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems) { // remove oldest added item delete removeAction(selectableActionGroup()->actions().first()); } d->m_noEntriesAction->setVisible(false); d->clearSeparator->setVisible(true); d->clearAction->setVisible(true); setEnabled(true); // add file to list const QString title = titleWithSensibleWidth(tmpName, file); QAction *action = new QAction(title, selectableActionGroup()); addAction(action, url, tmpName); } void KRecentFilesAction::addAction(QAction *action, const QUrl &url, const QString &name) { Q_D(KRecentFilesAction); menu()->insertAction(menu()->actions().value(0), action); d->m_shortNames.insert(action, name); d->m_urls.insert(action, url); } QAction *KRecentFilesAction::removeAction(QAction *action) { Q_D(KRecentFilesAction); KSelectAction::removeAction(action); d->m_shortNames.remove(action); d->m_urls.remove(action); return action; } void KRecentFilesAction::removeUrl(const QUrl &url) { Q_D(KRecentFilesAction); for (QMap::ConstIterator it = d->m_urls.constBegin(); it != d->m_urls.constEnd(); ++it) if (it.value() == url) { delete removeAction(it.key()); return; } } QList KRecentFilesAction::urls() const { Q_D(const KRecentFilesAction); - return d->m_urls.values(); + + // switch order so last opened file is first + QList sortedList; + for (int i=(d_urls.length()-1); i >= 0; i--) { + sortedList.append(d_urls[i]); + } + + return sortedList; } void KRecentFilesAction::clear() { clearEntries(); emit recentListCleared(); } void KRecentFilesAction::clearEntries() { Q_D(KRecentFilesAction); KSelectAction::clear(); d->m_shortNames.clear(); d->m_urls.clear(); d->m_noEntriesAction->setVisible(true); d->clearSeparator->setVisible(false); d->clearAction->setVisible(false); setEnabled(false); + + d_urls.clear(); } void KRecentFilesAction::loadEntries(const KConfigGroup &_config) { Q_D(KRecentFilesAction); clearEntries(); QString key; QString value; QString nameKey; QString nameValue; QString title; QUrl url; KConfigGroup cg = _config; if (cg.name().isEmpty()) { cg = KConfigGroup(cg.config(), "RecentFiles"); } bool thereAreEntries = false; // read file list for (int i = 1; i <= d->m_maxItems; i++) { key = QString("File%1").arg(i); value = cg.readPathEntry(key, QString()); if (value.isEmpty()) { continue; } url = QUrl::fromUserInput(value); + d_urls.append(QUrl(url)); // will be used to retrieve on the welcome screen // Don't restore if file doesn't exist anymore if (url.isLocalFile() && !QFile::exists(url.toLocalFile())) { continue; } // Don't restore where the url is already known (eg. broken config) if (d->m_urls.values().contains(url)) { continue; } #ifdef Q_OS_WIN // convert to backslashes if (url.isLocalFile()) { value = QDir::toNativeSeparators(value); } #endif nameKey = QString("Name%1").arg(i); nameValue = cg.readPathEntry(nameKey, url.fileName()); title = titleWithSensibleWidth(nameValue, value); if (!value.isNull()) { thereAreEntries = true; addAction(new QAction(title, selectableActionGroup()), url, nameValue); } } if (thereAreEntries) { d->m_noEntriesAction->setVisible(false); d->clearSeparator->setVisible(true); d->clearAction->setVisible(true); setEnabled(true); } } void KRecentFilesAction::saveEntries(const KConfigGroup &_cg) { Q_D(KRecentFilesAction); QString key; QString value; QStringList lst = items(); KConfigGroup cg = _cg; if (cg.name().isEmpty()) { cg = KConfigGroup(cg.config(), "RecentFiles"); } cg.deleteGroup(); // write file list for (int i = 1; i <= selectableActionGroup()->actions().count(); i++) { key = QString("File%1").arg(i); // i - 1 because we started from 1 value = d->m_urls[ selectableActionGroup()->actions()[ i - 1 ] ].toDisplayString(QUrl::PreferLocalFile); cg.writePathEntry(key, value); key = QString("Name%1").arg(i); value = d->m_shortNames[ selectableActionGroup()->actions()[ i - 1 ] ]; cg.writePathEntry(key, value); } } #include "moc_krecentfilesaction.cpp" diff --git a/libs/widgetutils/config/krecentfilesaction.h b/libs/widgetutils/config/krecentfilesaction.h index afb5227388..066e3a691f 100644 --- a/libs/widgetutils/config/krecentfilesaction.h +++ b/libs/widgetutils/config/krecentfilesaction.h @@ -1,193 +1,194 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Reginald Stadlbauer (C) 1999 Simon Hausmann (C) 2000 Nicolas Hadacek (C) 2000 Kurt Granroth (C) 2000 Michael Koch (C) 2001 Holger Freyther (C) 2002 Ellis Whitehead (C) 2003 Andras Mantia (C) 2005-2006 Hamish Rodda This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KRECENTFILESACTION_H #define KRECENTFILESACTION_H #include #include #include class KConfigGroup; class KRecentFilesActionPrivate; /** * @short Recent files action * * This class is an action to handle a recent files submenu. * The best way to create the action is to use KStandardAction::openRecent. * Then you simply need to call loadEntries on startup, saveEntries * on shutdown, addURL when your application loads/saves a file. * * @author Michael Koch */ class KRITAWIDGETUTILS_EXPORT KRecentFilesAction : public KSelectAction { Q_OBJECT Q_PROPERTY(int maxItems READ maxItems WRITE setMaxItems) Q_DECLARE_PRIVATE(KRecentFilesAction) public: /** * Constructs an action with the specified parent. * * @param parent The parent of this action. */ explicit KRecentFilesAction(QObject *parent); /** * Constructs an action with text; a shortcut may be specified by * the ampersand character (e.g. \"&Option\" creates a shortcut with key \e O ) * * This is the most common KAction used when you do not have a * corresponding icon (note that it won't appear in the current version * of the "Edit ToolBar" dialog, because an action needs an icon to be * plugged in a toolbar...). * * @param text The text that will be displayed. * @param parent The parent of this action. */ KRecentFilesAction(const QString &text, QObject *parent); /** * Constructs an action with text and an icon; a shortcut may be specified by * the ampersand character (e.g. \"&Option\" creates a shortcut with key \e O ) * * This is the other common KAction used. Use it when you * \e do have a corresponding icon. * * @param icon The icon to display. * @param text The text that will be displayed. * @param parent The parent of this action. */ KRecentFilesAction(const QIcon &icon, const QString &text, QObject *parent); /** * Destructor. */ ~KRecentFilesAction() override; /** * Adds \a action to the list of URLs, with \a url and title \a name. * * Do not use addAction(QAction*), as no url will be associated, and * consequently urlSelected() will not be emitted when \a action is selected. */ void addAction(QAction *action, const QUrl &url, const QString &name); /** * Reimplemented for internal reasons. */ QAction *removeAction(QAction *action) override; public Q_SLOTS: /** * Clears the recent files list. * Note that there is also an action shown to the user for clearing the list. */ virtual void clear(); public: /** * Returns the maximum of items in the recent files list. */ int maxItems() const; /** * Sets the maximum of items in the recent files list. * The default for this value is 10 set in the constructor. * * If this value is lesser than the number of items currently * in the recent files list the last items are deleted until * the number of items are equal to the new maximum. */ void setMaxItems(int maxItems); /** * Loads the recent files entries from a given KConfigGroup object. * You can provide the name of the group used to load the entries. * If the groupname is empty, entries are loaded from a group called 'RecentFiles'. * Local file entries that do not exist anymore are not restored. * */ void loadEntries(const KConfigGroup &config); /** * Saves the current recent files entries to a given KConfigGroup object. * You can provide the name of the group used to load the entries. * If the groupname is empty, entries are saved to a group called 'RecentFiles' * */ void saveEntries(const KConfigGroup &config); /** * Add URL to recent files list. * * @param url The URL of the file * @param name The user visible pretty name that appears before the URL */ void addUrl(const QUrl &url, const QString &name = QString()); /** * Remove an URL from the recent files list. * * @param url The URL of the file */ void removeUrl(const QUrl &url); /** * Retrieve a list of all URLs in the recent files list. */ QList urls() const; Q_SIGNALS: /** * This signal gets emitted when the user selects a URL. * * @param url The URL that the user selected. */ void urlSelected(const QUrl &url); /** * This signal gets emitted when the user clear list. * So when user stores url in specific config file it can saveEntry. * @since 4.3 */ void recentListCleared(); private: //Internal void clearEntries(); // Don't warn about the virtual overload. As the comment of the other // addAction() says, addAction( QAction* ) should not be used. using KSelectAction::addAction; KRecentFilesActionPrivate *d_ptr; + QList d_urls; Q_PRIVATE_SLOT(d_func(), void _k_urlSelected(QAction *)) }; #endif