]>
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_content
->showItem(item
);
250 void InformationPanel::slotFolderStatFinished(KJob
* job
)
252 m_folderStatJob
= nullptr;
253 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
254 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
257 void InformationPanel::slotInfoTimeout()
259 m_shownUrl
= m_urlCandidate
;
260 m_urlCandidate
.clear();
264 void InformationPanel::reset()
266 if (m_invalidUrlCandidate
== m_shownUrl
) {
267 m_invalidUrlCandidate
= QUrl();
269 // The current URL is still invalid. Reset
270 // the content to show the directory URL.
273 m_fileItem
= KFileItem();
278 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
280 if (m_shownUrl
== QUrl::fromUserInput(source
)) {
281 m_shownUrl
= QUrl::fromUserInput(dest
);
282 m_fileItem
= KFileItem(m_shownUrl
);
284 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
285 m_selection
[0] = m_fileItem
;
286 // Implementation note: Updating the selection is only required if exactly one
287 // item is selected, as the name of the item is shown. If this should change
288 // in future: Before parsing the whole selection take care to test possible
289 // performance bottlenecks when renaming several hundreds of files.
296 void InformationPanel::slotFilesAdded(const QString
& directory
)
298 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
299 // If the 'trash' icon changes because the trash has been emptied or got filled,
300 // the signal filesAdded("trash:/") will be emitted.
301 KFileItem
item(QUrl::fromUserInput(directory
));
302 requestDelayedItemInfo(item
);
306 void InformationPanel::slotFilesChanged(const QStringList
& files
)
308 for (const QString
& fileName
: files
) {
309 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
316 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
318 for (const QString
& fileName
: files
) {
319 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
320 // the currently shown item has been removed, show
321 // the parent directory as fallback
328 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
330 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
331 KFileItem
item(QUrl::fromUserInput(directory
));
332 requestDelayedItemInfo(item
);
336 void InformationPanel::slotLeftDirectory(const QString
& directory
)
338 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
339 // The signal 'leftDirectory' is also emitted when a media
340 // has been unmounted. In this case no directory change will be
341 // done in Dolphin, but the Information Panel must be updated to
342 // indicate an invalid directory.
347 void InformationPanel::cancelRequest()
349 delete m_folderStatJob
;
350 m_folderStatJob
= nullptr;
353 m_resetUrlTimer
->stop();
354 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
355 // has the smallest interval (see init()), it is not possible that the exceeded timer
356 // would overwrite an information provided by a selection or hovering.
358 m_invalidUrlCandidate
.clear();
359 m_urlCandidate
.clear();
362 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
364 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
367 void InformationPanel::markUrlAsInvalid()
369 m_invalidUrlCandidate
= m_shownUrl
;
370 m_resetUrlTimer
->start();
373 void InformationPanel::init()
375 m_infoTimer
= new QTimer(this);
376 m_infoTimer
->setInterval(300);
377 m_infoTimer
->setSingleShot(true);
378 connect(m_infoTimer
, &QTimer::timeout
,
379 this, &InformationPanel::slotInfoTimeout
);
381 m_urlChangedTimer
= new QTimer(this);
382 m_urlChangedTimer
->setInterval(200);
383 m_urlChangedTimer
->setSingleShot(true);
384 connect(m_urlChangedTimer
, &QTimer::timeout
,
385 this, &InformationPanel::showItemInfo
);
387 m_resetUrlTimer
= new QTimer(this);
388 m_resetUrlTimer
->setInterval(1000);
389 m_resetUrlTimer
->setSingleShot(true);
390 connect(m_resetUrlTimer
, &QTimer::timeout
,
391 this, &InformationPanel::reset
);
393 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
394 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
396 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
397 QDBusConnection::sessionBus(), this);
398 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
399 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
400 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
401 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
402 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
403 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
405 m_content
= new InformationPanelContent(this);
406 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
407 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
409 QVBoxLayout
* layout
= new QVBoxLayout(this);
410 layout
->setContentsMargins(0, 0, 0, 0);
411 layout
->addWidget(m_content
);
413 m_initialized
= true;