]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
5420cece899f31b5e69727f323aa064b33a882d9
2 * SPDX-FileCopyrightText: 2006-2009 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "informationpanel.h"
9 #include "informationpanelcontent.h"
12 #include <KIO/JobUiDelegate>
13 #include <KJobWidgets>
15 #include <KLocalizedString>
17 #include <Baloo/FileMetaDataWidget>
19 #include <QApplication>
21 #include <QVBoxLayout>
25 #include "dolphin_informationpanelsettings.h"
27 InformationPanel::InformationPanel(QWidget
* parent
) :
31 m_urlChangedTimer(nullptr),
32 m_resetUrlTimer(nullptr),
35 m_invalidUrlCandidate(),
38 m_folderStatJob(nullptr),
43 InformationPanel::~InformationPanel()
47 void InformationPanel::setSelection(const KFileItemList
& selection
)
49 m_selection
= selection
;
55 const int count
= selection
.count();
57 if (!isEqualToShownUrl(url())) {
63 if ((count
== 1) && !selection
.first().url().isEmpty()) {
64 m_urlCandidate
= selection
.first().url();
70 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
76 if (QApplication::mouseButtons() & Qt::LeftButton
) {
77 // Ignore the request of an item information when a rubberband
78 // selection is ongoing.
83 m_hoveredItem
= KFileItem();
93 bool InformationPanel::urlChanged()
95 if (!url().isValid()) {
106 if (!isEqualToShownUrl(url())) {
109 // Update the content with a delay. This gives
110 // the directory lister the chance to show the content
111 // before expensive operations are done to show
113 m_urlChangedTimer
->start();
119 void InformationPanel::showEvent(QShowEvent
* event
)
121 Panel::showEvent(event
);
122 if (!event
->spontaneous()) {
123 if (!m_initialized
) {
124 // do a delayed initialization so that no performance
125 // penalty is given when Dolphin is started with a closed
135 void InformationPanel::resizeEvent(QResizeEvent
* event
)
138 m_urlCandidate
= m_shownUrl
;
139 m_infoTimer
->start();
141 Panel::resizeEvent(event
);
144 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
146 showContextMenu(event
->globalPos());
147 Panel::contextMenuEvent(event
);
150 void InformationPanel::showContextMenu(const QPoint
&pos
)
154 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
155 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
156 previewAction
->setCheckable(true);
157 previewAction
->setChecked(InformationPanelSettings::previewsShown());
159 QAction
* previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
160 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
161 previewAutoPlayAction
->setCheckable(true);
162 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
164 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
165 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
166 if (m_inConfigurationMode
) {
167 configureAction
->setEnabled(false);
170 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
171 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
172 dateformatAction
->setCheckable(true);
173 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
175 popup
.addSeparator();
176 const auto actions
= customContextMenuActions();
177 for (QAction
*action
: actions
) {
178 popup
.addAction(action
);
181 // Open the popup and adjust the settings for the
183 QAction
* action
= popup
.exec(pos
);
188 const bool isChecked
= action
->isChecked();
189 if (action
== previewAction
) {
190 InformationPanelSettings::setPreviewsShown(isChecked
);
191 m_content
->refreshPreview();
192 } else if (action
== configureAction
) {
193 m_inConfigurationMode
= true;
194 m_content
->configureShownProperties();
196 if (action
== dateformatAction
) {
197 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
199 InformationPanelSettings::setDateFormat(dateFormat
);
200 m_content
->refreshMetaData();
201 } else if (action
== previewAutoPlayAction
) {
202 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
203 m_content
->setPreviewAutoPlay(isChecked
);
207 void InformationPanel::showItemInfo()
214 //qDebug() << "showItemInfo" << m_fileItem;
216 if (m_hoveredItem
.isNull() && (m_selection
.count() > 1)) {
217 // The information for a selection of items should be shown
218 m_content
->showItems(m_selection
);
220 // The information for exactly one item should be shown
222 if (!m_hoveredItem
.isNull()) {
223 item
= m_hoveredItem
;
224 } else if (!m_selection
.isEmpty()) {
225 Q_ASSERT(m_selection
.count() == 1);
226 item
= m_selection
.first();
229 if (!item
.isNull()) {
230 m_shownUrl
= item
.url();
231 m_content
->showItem(item
);
235 // No item is hovered and no selection has been done: provide
236 // an item for the currently shown directory.
238 m_folderStatJob
= KIO::statDetails(m_shownUrl
, KIO::StatJob::SourceSide
, KIO::StatDefaultDetails
| KIO::StatRecursiveSize
, KIO::HideProgressInfo
);
239 if (m_folderStatJob
->uiDelegate()) {
240 KJobWidgets::setWindow(m_folderStatJob
, this);
242 connect(m_folderStatJob
, &KIO::Job::result
,
243 this, &InformationPanel::slotFolderStatFinished
);
247 void InformationPanel::slotFolderStatFinished(KJob
* job
)
249 m_folderStatJob
= nullptr;
250 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
251 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
254 void InformationPanel::slotInfoTimeout()
256 m_shownUrl
= m_urlCandidate
;
257 m_urlCandidate
.clear();
261 void InformationPanel::reset()
263 if (m_invalidUrlCandidate
== m_shownUrl
) {
264 m_invalidUrlCandidate
= QUrl();
266 // The current URL is still invalid. Reset
267 // the content to show the directory URL.
274 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
276 auto sourceUrl
= QUrl::fromUserInput(source
);
277 if (m_shownUrl
== sourceUrl
) {
278 auto destUrl
= QUrl::fromUserInput(dest
);
280 if ((m_selection
.count() == 1) && (m_selection
[0].url() == sourceUrl
)) {
281 m_selection
[0] = KFileItem(destUrl
);
282 // Implementation note: Updating the selection is only required if exactly one
283 // item is selected, as the name of the item is shown. If this should change
284 // in future: Before parsing the whole selection take care to test possible
285 // performance bottlenecks when renaming several hundreds of files.
287 m_hoveredItem
= KFileItem(destUrl
);
294 void InformationPanel::slotFilesAdded(const QString
& directory
)
296 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
297 // If the 'trash' icon changes because the trash has been emptied or got filled,
298 // the signal filesAdded("trash:/") will be emitted.
299 requestDelayedItemInfo(KFileItem());
303 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
305 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
306 if (!item
.isNull()) {
311 void InformationPanel::slotFilesChanged(const QStringList
& files
)
313 for (const QString
& fileName
: files
) {
314 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
321 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
323 for (const QString
& fileName
: files
) {
324 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
325 // the currently shown item has been removed, show
326 // the parent directory as fallback
333 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
338 void InformationPanel::slotLeftDirectory(const QString
& directory
)
340 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
341 // The signal 'leftDirectory' is also emitted when a media
342 // has been unmounted. In this case no directory change will be
343 // done in Dolphin, but the Information Panel must be updated to
344 // indicate an invalid directory.
349 void InformationPanel::cancelRequest()
351 delete m_folderStatJob
;
352 m_folderStatJob
= nullptr;
355 m_resetUrlTimer
->stop();
356 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
357 // has the smallest interval (see init()), it is not possible that the exceeded timer
358 // would overwrite an information provided by a selection or hovering.
360 m_invalidUrlCandidate
.clear();
361 m_urlCandidate
.clear();
364 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
366 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
369 void InformationPanel::markUrlAsInvalid()
371 m_invalidUrlCandidate
= m_shownUrl
;
372 m_resetUrlTimer
->start();
375 void InformationPanel::init()
377 m_infoTimer
= new QTimer(this);
378 m_infoTimer
->setInterval(300);
379 m_infoTimer
->setSingleShot(true);
380 connect(m_infoTimer
, &QTimer::timeout
,
381 this, &InformationPanel::slotInfoTimeout
);
383 m_urlChangedTimer
= new QTimer(this);
384 m_urlChangedTimer
->setInterval(200);
385 m_urlChangedTimer
->setSingleShot(true);
386 connect(m_urlChangedTimer
, &QTimer::timeout
,
387 this, &InformationPanel::showItemInfo
);
389 m_resetUrlTimer
= new QTimer(this);
390 m_resetUrlTimer
->setInterval(1000);
391 m_resetUrlTimer
->setSingleShot(true);
392 connect(m_resetUrlTimer
, &QTimer::timeout
,
393 this, &InformationPanel::reset
);
395 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
396 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
398 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
399 QDBusConnection::sessionBus(), this);
400 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
401 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
402 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
403 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
404 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
405 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
407 m_content
= new InformationPanelContent(this);
408 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
409 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
410 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
412 QVBoxLayout
* layout
= new QVBoxLayout(this);
413 layout
->setContentsMargins(0, 0, 0, 0);
414 layout
->addWidget(m_content
);
416 m_initialized
= true;