diff --git a/kdeui/actions/krecentfilesaction.cpp b/kdeui/actions/krecentfilesaction.cpp index 452afc81..4a3c93ea 100644 --- a/kdeui/actions/krecentfilesaction.cpp +++ b/kdeui/actions/krecentfilesaction.cpp @@ -40,78 +40,7 @@ #include -KRecentFilesAction::KRecentFilesAction(QObject *parent) - : KSelectAction(*new KRecentFilesActionPrivate, parent) -{ - Q_D(KRecentFilesAction); - d->init(); -} - -KRecentFilesAction::KRecentFilesAction(const QString &text, QObject *parent) - : KSelectAction(*new KRecentFilesActionPrivate, parent) -{ - Q_D(KRecentFilesAction); - d->init(); - - // Want to keep the ampersands - setText(text); -} - -KRecentFilesAction::KRecentFilesAction(const KIcon &icon, const QString &text, QObject *parent) - : KSelectAction(*new KRecentFilesActionPrivate, parent) -{ - 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 KMenu()); - q->setToolBarMode(KSelectAction::MenuMode); - m_noEntriesAction = q->menu()->addAction(i18n("No Entries")); - m_noEntriesAction->setEnabled(false); - clearSeparator = q->menu()->addSeparator(); - clearSeparator->setVisible(false); - clearAction = q->menu()->addAction(i18n("Clear List"), q, SLOT(clear())); - clearAction->setVisible(false); - q->setEnabled(false); - q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*))); -} - -KRecentFilesAction::~KRecentFilesAction() -{ -} - -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) +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 @@ -143,7 +72,79 @@ static QString titleWithSensibleWidth(const QString& nameValue, const QString& v return title; } -void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name ) +KRecentFilesAction::KRecentFilesAction(QObject *parent) + : KSelectAction(*new KRecentFilesActionPrivate, parent) +{ + Q_D(KRecentFilesAction); + d->init(); +} + +KRecentFilesAction::KRecentFilesAction(const QString &text, QObject *parent) + : KSelectAction(*new KRecentFilesActionPrivate, parent) +{ + Q_D(KRecentFilesAction); + d->init(); + + // Want to keep the ampersands + setText(text); +} + +KRecentFilesAction::KRecentFilesAction(const KIcon &icon, const QString &text, QObject *parent) + : KSelectAction(*new KRecentFilesActionPrivate, parent) +{ + 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 KMenu()); + q->setToolBarMode(KSelectAction::MenuMode); + m_noEntriesAction = q->menu()->addAction(i18n("No Entries")); + m_noEntriesAction->setEnabled(false); + clearSeparator = q->menu()->addSeparator(); + clearSeparator->setVisible(false); + clearAction = q->menu()->addAction(i18n("Clear List"), q, SLOT(clear())); + clearAction->setVisible(false); + q->setEnabled(false); + q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*))); +} + +KRecentFilesAction::~KRecentFilesAction() +{ +} + +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()); + } +} + +void KRecentFilesAction::addUrl(const KUrl &_url, const QString &name) { Q_D(KRecentFilesAction); /** @@ -151,25 +152,23 @@ void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name ) * urlSelected() signal, we will delete it in the removeAction() call below. * but access it again in the addAction call... => crash */ - const KUrl url( _url ); + const KUrl url(_url); - if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation("tmp", url.toLocalFile()) != url.toLocalFile() ) - return; - const QString tmpName = name.isEmpty() ? url.fileName() : name; + if (url.isLocalFile() && KGlobal::dirs()->relativeLocation("tmp", url.toLocalFile()) != url.toLocalFile()) { + return; + } + const QString tmpName = (name.isEmpty() ? url.fileName() : name); const QString file = url.pathOrUrl(); // remove file if already in list - foreach (QAction* action, selectableActionGroup()->actions()) - { - if ( d->m_urls[action].pathOrUrl().endsWith(file) ) - { - removeAction(action)->deleteLater(); - break; - } + foreach (QAction *action, selectableActionGroup()->actions()) { + if ( d->m_urls[action].pathOrUrl().endsWith(file)) { + removeAction(action)->deleteLater(); + break; + } } // remove oldest item if already maxitems in list - if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems ) - { + if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems) { // remove oldest added item delete removeAction(selectableActionGroup()->actions().first()); } @@ -184,16 +183,17 @@ void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name ) addAction(action, url, tmpName); } -void KRecentFilesAction::addAction(QAction* action, const KUrl& url, const QString& name) +void KRecentFilesAction::addAction(QAction *action, const KUrl &url, const QString &name) { - Q_D(KRecentFilesAction); - //kDebug (129) << "KRecentFilesAction::addAction(" << action << ")"; + Q_D(KRecentFilesAction); + // kDebug (129) << "KRecentFilesAction::addAction(" << action << ")"; - action->setActionGroup(selectableActionGroup()); + action->setActionGroup(selectableActionGroup()); - // Keep in sync with createToolBarWidget() - foreach (QToolButton* button, d->m_buttons) - button->insertAction(button->actions().value(0), action); + // Keep in sync with createToolBarWidget() + foreach (QToolButton* button, d->m_buttons) { + button->insertAction(button->actions().value(0), action); + } foreach (KComboBox* comboBox, d->m_comboBoxes) comboBox->insertAction(comboBox->actions().value(0), action); @@ -204,31 +204,30 @@ void KRecentFilesAction::addAction(QAction* action, const KUrl& url, const QStri d->m_urls.insert( action, url ); } -QAction* KRecentFilesAction::removeAction(QAction* action) +QAction* KRecentFilesAction::removeAction(QAction *action) { - Q_D(KRecentFilesAction); - KSelectAction::removeAction( action ); - - d->m_shortNames.remove( action ); - d->m_urls.remove( action ); - - return action; + Q_D(KRecentFilesAction); + KSelectAction::removeAction(action); + d->m_shortNames.remove(action); + d->m_urls.remove(action); + return action; } void KRecentFilesAction::removeUrl( const KUrl& 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; + 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; + } } } KUrl::List KRecentFilesAction::urls() const { - Q_D(const KRecentFilesAction); - return d->m_urls.values (); + Q_D(const KRecentFilesAction); + return d->m_urls.values(); } void KRecentFilesAction::clear() @@ -249,51 +248,52 @@ void KRecentFilesAction::clearEntries() setEnabled(false); } -void KRecentFilesAction::loadEntries( const KConfigGroup& _config) +void KRecentFilesAction::loadEntries(const KConfigGroup &config) { Q_D(KRecentFilesAction); clearEntries(); - QString key; - QString value; - QString nameKey; - QString nameValue; - QString title; - KUrl url; + QString key; + QString value; + QString nameKey; + QString nameValue; + QString title; + KUrl url; - KConfigGroup cg = _config; - if ( cg.name().isEmpty()) - cg = KConfigGroup(cg.config(),"RecentFiles"); + KConfigGroup cg = config; + if (cg.name().isEmpty()) { + cg = KConfigGroup(cg.config(), "RecentFiles"); + } - bool thereAreEntries=false; + 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 = KUrl( value ); + 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 = KUrl(value); // Don't restore if file doesn't exist anymore - if (url.isLocalFile() && !QFile::exists(url.toLocalFile())) - continue; + 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; + if (d->m_urls.values().contains(url)) { + continue; + } - - nameKey = QString( "Name%1" ).arg( i ); - nameValue = cg.readPathEntry( nameKey, url.fileName() ); + nameKey = QString("Name%1").arg(i); + nameValue = cg.readPathEntry(nameKey, url.fileName()); title = titleWithSensibleWidth(nameValue, value); - if (!value.isNull()) - { + if (!value.isNull()) { thereAreEntries=true; addAction(new QAction(title, selectableActionGroup()), url, nameValue); } } - if (thereAreEntries) - { + if (thereAreEntries) { d->m_noEntriesAction->setVisible(false); d->clearSeparator->setVisible(true); d->clearAction->setVisible(true); @@ -301,29 +301,29 @@ void KRecentFilesAction::loadEntries( const KConfigGroup& _config) } } -void KRecentFilesAction::saveEntries( const KConfigGroup &_cg ) +void KRecentFilesAction::saveEntries(const KConfigGroup &_cg) { Q_D(KRecentFilesAction); - QString key; - QString value; + QString key; + QString value; QStringList lst = items(); KConfigGroup cg = _cg; - if (cg.name().isEmpty()) - cg = KConfigGroup(cg.config(),"RecentFiles"); + 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 ); + 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 ] ].pathOrUrl(); - cg.writePathEntry( key, value ); - key = QString( "Name%1" ).arg( i ); - value = d->m_shortNames[ selectableActionGroup()->actions()[ i - 1 ] ]; - cg.writePathEntry( key, value ); + value = d->m_urls[ selectableActionGroup()->actions()[i - 1]].pathOrUrl(); + cg.writePathEntry(key, value); + key = QString("Name%1" ).arg(i); + value = d->m_shortNames[selectableActionGroup()->actions()[i - 1]]; + cg.writePathEntry(key, value); } } diff --git a/kdeui/actions/krecentfilesaction.h b/kdeui/actions/krecentfilesaction.h index 7486af8f..e315818d 100644 --- a/kdeui/actions/krecentfilesaction.h +++ b/kdeui/actions/krecentfilesaction.h @@ -45,9 +45,9 @@ class KRecentFilesActionPrivate; */ class KDEUI_EXPORT KRecentFilesAction : public KSelectAction { - Q_OBJECT - Q_PROPERTY( int maxItems READ maxItems WRITE setMaxItems ) - Q_DECLARE_PRIVATE(KRecentFilesAction) + Q_OBJECT + Q_PROPERTY(int maxItems READ maxItems WRITE setMaxItems) + Q_DECLARE_PRIVATE(KRecentFilesAction) public: /** @@ -84,97 +84,97 @@ public: */ KRecentFilesAction(const KIcon &icon, const QString &text, QObject *parent); - /** - * Destructor. - */ - virtual ~KRecentFilesAction(); + /** + * Destructor. + */ + virtual ~KRecentFilesAction(); - /** - * 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 KUrl& url, const QString& name); + /** + * 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 KUrl &url, const QString &name); - /** - * Reimplemented for internal reasons. - */ - virtual QAction* removeAction(QAction* action); + /** + * Reimplemented for internal reasons. + */ + virtual QAction* removeAction(QAction *action); 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(); + /** + * 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; + /** + * 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 ); + /** + * 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 load from a group called 'RecentFiles' - * - */ - void loadEntries( const KConfigGroup &config ); + /** + * 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 load from a group called 'RecentFiles' + * + */ + 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 ); + /** + * 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 KUrl& url, const QString& name = QString() ); + /** + * 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 KUrl &url, const QString &name = QString()); - /** - * Remove an URL from the recent files list. - * - * @param url The URL of the file - */ - void removeUrl( const KUrl& url ); + /** + * Remove an URL from the recent files list. + * + * @param url The URL of the file + */ + void removeUrl(const KUrl &url); - /** - * Retrieve a list of all URLs in the recent files list. - */ - KUrl::List urls() const; + /** + * Retrieve a list of all URLs in the recent files list. + */ + KUrl::List urls() const; Q_SIGNALS: - /** - * This signal gets emitted when the user selects an URL. - * - * @param url The URL thats the user selected. - */ - void urlSelected( const KUrl& url ); + /** + * This signal gets emitted when the user selects an URL. + * + * @param url The URL thats the user selected. + */ + void urlSelected(const KUrl &url); - /** - * This signal gets emitted when the user clear list. - * So when user store url in specific config file it can saveEntry. - * @since 4.3 - */ - void recentListCleared(); + /** + * This signal gets emitted when the user clear list. + * So when user store url in specific config file it can saveEntry. + * @since 4.3 + */ + void recentListCleared(); private: //Internal @@ -183,7 +183,7 @@ private: // addAction() says, addAction( QAction* ) should not be used. using KSelectAction::addAction; - Q_PRIVATE_SLOT( d_func(), void _k_urlSelected(QAction*) ) + Q_PRIVATE_SLOT(d_func(), void _k_urlSelected(QAction*)) }; #endif