]>
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"
11 #define HAVE_KDIRNOTIFY __has_include(<KDirNotify>)
15 #include <KIO/StatJob>
16 #include <KJobWidgets>
17 #include <KLocalizedString>
19 #include <Baloo/FileMetaDataWidget>
21 #include <QApplication>
25 #include <QVBoxLayout>
27 #include "dolphin_informationpanelsettings.h"
29 InformationPanel::InformationPanel(QWidget
*parent
)
31 , m_initialized(false)
32 , m_infoTimer(nullptr)
33 , m_urlChangedTimer(nullptr)
34 , m_resetUrlTimer(nullptr)
37 , m_invalidUrlCandidate()
40 , m_folderStatJob(nullptr)
45 InformationPanel::~InformationPanel()
49 void InformationPanel::setSelection(const KFileItemList
&selection
)
51 m_selection
= selection
;
57 const int count
= selection
.count();
59 if (!isEqualToShownUrl(url())) {
65 if ((count
== 1) && !selection
.first().url().isEmpty()) {
66 m_urlCandidate
= selection
.first().url();
72 void InformationPanel::requestDelayedItemInfo(const KFileItem
&item
)
74 if (!isVisible() || !InformationPanelSettings::showHovered()) {
78 if (QApplication::mouseButtons() & Qt::LeftButton
) {
79 // Ignore the request of an item information when a rubberband
80 // selection is ongoing.
85 m_hoveredItem
= KFileItem();
95 bool InformationPanel::urlChanged()
97 if (!url().isValid()) {
108 if (!isEqualToShownUrl(url())) {
111 // Update the content with a delay. This gives
112 // the directory lister the chance to show the content
113 // before expensive operations are done to show
115 m_urlChangedTimer
->start();
121 void InformationPanel::showEvent(QShowEvent
*event
)
123 Panel::showEvent(event
);
124 if (!event
->spontaneous()) {
125 if (!m_initialized
) {
126 // do a delayed initialization so that no performance
127 // penalty is given when Dolphin is started with a closed
137 void InformationPanel::resizeEvent(QResizeEvent
*event
)
140 m_urlCandidate
= m_shownUrl
;
141 m_infoTimer
->start();
143 Panel::resizeEvent(event
);
146 void InformationPanel::contextMenuEvent(QContextMenuEvent
*event
)
148 showContextMenu(event
->globalPos());
149 Panel::contextMenuEvent(event
);
152 void InformationPanel::showContextMenu(const QPoint
&pos
)
156 QAction
*previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
157 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
158 previewAction
->setCheckable(true);
159 previewAction
->setChecked(InformationPanelSettings::previewsShown());
161 QAction
*previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
162 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
163 previewAutoPlayAction
->setCheckable(true);
164 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
166 QAction
*showHoveredAction
= popup
.addAction(i18nc("@action:inmenu", "Show item on hover"));
167 showHoveredAction
->setIcon(QIcon::fromTheme(QStringLiteral("followmouse")));
168 showHoveredAction
->setCheckable(true);
169 showHoveredAction
->setChecked(InformationPanelSettings::showHovered());
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
== previewAutoPlayAction
) {
200 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
201 m_content
->setPreviewAutoPlay(isChecked
);
202 } else if (action
== showHoveredAction
) {
203 InformationPanelSettings::setShowHovered(isChecked
);
205 m_hoveredItem
= KFileItem();
208 } else if (action
== configureAction
) {
209 m_inConfigurationMode
= true;
210 m_content
->configureShownProperties();
211 } else if (action
== dateformatAction
) {
212 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
214 InformationPanelSettings::setDateFormat(dateFormat
);
215 m_content
->refreshMetaData();
219 void InformationPanel::showItemInfo()
226 //qDebug() << "showItemInfo" << m_fileItem;
228 if (m_hoveredItem
.isNull() && (m_selection
.count() > 1)) {
229 // The information for a selection of items should be shown
230 m_content
->showItems(m_selection
);
232 // The information for exactly one item should be shown
234 if (!m_hoveredItem
.isNull() && InformationPanelSettings::showHovered()) {
235 item
= m_hoveredItem
;
236 } else if (!m_selection
.isEmpty()) {
237 Q_ASSERT(m_selection
.count() == 1);
238 item
= m_selection
.first();
241 if (!item
.isNull()) {
242 m_shownUrl
= item
.url();
243 m_content
->showItem(item
);
247 // No item is hovered and no selection has been done: provide
248 // an item for the currently shown directory.
250 m_folderStatJob
= KIO::stat(m_shownUrl
, KIO::StatJob::SourceSide
, KIO::StatDefaultDetails
| KIO::StatRecursiveSize
, KIO::HideProgressInfo
);
251 if (m_folderStatJob
->uiDelegate()) {
252 KJobWidgets::setWindow(m_folderStatJob
, this);
254 connect(m_folderStatJob
, &KIO::Job::result
, this, &InformationPanel::slotFolderStatFinished
);
258 void InformationPanel::slotFolderStatFinished(KJob
*job
)
260 m_folderStatJob
= nullptr;
261 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
262 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
265 void InformationPanel::slotInfoTimeout()
267 m_shownUrl
= m_urlCandidate
;
268 m_urlCandidate
.clear();
272 void InformationPanel::reset()
274 if (m_invalidUrlCandidate
== m_shownUrl
) {
275 m_invalidUrlCandidate
= QUrl();
277 // The current URL is still invalid. Reset
278 // the content to show the directory URL.
285 void InformationPanel::slotFileRenamed(const QString
&source
, const QString
&dest
)
287 auto sourceUrl
= QUrl::fromUserInput(source
);
288 if (m_shownUrl
== sourceUrl
) {
289 auto destUrl
= QUrl::fromUserInput(dest
);
291 if ((m_selection
.count() == 1) && (m_selection
[0].url() == sourceUrl
)) {
292 m_selection
[0] = KFileItem(destUrl
);
293 // Implementation note: Updating the selection is only required if exactly one
294 // item is selected, as the name of the item is shown. If this should change
295 // in future: Before parsing the whole selection take care to test possible
296 // performance bottlenecks when renaming several hundreds of files.
303 void InformationPanel::slotFilesAdded(const QString
&directory
)
305 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
306 // If the 'trash' icon changes because the trash has been emptied or got filled,
307 // the signal filesAdded("trash:/") will be emitted.
308 requestDelayedItemInfo(KFileItem());
312 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
314 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
315 if (!item
.isNull()) {
320 void InformationPanel::slotFilesChanged(const QStringList
&files
)
322 for (const QString
&fileName
: files
) {
323 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
330 void InformationPanel::slotFilesRemoved(const QStringList
&files
)
332 for (const QString
&fileName
: files
) {
333 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
334 // the currently shown item has been removed, show
335 // the parent directory as fallback
342 void InformationPanel::slotEnteredDirectory(const QString
&directory
)
347 void InformationPanel::slotLeftDirectory(const QString
&directory
)
349 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
350 // The signal 'leftDirectory' is also emitted when a media
351 // has been unmounted. In this case no directory change will be
352 // done in Dolphin, but the Information Panel must be updated to
353 // indicate an invalid directory.
358 void InformationPanel::cancelRequest()
360 delete m_folderStatJob
;
361 m_folderStatJob
= nullptr;
364 m_resetUrlTimer
->stop();
365 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
366 // has the smallest interval (see init()), it is not possible that the exceeded timer
367 // would overwrite an information provided by a selection or hovering.
369 m_invalidUrlCandidate
.clear();
370 m_urlCandidate
.clear();
373 bool InformationPanel::isEqualToShownUrl(const QUrl
&url
) const
375 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
378 void InformationPanel::markUrlAsInvalid()
380 m_invalidUrlCandidate
= m_shownUrl
;
381 m_resetUrlTimer
->start();
384 void InformationPanel::readSettings()
387 m_content
->refreshPreview();
388 m_content
->refreshMetaData();
392 void InformationPanel::init()
394 m_infoTimer
= new QTimer(this);
395 m_infoTimer
->setInterval(300);
396 m_infoTimer
->setSingleShot(true);
397 connect(m_infoTimer
, &QTimer::timeout
, this, &InformationPanel::slotInfoTimeout
);
399 m_urlChangedTimer
= new QTimer(this);
400 m_urlChangedTimer
->setInterval(200);
401 m_urlChangedTimer
->setSingleShot(true);
402 connect(m_urlChangedTimer
, &QTimer::timeout
, this, &InformationPanel::showItemInfo
);
404 m_resetUrlTimer
= new QTimer(this);
405 m_resetUrlTimer
->setInterval(1000);
406 m_resetUrlTimer
->setSingleShot(true);
407 connect(m_resetUrlTimer
, &QTimer::timeout
, this, &InformationPanel::reset
);
409 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
410 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
413 org::kde::KDirNotify
*dirNotify
= new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
414 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
415 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
416 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
417 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
418 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
419 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
422 m_content
= new InformationPanelContent(this);
423 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
424 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() {
425 m_inConfigurationMode
= false;
427 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
429 QVBoxLayout
*layout
= new QVBoxLayout(this);
430 layout
->setContentsMargins(0, 0, 0, 0);
431 layout
->addWidget(m_content
);
433 m_initialized
= true;
436 #include "moc_informationpanel.cpp"