]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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"
22 #include "informationpanelcontent.h"
25 #include <KIO/JobUiDelegate>
26 #include <KJobWidgets>
28 #include <KLocalizedString>
30 #include <Baloo/FileMetaDataWidget>
32 #include <QApplication>
34 #include <QVBoxLayout>
38 #include "dolphin_informationpanelsettings.h"
40 InformationPanel::InformationPanel(QWidget
* parent
) :
44 m_urlChangedTimer(nullptr),
45 m_resetUrlTimer(nullptr),
48 m_invalidUrlCandidate(),
51 m_folderStatJob(nullptr),
56 InformationPanel::~InformationPanel()
60 void InformationPanel::setSelection(const KFileItemList
& selection
)
62 m_selection
= selection
;
63 m_fileItem
= KFileItem();
69 const int count
= selection
.count();
71 if (!isEqualToShownUrl(url())) {
76 if ((count
== 1) && !selection
.first().url().isEmpty()) {
77 m_urlCandidate
= selection
.first().url();
83 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
85 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
89 if (QApplication::mouseButtons() & Qt::LeftButton
) {
90 // Ignore the request of an item information when a rubberband
91 // selection is ongoing.
98 // The cursor is above the viewport. If files are selected,
99 // show information regarding the selection.
100 if (!m_selection
.isEmpty()) {
101 m_fileItem
= KFileItem();
102 m_infoTimer
->start();
104 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
105 // The cursor is above an item that is not shown currently
106 m_urlCandidate
= item
.url();
108 m_infoTimer
->start();
112 bool InformationPanel::urlChanged()
114 if (!url().isValid()) {
125 if (!isEqualToShownUrl(url())) {
127 m_fileItem
= KFileItem();
129 // Update the content with a delay. This gives
130 // the directory lister the chance to show the content
131 // before expensive operations are done to show
133 m_urlChangedTimer
->start();
139 void InformationPanel::showEvent(QShowEvent
* event
)
141 Panel::showEvent(event
);
142 if (!event
->spontaneous()) {
143 if (!m_initialized
) {
144 // do a delayed initialization so that no performance
145 // penalty is given when Dolphin is started with a closed
155 void InformationPanel::resizeEvent(QResizeEvent
* event
)
158 m_urlCandidate
= m_shownUrl
;
159 m_infoTimer
->start();
161 Panel::resizeEvent(event
);
164 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
166 showContextMenu(event
->globalPos());
167 Panel::contextMenuEvent(event
);
170 void InformationPanel::showContextMenu(const QPoint
&pos
)
174 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
175 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
176 previewAction
->setCheckable(true);
177 previewAction
->setChecked(InformationPanelSettings::previewsShown());
179 QAction
* previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
180 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
181 previewAutoPlayAction
->setCheckable(true);
182 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
184 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
185 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
186 if (m_inConfigurationMode
) {
187 configureAction
->setEnabled(false);
190 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
191 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
192 dateformatAction
->setCheckable(true);
193 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
195 popup
.addSeparator();
196 const auto actions
= customContextMenuActions();
197 for (QAction
*action
: actions
) {
198 popup
.addAction(action
);
201 // Open the popup and adjust the settings for the
203 QAction
* action
= popup
.exec(pos
);
208 const bool isChecked
= action
->isChecked();
209 if (action
== previewAction
) {
210 InformationPanelSettings::setPreviewsShown(isChecked
);
211 m_content
->refreshPreview();
212 } else if (action
== configureAction
) {
213 m_inConfigurationMode
= true;
214 m_content
->configureShownProperties();
216 if (action
== dateformatAction
) {
217 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
219 InformationPanelSettings::setDateFormat(dateFormat
);
220 m_content
->refreshMetaData();
221 } else if (action
== previewAutoPlayAction
) {
222 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
223 m_content
->setPreviewAutoPlay(isChecked
);
227 void InformationPanel::showItemInfo()
235 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
236 // The information for a selection of items should be shown
237 m_content
->showItems(m_selection
);
239 // The information for exactly one item should be shown
241 if (!m_fileItem
.isNull()) {
243 } else if (!m_selection
.isEmpty()) {
244 Q_ASSERT(m_selection
.count() == 1);
245 item
= m_selection
.first();
249 // No item is hovered and no selection has been done: provide
250 // an item for the currently shown directory.
251 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
252 if (m_folderStatJob
->uiDelegate()) {
253 KJobWidgets::setWindow(m_folderStatJob
, this);
255 connect(m_folderStatJob
, &KIO::Job::result
,
256 this, &InformationPanel::slotFolderStatFinished
);
258 m_content
->showItem(item
);
263 void InformationPanel::slotFolderStatFinished(KJob
* job
)
265 m_folderStatJob
= nullptr;
266 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
267 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
270 void InformationPanel::slotInfoTimeout()
272 m_shownUrl
= m_urlCandidate
;
273 m_urlCandidate
.clear();
277 void InformationPanel::reset()
279 if (m_invalidUrlCandidate
== m_shownUrl
) {
280 m_invalidUrlCandidate
= QUrl();
282 // The current URL is still invalid. Reset
283 // the content to show the directory URL.
286 m_fileItem
= KFileItem();
291 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
293 if (m_shownUrl
== QUrl::fromUserInput(source
)) {
294 m_shownUrl
= QUrl::fromUserInput(dest
);
295 m_fileItem
= KFileItem(m_shownUrl
);
297 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
298 m_selection
[0] = m_fileItem
;
299 // Implementation note: Updating the selection is only required if exactly one
300 // item is selected, as the name of the item is shown. If this should change
301 // in future: Before parsing the whole selection take care to test possible
302 // performance bottlenecks when renaming several hundreds of files.
309 void InformationPanel::slotFilesAdded(const QString
& directory
)
311 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
312 // If the 'trash' icon changes because the trash has been emptied or got filled,
313 // the signal filesAdded("trash:/") will be emitted.
314 KFileItem
item(QUrl::fromUserInput(directory
));
315 requestDelayedItemInfo(item
);
319 void InformationPanel::slotFilesChanged(const QStringList
& files
)
321 for (const QString
& fileName
: files
) {
322 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
329 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
331 for (const QString
& fileName
: files
) {
332 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
333 // the currently shown item has been removed, show
334 // the parent directory as fallback
341 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
343 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
344 KFileItem
item(QUrl::fromUserInput(directory
));
345 requestDelayedItemInfo(item
);
349 void InformationPanel::slotLeftDirectory(const QString
& directory
)
351 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
352 // The signal 'leftDirectory' is also emitted when a media
353 // has been unmounted. In this case no directory change will be
354 // done in Dolphin, but the Information Panel must be updated to
355 // indicate an invalid directory.
360 void InformationPanel::cancelRequest()
362 delete m_folderStatJob
;
363 m_folderStatJob
= nullptr;
366 m_resetUrlTimer
->stop();
367 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
368 // has the smallest interval (see init()), it is not possible that the exceeded timer
369 // would overwrite an information provided by a selection or hovering.
371 m_invalidUrlCandidate
.clear();
372 m_urlCandidate
.clear();
375 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
377 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
380 void InformationPanel::markUrlAsInvalid()
382 m_invalidUrlCandidate
= m_shownUrl
;
383 m_resetUrlTimer
->start();
386 void InformationPanel::init()
388 m_infoTimer
= new QTimer(this);
389 m_infoTimer
->setInterval(300);
390 m_infoTimer
->setSingleShot(true);
391 connect(m_infoTimer
, &QTimer::timeout
,
392 this, &InformationPanel::slotInfoTimeout
);
394 m_urlChangedTimer
= new QTimer(this);
395 m_urlChangedTimer
->setInterval(200);
396 m_urlChangedTimer
->setSingleShot(true);
397 connect(m_urlChangedTimer
, &QTimer::timeout
,
398 this, &InformationPanel::showItemInfo
);
400 m_resetUrlTimer
= new QTimer(this);
401 m_resetUrlTimer
->setInterval(1000);
402 m_resetUrlTimer
->setSingleShot(true);
403 connect(m_resetUrlTimer
, &QTimer::timeout
,
404 this, &InformationPanel::reset
);
406 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
407 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
409 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
410 QDBusConnection::sessionBus(), this);
411 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
412 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
413 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
414 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
415 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
416 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
418 m_content
= new InformationPanelContent(this);
419 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
420 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
422 QVBoxLayout
* layout
= new QVBoxLayout(this);
423 layout
->setContentsMargins(0, 0, 0, 0);
424 layout
->addWidget(m_content
);
426 m_initialized
= true;