]>
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())) {
63 if ((count
== 1) && !selection
.first().url().isEmpty()) {
64 m_urlCandidate
= selection
.first().url();
70 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
72 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
76 if (QApplication::mouseButtons() & Qt::LeftButton
) {
77 // Ignore the request of an item information when a rubberband
78 // selection is ongoing.
85 // The cursor is above the viewport. If files are selected,
86 // show information regarding the selection.
87 if (!m_selection
.isEmpty()) {
88 m_fileItem
= KFileItem();
91 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
92 // The cursor is above an item that is not shown currently
93 m_urlCandidate
= item
.url();
99 bool InformationPanel::urlChanged()
101 if (!url().isValid()) {
112 if (!isEqualToShownUrl(url())) {
114 m_fileItem
= KFileItem();
116 // Update the content with a delay. This gives
117 // the directory lister the chance to show the content
118 // before expensive operations are done to show
120 m_urlChangedTimer
->start();
126 void InformationPanel::showEvent(QShowEvent
* event
)
128 Panel::showEvent(event
);
129 if (!event
->spontaneous()) {
130 if (!m_initialized
) {
131 // do a delayed initialization so that no performance
132 // penalty is given when Dolphin is started with a closed
142 void InformationPanel::resizeEvent(QResizeEvent
* event
)
145 m_urlCandidate
= m_shownUrl
;
146 m_infoTimer
->start();
148 Panel::resizeEvent(event
);
151 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
153 showContextMenu(event
->globalPos());
154 Panel::contextMenuEvent(event
);
157 void InformationPanel::showContextMenu(const QPoint
&pos
)
161 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
162 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
163 previewAction
->setCheckable(true);
164 previewAction
->setChecked(InformationPanelSettings::previewsShown());
166 QAction
* previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
167 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
168 previewAutoPlayAction
->setCheckable(true);
169 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
171 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
172 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
173 if (m_inConfigurationMode
) {
174 configureAction
->setEnabled(false);
177 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
178 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
179 dateformatAction
->setCheckable(true);
180 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
182 popup
.addSeparator();
183 const auto actions
= customContextMenuActions();
184 for (QAction
*action
: actions
) {
185 popup
.addAction(action
);
188 // Open the popup and adjust the settings for the
190 QAction
* action
= popup
.exec(pos
);
195 const bool isChecked
= action
->isChecked();
196 if (action
== previewAction
) {
197 InformationPanelSettings::setPreviewsShown(isChecked
);
198 m_content
->refreshPreview();
199 } else if (action
== configureAction
) {
200 m_inConfigurationMode
= true;
201 m_content
->configureShownProperties();
203 if (action
== dateformatAction
) {
204 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
206 InformationPanelSettings::setDateFormat(dateFormat
);
207 m_content
->refreshMetaData();
208 } else if (action
== previewAutoPlayAction
) {
209 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
210 m_content
->setPreviewAutoPlay(isChecked
);
214 void InformationPanel::showItemInfo()
222 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
223 // The information for a selection of items should be shown
224 m_content
->showItems(m_selection
);
226 // The information for exactly one item should be shown
228 if (!m_fileItem
.isNull()) {
230 } else if (!m_selection
.isEmpty()) {
231 Q_ASSERT(m_selection
.count() == 1);
232 item
= m_selection
.first();
236 // No item is hovered and no selection has been done: provide
237 // an item for the currently shown directory.
238 m_folderStatJob
= KIO::statDetails(url(), 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
);
245 m_shownUrl
= item
.url();
246 m_content
->showItem(item
);
251 void InformationPanel::slotFolderStatFinished(KJob
* job
)
253 m_folderStatJob
= nullptr;
254 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
255 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
258 void InformationPanel::slotInfoTimeout()
260 m_shownUrl
= m_urlCandidate
;
261 m_urlCandidate
.clear();
265 void InformationPanel::reset()
267 if (m_invalidUrlCandidate
== m_shownUrl
) {
268 m_invalidUrlCandidate
= QUrl();
270 // The current URL is still invalid. Reset
271 // the content to show the directory URL.
274 m_fileItem
= KFileItem();
279 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
281 if (m_shownUrl
== QUrl::fromUserInput(source
)) {
282 m_shownUrl
= QUrl::fromUserInput(dest
);
283 m_fileItem
= KFileItem(m_shownUrl
);
285 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
286 m_selection
[0] = m_fileItem
;
287 // Implementation note: Updating the selection is only required if exactly one
288 // item is selected, as the name of the item is shown. If this should change
289 // in future: Before parsing the whole selection take care to test possible
290 // performance bottlenecks when renaming several hundreds of files.
297 void InformationPanel::slotFilesAdded(const QString
& directory
)
299 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
300 // If the 'trash' icon changes because the trash has been emptied or got filled,
301 // the signal filesAdded("trash:/") will be emitted.
302 KFileItem
item(QUrl::fromUserInput(directory
));
303 requestDelayedItemInfo(item
);
307 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
309 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
310 if (!item
.isNull()) {
316 void InformationPanel::slotFilesChanged(const QStringList
& files
)
318 for (const QString
& fileName
: files
) {
319 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
326 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
328 for (const QString
& fileName
: files
) {
329 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
330 // the currently shown item has been removed, show
331 // the parent directory as fallback
338 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
340 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
341 KFileItem
item(QUrl::fromUserInput(directory
));
342 requestDelayedItemInfo(item
);
346 void InformationPanel::slotLeftDirectory(const QString
& directory
)
348 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
349 // The signal 'leftDirectory' is also emitted when a media
350 // has been unmounted. In this case no directory change will be
351 // done in Dolphin, but the Information Panel must be updated to
352 // indicate an invalid directory.
357 void InformationPanel::cancelRequest()
359 delete m_folderStatJob
;
360 m_folderStatJob
= nullptr;
363 m_resetUrlTimer
->stop();
364 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
365 // has the smallest interval (see init()), it is not possible that the exceeded timer
366 // would overwrite an information provided by a selection or hovering.
368 m_invalidUrlCandidate
.clear();
369 m_urlCandidate
.clear();
372 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
374 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
377 void InformationPanel::markUrlAsInvalid()
379 m_invalidUrlCandidate
= m_shownUrl
;
380 m_resetUrlTimer
->start();
383 void InformationPanel::init()
385 m_infoTimer
= new QTimer(this);
386 m_infoTimer
->setInterval(300);
387 m_infoTimer
->setSingleShot(true);
388 connect(m_infoTimer
, &QTimer::timeout
,
389 this, &InformationPanel::slotInfoTimeout
);
391 m_urlChangedTimer
= new QTimer(this);
392 m_urlChangedTimer
->setInterval(200);
393 m_urlChangedTimer
->setSingleShot(true);
394 connect(m_urlChangedTimer
, &QTimer::timeout
,
395 this, &InformationPanel::showItemInfo
);
397 m_resetUrlTimer
= new QTimer(this);
398 m_resetUrlTimer
->setInterval(1000);
399 m_resetUrlTimer
->setSingleShot(true);
400 connect(m_resetUrlTimer
, &QTimer::timeout
,
401 this, &InformationPanel::reset
);
403 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
404 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
406 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
407 QDBusConnection::sessionBus(), this);
408 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
409 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
410 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
411 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
412 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
413 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
415 m_content
= new InformationPanelContent(this);
416 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
417 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
418 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
420 QVBoxLayout
* layout
= new QVBoxLayout(this);
421 layout
->setContentsMargins(0, 0, 0, 0);
422 layout
->addWidget(m_content
);
424 m_initialized
= true;