• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • actions
krecentfilesaction.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
3 (C) 1999 Simon Hausmann <hausmann@kde.org>
4 (C) 2000 Nicolas Hadacek <haadcek@kde.org>
5 (C) 2000 Kurt Granroth <granroth@kde.org>
6 (C) 2000 Michael Koch <koch@kde.org>
7 (C) 2001 Holger Freyther <freyther@kde.org>
8 (C) 2002 Ellis Whitehead <ellis@kde.org>
9 (C) 2002 Joseph Wenninger <jowenn@kde.org>
10 (C) 2003 Andras Mantia <amantia@kde.org>
11 (C) 2005-2006 Hamish Rodda <rodda@kde.org>
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Library General Public
15 License version 2 as published by the Free Software Foundation.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Library General Public License for more details.
21
22 You should have received a copy of the GNU Library General Public License
23 along with this library; see the file COPYING.LIB. If not, write to
24 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA.
26*/
27
28#include "krecentfilesaction.h"
29#include "krecentfilesaction_p.h"
30
31#include <QtCore/QFile>
32#include <QtGui/QDesktopWidget>
33#ifdef Q_OS_WIN
34#include <QtCore/QDir>
35#endif
36
37#include <kconfig.h>
38#include <kconfiggroup.h>
39#include <kdebug.h>
40#include <kicon.h>
41#include <klocale.h>
42#include <kstandarddirs.h>
43
44#include "kmenu.h"
45
46
47KRecentFilesAction::KRecentFilesAction(QObject *parent)
48 : KSelectAction(*new KRecentFilesActionPrivate, parent)
49{
50 Q_D(KRecentFilesAction);
51 d->init();
52}
53
54KRecentFilesAction::KRecentFilesAction(const QString &text, QObject *parent)
55 : KSelectAction(*new KRecentFilesActionPrivate, parent)
56{
57 Q_D(KRecentFilesAction);
58 d->init();
59
60 // Want to keep the ampersands
61 setText(text);
62}
63
64KRecentFilesAction::KRecentFilesAction(const KIcon &icon, const QString &text, QObject *parent)
65 : KSelectAction(*new KRecentFilesActionPrivate, parent)
66{
67 Q_D(KRecentFilesAction);
68 d->init();
69
70 setIcon(icon);
71 // Want to keep the ampersands
72 setText(text);
73}
74
75void KRecentFilesActionPrivate::init()
76{
77 Q_Q(KRecentFilesAction);
78 delete q->menu();
79 q->setMenu(new KMenu());
80 q->setToolBarMode(KSelectAction::MenuMode);
81 m_noEntriesAction = q->menu()->addAction(i18n("No Entries"));
82 m_noEntriesAction->setEnabled(false);
83 clearSeparator = q->menu()->addSeparator();
84 clearSeparator->setVisible(false);
85 clearAction = q->menu()->addAction(i18n("Clear List"), q, SLOT(clear()));
86 clearAction->setVisible(false);
87 q->setEnabled(false);
88 q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*)));
89}
90
91KRecentFilesAction::~KRecentFilesAction()
92{
93}
94
95void KRecentFilesActionPrivate::_k_urlSelected( QAction* action )
96{
97 Q_Q(KRecentFilesAction);
98 emit q->urlSelected(m_urls[action]);
99}
100
101int KRecentFilesAction::maxItems() const
102{
103 Q_D(const KRecentFilesAction);
104 return d->m_maxItems;
105}
106
107void KRecentFilesAction::setMaxItems( int maxItems )
108{
109 Q_D(KRecentFilesAction);
110 // set new maxItems
111 d->m_maxItems = maxItems;
112
113 // remove all excess items
114 while( selectableActionGroup()->actions().count() > maxItems )
115 delete removeAction(selectableActionGroup()->actions().last());
116}
117
118static QString titleWithSensibleWidth(const QString& nameValue, const QString& value)
119{
120 // Calculate 3/4 of screen geometry, we do not want
121 // action titles to be bigger than that
122 // Since we do not know in which screen we are going to show
123 // we choose the min of all the screens
124 const QDesktopWidget desktopWidget;
125 int maxWidthForTitles = INT_MAX;
126 for (int i = 0; i < desktopWidget.screenCount(); ++i) {
127 maxWidthForTitles = qMin(maxWidthForTitles, desktopWidget.availableGeometry(i).width() * 3 / 4);
128 }
129 const QFontMetrics fontMetrics = QFontMetrics(QFont());
130
131 QString title = nameValue + " [" + value + ']';
132 if (fontMetrics.width(title) > maxWidthForTitles){
133 // If it does not fit, try to cut only the whole path, though if the
134 // name is too long (more than 3/4 of the whole text) we cut it a bit too
135 const int nameValueMaxWidth = maxWidthForTitles * 3 / 4;
136 const int nameWidth = fontMetrics.width(nameValue);
137 QString cutNameValue, cutValue;
138 if (nameWidth > nameValueMaxWidth) {
139 cutNameValue = fontMetrics.elidedText(nameValue, Qt::ElideMiddle, nameValueMaxWidth);
140 cutValue = fontMetrics.elidedText(value, Qt::ElideMiddle, maxWidthForTitles - nameValueMaxWidth);
141 } else {
142 cutNameValue = nameValue;
143 cutValue = fontMetrics.elidedText(value, Qt::ElideMiddle, maxWidthForTitles - nameWidth);
144 }
145 title = cutNameValue + " [" + cutValue + ']';
146 }
147 return title;
148}
149
150void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name )
151{
152 Q_D(KRecentFilesAction);
158 const KUrl url( _url );
159
160 if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation("tmp", url.toLocalFile()) != url.toLocalFile() )
161 return;
162 const QString tmpName = name.isEmpty() ? url.fileName() : name;
163#ifdef Q_OS_WIN
164 const QString file = url.isLocalFile() ? QDir::toNativeSeparators( url.pathOrUrl() ) : url.pathOrUrl();
165#else
166 const QString file = url.pathOrUrl();
167#endif
168
169 // remove file if already in list
170 foreach (QAction* action, selectableActionGroup()->actions())
171 {
172#ifdef Q_OS_WIN
173 const QString tmpFileName = url.isLocalFile() ? QDir::toNativeSeparators( d->m_urls[action].pathOrUrl() ) : d->m_urls[action].pathOrUrl();
174 if ( tmpFileName.endsWith(file, Qt::CaseInsensitive) )
175#else
176 if ( d->m_urls[action].pathOrUrl().endsWith(file) )
177#endif
178 {
179 removeAction(action)->deleteLater();
180 break;
181 }
182 }
183 // remove oldest item if already maxitems in list
184 if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems )
185 {
186 // remove oldest added item
187 delete removeAction(selectableActionGroup()->actions().first());
188 }
189
190 d->m_noEntriesAction->setVisible(false);
191 d->clearSeparator->setVisible(true);
192 d->clearAction->setVisible(true);
193 setEnabled(true);
194 // add file to list
195 const QString title = titleWithSensibleWidth(tmpName, file);
196 QAction* action = new QAction(title, selectableActionGroup());
197 addAction(action, url, tmpName);
198}
199
200void KRecentFilesAction::addAction(QAction* action, const KUrl& url, const QString& name)
201{
202 Q_D(KRecentFilesAction);
203 //kDebug (129) << "KRecentFilesAction::addAction(" << action << ")";
204
205 action->setActionGroup(selectableActionGroup());
206
207 // Keep in sync with createToolBarWidget()
208 foreach (QToolButton* button, d->m_buttons)
209 button->insertAction(button->actions().value(0), action);
210
211 foreach (KComboBox* comboBox, d->m_comboBoxes)
212 comboBox->insertAction(comboBox->actions().value(0), action);
213
214 menu()->insertAction(menu()->actions().value(0), action);
215
216 d->m_shortNames.insert( action, name );
217 d->m_urls.insert( action, url );
218}
219
220QAction* KRecentFilesAction::removeAction(QAction* action)
221{
222 Q_D(KRecentFilesAction);
223 KSelectAction::removeAction( action );
224
225 d->m_shortNames.remove( action );
226 d->m_urls.remove( action );
227
228 return action;
229}
230
231void KRecentFilesAction::removeUrl( const KUrl& url )
232{
233 Q_D(KRecentFilesAction);
234 for (QMap<QAction*, KUrl>::ConstIterator it = d->m_urls.constBegin(); it != d->m_urls.constEnd(); ++it)
235 if (it.value() == url) {
236 delete removeAction(it.key());
237 return;
238 }
239}
240
241KUrl::List KRecentFilesAction::urls() const
242{
243 Q_D(const KRecentFilesAction);
244 return d->m_urls.values ();
245}
246
247void KRecentFilesAction::clear()
248{
249 clearEntries();
250 emit recentListCleared();
251}
252
253void KRecentFilesAction::clearEntries()
254{
255 Q_D(KRecentFilesAction);
256 KSelectAction::clear();
257 d->m_shortNames.clear();
258 d->m_urls.clear();
259 d->m_noEntriesAction->setVisible(true);
260 d->clearSeparator->setVisible(false);
261 d->clearAction->setVisible(false);
262 setEnabled(false);
263}
264
265void KRecentFilesAction::loadEntries( const KConfigGroup& _config)
266{
267 Q_D(KRecentFilesAction);
268 clearEntries();
269
270 QString key;
271 QString value;
272 QString nameKey;
273 QString nameValue;
274 QString title;
275 KUrl url;
276
277 KConfigGroup cg = _config;
278 if ( cg.name().isEmpty())
279 cg = KConfigGroup(cg.config(),"RecentFiles");
280
281 bool thereAreEntries=false;
282 // read file list
283 for( int i = 1 ; i <= d->m_maxItems ; i++ )
284 {
285 key = QString( "File%1" ).arg( i );
286 value = cg.readPathEntry( key, QString() );
287 if (value.isEmpty()) continue;
288 url = KUrl( value );
289
290 // Don't restore if file doesn't exist anymore
291 if (url.isLocalFile() && !QFile::exists(url.toLocalFile()))
292 continue;
293
294 // Don't restore where the url is already known (eg. broken config)
295 if (d->m_urls.values().contains(url))
296 continue;
297
298#ifdef Q_OS_WIN
299 // convert to backslashes
300 if ( url.isLocalFile() )
301 value = QDir::toNativeSeparators( value );
302#endif
303
304 nameKey = QString( "Name%1" ).arg( i );
305 nameValue = cg.readPathEntry( nameKey, url.fileName() );
306 title = titleWithSensibleWidth(nameValue, value);
307 if (!value.isNull())
308 {
309 thereAreEntries=true;
310 addAction(new QAction(title, selectableActionGroup()), url, nameValue);
311 }
312 }
313 if (thereAreEntries)
314 {
315 d->m_noEntriesAction->setVisible(false);
316 d->clearSeparator->setVisible(true);
317 d->clearAction->setVisible(true);
318 setEnabled(true);
319 }
320}
321
322void KRecentFilesAction::saveEntries( const KConfigGroup &_cg )
323{
324 Q_D(KRecentFilesAction);
325 QString key;
326 QString value;
327 QStringList lst = items();
328
329 KConfigGroup cg = _cg;
330 if (cg.name().isEmpty())
331 cg = KConfigGroup(cg.config(),"RecentFiles");
332
333 cg.deleteGroup();
334
335 // write file list
336 for ( int i = 1 ; i <= selectableActionGroup()->actions().count() ; i++ )
337 {
338 key = QString( "File%1" ).arg( i );
339 // i - 1 because we started from 1
340 value = d->m_urls[ selectableActionGroup()->actions()[ i - 1 ] ].pathOrUrl();
341 cg.writePathEntry( key, value );
342 key = QString( "Name%1" ).arg( i );
343 value = d->m_shortNames[ selectableActionGroup()->actions()[ i - 1 ] ];
344 cg.writePathEntry( key, value );
345 }
346
347}
348
349/* vim: et sw=2 ts=2
350 */
351
352#include "krecentfilesaction.moc"
KComboBox
An enhanced combo box.
Definition: kcombobox.h:149
KConfigGroup
KConfigGroup::name
QString name() const
KConfigGroup::readPathEntry
QString readPathEntry(const char *key, const QString &aDefault) const
KConfigGroup::deleteGroup
void deleteGroup(const char *group, WriteConfigFlags flags=Normal)
KConfigGroup::config
KConfig * config()
KConfigGroup::writePathEntry
void writePathEntry(const char *pKey, const QString &path, WriteConfigFlags pFlags=Normal)
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KMenu
A menu with keyboard searching.
Definition: kmenu.h:42
KRecentFilesAction
Recent files action.
Definition: krecentfilesaction.h:47
KRecentFilesAction::addUrl
void addUrl(const KUrl &url, const QString &name=QString())
Add URL to recent files list.
Definition: krecentfilesaction.cpp:150
KRecentFilesAction::addAction
void addAction(QAction *action, const KUrl &url, const QString &name)
Adds action to the list of URLs, with url and title name.
Definition: krecentfilesaction.cpp:200
KRecentFilesAction::~KRecentFilesAction
virtual ~KRecentFilesAction()
Destructor.
Definition: krecentfilesaction.cpp:91
KRecentFilesAction::KRecentFilesAction
KRecentFilesAction(QObject *parent)
Constructs an action with the specified parent.
Definition: krecentfilesaction.cpp:47
KRecentFilesAction::removeAction
virtual QAction * removeAction(QAction *action)
Reimplemented for internal reasons.
Definition: krecentfilesaction.cpp:220
KRecentFilesAction::recentListCleared
void recentListCleared()
This signal gets emitted when the user clear list.
KRecentFilesAction::setMaxItems
void setMaxItems(int maxItems)
Sets the maximum of items in the recent files list.
Definition: krecentfilesaction.cpp:107
KRecentFilesAction::removeUrl
void removeUrl(const KUrl &url)
Remove an URL from the recent files list.
Definition: krecentfilesaction.cpp:231
KRecentFilesAction::maxItems
int maxItems
Definition: krecentfilesaction.h:49
KRecentFilesAction::loadEntries
void loadEntries(const KConfigGroup &config)
Loads the recent files entries from a given KConfigGroup object.
Definition: krecentfilesaction.cpp:265
KRecentFilesAction::urls
KUrl::List urls() const
Retrieve a list of all URLs in the recent files list.
Definition: krecentfilesaction.cpp:241
KRecentFilesAction::saveEntries
void saveEntries(const KConfigGroup &config)
Saves the current recent files entries to a given KConfigGroup object.
Definition: krecentfilesaction.cpp:322
KRecentFilesAction::clear
virtual void clear()
Clears the recent files list.
Definition: krecentfilesaction.cpp:247
KSelectAction
Action for selecting one of several items.
Definition: kselectaction.h:52
KSelectAction::removeAction
virtual QAction * removeAction(QAction *action)
Remove the specified action from this action selector.
Definition: kselectaction.cpp:278
KSelectAction::items
QStringList items
Definition: kselectaction.h:62
KSelectAction::clear
void clear()
Clears up all the items in this action.
Definition: kselectaction.cpp:376
KSelectAction::MenuMode
@ MenuMode
Creates a button which pops up a menu when interacted with, as defined by toolButtonPopupMode().
Definition: kselectaction.h:108
KSelectAction::actions
QList< QAction * > actions() const
Returns the list of selectable actions.
Definition: kselectaction.cpp:110
KSelectAction::selectableActionGroup
QActionGroup * selectableActionGroup() const
The action group used to create exclusivity between the actions associated with this action.
Definition: kselectaction.cpp:104
KStandardDirs::relativeLocation
QString relativeLocation(const char *type, const QString &absPath)
KUrl::List
KUrl
KUrl::pathOrUrl
QString pathOrUrl() const
KUrl::isLocalFile
bool isLocalFile() const
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QAction
QMap
QObject
QToolButton
kconfig.h
kconfiggroup.h
kdebug.h
kicon.h
klocale.h
i18n
QString i18n(const char *text)
kmenu.h
titleWithSensibleWidth
static QString titleWithSensibleWidth(const QString &nameValue, const QString &value)
Definition: krecentfilesaction.cpp:118
krecentfilesaction.h
kstandarddirs.h
KGlobal::dirs
KStandardDirs * dirs()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal