]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
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
;
50 m_fileItem
= KFileItem();
56 const int count
= selection
.count();
58 if (!isEqualToShownUrl(url())) {
64 if ((count
== 1) && !selection
.first().url().isEmpty()) {
65 m_urlCandidate
= selection
.first().url();
71 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
73 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
77 if (QApplication::mouseButtons() & Qt::LeftButton
) {
78 // Ignore the request of an item information when a rubberband
79 // selection is ongoing.
86 // The cursor is above the viewport. If files are selected,
87 // show information regarding the selection.
88 if (!m_selection
.isEmpty()) {
89 m_fileItem
= KFileItem();
92 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
93 // The cursor is above an item that is not shown currently
94 m_urlCandidate
= item
.url();
100 bool InformationPanel::urlChanged()
102 if (!url().isValid()) {
113 if (!isEqualToShownUrl(url())) {
115 m_fileItem
= KFileItem();
117 // Update the content with a delay. This gives
118 // the directory lister the chance to show the content
119 // before expensive operations are done to show
121 m_urlChangedTimer
->start();
127 void InformationPanel::showEvent(QShowEvent
* event
)
129 Panel::showEvent(event
);
130 if (!event
->spontaneous()) {
131 if (!m_initialized
) {
132 // do a delayed initialization so that no performance
133 // penalty is given when Dolphin is started with a closed
143 void InformationPanel::resizeEvent(QResizeEvent
* event
)
146 m_urlCandidate
= m_shownUrl
;
147 m_infoTimer
->start();
149 Panel::resizeEvent(event
);
152 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
154 showContextMenu(event
->globalPos());
155 Panel::contextMenuEvent(event
);
158 void InformationPanel::showContextMenu(const QPoint
&pos
)
162 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
163 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
164 previewAction
->setCheckable(true);
165 previewAction
->setChecked(InformationPanelSettings::previewsShown());
167 QAction
* previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
168 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
169 previewAutoPlayAction
->setCheckable(true);
170 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
172 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
173 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
174 if (m_inConfigurationMode
) {
175 configureAction
->setEnabled(false);
178 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
179 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
180 dateformatAction
->setCheckable(true);
181 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
183 popup
.addSeparator();
184 const auto actions
= customContextMenuActions();
185 for (QAction
*action
: actions
) {
186 popup
.addAction(action
);
189 // Open the popup and adjust the settings for the
191 QAction
* action
= popup
.exec(pos
);
196 const bool isChecked
= action
->isChecked();
197 if (action
== previewAction
) {
198 InformationPanelSettings::setPreviewsShown(isChecked
);
199 m_content
->refreshPreview();
200 } else if (action
== configureAction
) {
201 m_inConfigurationMode
= true;
202 m_content
->configureShownProperties();
204 if (action
== dateformatAction
) {
205 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
207 InformationPanelSettings::setDateFormat(dateFormat
);
208 m_content
->refreshMetaData();
209 } else if (action
== previewAutoPlayAction
) {
210 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
211 m_content
->setPreviewAutoPlay(isChecked
);
215 void InformationPanel::showItemInfo()
223 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
224 // The information for a selection of items should be shown
225 m_content
->showItems(m_selection
);
227 // The information for exactly one item should be shown
229 if (!m_fileItem
.isNull()) {
231 } else if (!m_selection
.isEmpty()) {
232 Q_ASSERT(m_selection
.count() == 1);
233 item
= m_selection
.first();
237 // No item is hovered and no selection has been done: provide
238 // an item for the currently shown directory.
240 m_folderStatJob
= KIO::statDetails(url(), KIO::StatJob::SourceSide
, KIO::StatDefaultDetails
| KIO::StatRecursiveSize
, KIO::HideProgressInfo
);
241 if (m_folderStatJob
->uiDelegate()) {
242 KJobWidgets::setWindow(m_folderStatJob
, this);
244 connect(m_folderStatJob
, &KIO::Job::result
,
245 this, &InformationPanel::slotFolderStatFinished
);
247 m_shownUrl
= item
.url();
248 m_content
->showItem(item
);
253 void InformationPanel::slotFolderStatFinished(KJob
* job
)
255 m_folderStatJob
= nullptr;
256 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
257 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
260 void InformationPanel::slotInfoTimeout()
262 m_shownUrl
= m_urlCandidate
;
263 m_urlCandidate
.clear();
267 void InformationPanel::reset()
269 if (m_invalidUrlCandidate
== m_shownUrl
) {
270 m_invalidUrlCandidate
= QUrl();
272 // The current URL is still invalid. Reset
273 // the content to show the directory URL.
276 m_fileItem
= KFileItem();
281 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
283 if (m_shownUrl
== QUrl::fromUserInput(source
)) {
284 m_shownUrl
= QUrl::fromUserInput(dest
);
285 m_fileItem
= KFileItem(m_shownUrl
);
287 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
288 m_selection
[0] = m_fileItem
;
289 // Implementation note: Updating the selection is only required if exactly one
290 // item is selected, as the name of the item is shown. If this should change
291 // in future: Before parsing the whole selection take care to test possible
292 // performance bottlenecks when renaming several hundreds of files.
299 void InformationPanel::slotFilesAdded(const QString
& directory
)
301 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
302 // If the 'trash' icon changes because the trash has been emptied or got filled,
303 // the signal filesAdded("trash:/") will be emitted.
304 KFileItem
item(QUrl::fromUserInput(directory
));
305 requestDelayedItemInfo(item
);
309 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
311 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
312 if (!item
.isNull()) {
318 void InformationPanel::slotFilesChanged(const QStringList
& files
)
320 for (const QString
& fileName
: files
) {
321 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
328 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
330 for (const QString
& fileName
: files
) {
331 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
332 // the currently shown item has been removed, show
333 // the parent directory as fallback
340 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
342 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
343 KFileItem
item(QUrl::fromUserInput(directory
));
344 requestDelayedItemInfo(item
);
348 void InformationPanel::slotLeftDirectory(const QString
& directory
)
350 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
351 // The signal 'leftDirectory' is also emitted when a media
352 // has been unmounted. In this case no directory change will be
353 // done in Dolphin, but the Information Panel must be updated to
354 // indicate an invalid directory.
359 void InformationPanel::cancelRequest()
361 delete m_folderStatJob
;
362 m_folderStatJob
= nullptr;
365 m_resetUrlTimer
->stop();
366 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
367 // has the smallest interval (see init()), it is not possible that the exceeded timer
368 // would overwrite an information provided by a selection or hovering.
370 m_invalidUrlCandidate
.clear();
371 m_urlCandidate
.clear();
374 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
376 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
379 void InformationPanel::markUrlAsInvalid()
381 m_invalidUrlCandidate
= m_shownUrl
;
382 m_resetUrlTimer
->start();
385 void InformationPanel::init()
387 m_infoTimer
= new QTimer(this);
388 m_infoTimer
->setInterval(300);
389 m_infoTimer
->setSingleShot(true);
390 connect(m_infoTimer
, &QTimer::timeout
,
391 this, &InformationPanel::slotInfoTimeout
);
393 m_urlChangedTimer
= new QTimer(this);
394 m_urlChangedTimer
->setInterval(200);
395 m_urlChangedTimer
->setSingleShot(true);
396 connect(m_urlChangedTimer
, &QTimer::timeout
,
397 this, &InformationPanel::showItemInfo
);
399 m_resetUrlTimer
= new QTimer(this);
400 m_resetUrlTimer
->setInterval(1000);
401 m_resetUrlTimer
->setSingleShot(true);
402 connect(m_resetUrlTimer
, &QTimer::timeout
,
403 this, &InformationPanel::reset
);
405 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
406 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
408 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
409 QDBusConnection::sessionBus(), this);
410 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
411 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
412 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
413 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
414 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
415 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
417 m_content
= new InformationPanelContent(this);
418 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
419 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
420 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
422 QVBoxLayout
* layout
= new QVBoxLayout(this);
423 layout
->setContentsMargins(0, 0, 0, 0);
424 layout
->addWidget(m_content
);
426 m_initialized
= true;