1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "informationpanel.h"
21 #include <kdirnotify.h>
23 #include "informationpanelcontent.h"
25 InformationPanel::InformationPanel(QWidget
* parent
) :
33 m_invalidUrlCandidate(),
40 InformationPanel::~InformationPanel()
44 QSize
InformationPanel::sizeHint() const
46 QSize size
= Panel::sizeHint();
47 size
.setWidth(minimumSizeHint().width());
51 void InformationPanel::setUrl(const KUrl
& url
)
54 if (url
.isValid() && !isEqualToShownUrl(url
)) {
58 // Update the content with a delay. This gives
59 // the directory lister the chance to show the content
60 // before expensive operations are done to show
62 m_urlChangedTimer
->start();
69 void InformationPanel::setSelection(const KFileItemList
& selection
)
75 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
76 // The selection has not really changed, only the current index.
77 // QItemSelectionModel emits a signal in this case and it is less
78 // expensive doing the check this way instead of patching
79 // DolphinView::emitSelectionChanged().
83 m_selection
= selection
;
85 const int count
= selection
.count();
87 if (!isEqualToShownUrl(url())) {
92 if ((count
== 1) && !selection
.first().url().isEmpty()) {
93 m_urlCandidate
= selection
.first().url();
99 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
107 m_fileItem
= KFileItem();
109 // The cursor is above the viewport. If files are selected,
110 // show information regarding the selection.
111 if (m_selection
.size() > 0) {
112 m_infoTimer
->start();
115 const KUrl url
= item
.url();
116 if (url
.isValid() && !isEqualToShownUrl(url
)) {
117 m_urlCandidate
= item
.url();
119 m_infoTimer
->start();
124 void InformationPanel::showEvent(QShowEvent
* event
)
126 Panel::showEvent(event
);
127 if (!event
->spontaneous()) {
128 if (!m_initialized
) {
129 // do a delayed initialization so that no performance
130 // penalty is given when Dolphin is started with a closed
138 void InformationPanel::resizeEvent(QResizeEvent
* event
)
141 m_urlCandidate
= m_shownUrl
;
142 m_infoTimer
->start();
144 Panel::resizeEvent(event
);
147 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
149 m_content
->configureSettings();
150 Panel::contextMenuEvent(event
);
153 void InformationPanel::showItemInfo()
161 if (showMultipleSelectionInfo()) {
162 m_content
->showItems(m_selection
);
166 if (!m_fileItem
.isNull()) {
168 } else if (!m_selection
.isEmpty()) {
169 Q_ASSERT(m_selection
.count() == 1);
170 item
= m_selection
.first();
172 // no item is hovered and no selection has been done: provide
173 // an item for the directory represented by m_shownUrl
174 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
175 m_fileItem
.refresh();
179 m_content
->showItem(item
);
183 void InformationPanel::slotInfoTimeout()
185 m_shownUrl
= m_urlCandidate
;
189 void InformationPanel::reset()
191 if (m_invalidUrlCandidate
== m_shownUrl
) {
192 m_invalidUrlCandidate
= KUrl();
194 // The current URL is still invalid. Reset
195 // the content to show the directory URL.
198 m_fileItem
= KFileItem();
203 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
205 if (m_shownUrl
== KUrl(source
)) {
206 m_shownUrl
= KUrl(dest
);
207 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
212 void InformationPanel::slotFilesAdded(const QString
& directory
)
214 if (m_shownUrl
== KUrl(directory
)) {
215 // If the 'trash' icon changes because the trash has been emptied or got filled,
216 // the signal filesAdded("trash:/") will be emitted.
217 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
218 requestDelayedItemInfo(item
);
222 void InformationPanel::slotFilesChanged(const QStringList
& files
)
224 foreach (const QString
& fileName
, files
) {
225 if (m_shownUrl
== KUrl(fileName
)) {
232 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
234 foreach (const QString
& fileName
, files
) {
235 if (m_shownUrl
== KUrl(fileName
)) {
236 // the currently shown item has been removed, show
237 // the parent directory as fallback
244 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
246 if (m_shownUrl
== KUrl(directory
)) {
247 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
248 requestDelayedItemInfo(item
);
252 void InformationPanel::slotLeftDirectory(const QString
& directory
)
254 if (m_shownUrl
== KUrl(directory
)) {
255 // The signal 'leftDirectory' is also emitted when a media
256 // has been unmounted. In this case no directory change will be
257 // done in Dolphin, but the Information Panel must be updated to
258 // indicate an invalid directory.
263 void InformationPanel::cancelRequest()
268 bool InformationPanel::showMultipleSelectionInfo() const
270 return m_fileItem
.isNull() && (m_selection
.count() > 1);
273 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
275 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
278 void InformationPanel::markUrlAsInvalid()
280 m_invalidUrlCandidate
= m_shownUrl
;
281 m_resetUrlTimer
->start();
284 void InformationPanel::init()
286 const int defaultDelay
= 300;
288 m_infoTimer
= new QTimer(this);
289 m_infoTimer
->setInterval(defaultDelay
);
290 m_infoTimer
->setSingleShot(true);
291 connect(m_infoTimer
, SIGNAL(timeout()),
292 this, SLOT(slotInfoTimeout()));
294 m_urlChangedTimer
= new QTimer(this);
295 m_urlChangedTimer
->setInterval(defaultDelay
);
296 m_urlChangedTimer
->setSingleShot(true);
297 connect(m_urlChangedTimer
, SIGNAL(timeout()),
298 this, SLOT(showItemInfo()));
300 m_resetUrlTimer
= new QTimer(this);
301 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
302 m_resetUrlTimer
->setSingleShot(true);
303 connect(m_resetUrlTimer
, SIGNAL(timeout()),
304 this, SLOT(reset()));
306 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
307 QDBusConnection::sessionBus(), this);
308 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
309 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
310 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
311 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
312 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
313 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
315 m_content
= new InformationPanelContent(this);
317 m_initialized
= true;
320 #include "informationpanel.moc"