Warning, file /multimedia/kid3/src/app/qt/platformtools.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file platformtools.cpp
0003  * Platform specific tools.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 30 Mar 2013
0008  *
0009  * Copyright (C) 2013-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "platformtools.h"
0028 #include <QFileInfo>
0029 #include <QFileDialog>
0030 #include <QIcon>
0031 #include <QSettings>
0032 #include <QCoreApplication>
0033 #include "config.h"
0034 #include "browserdialog.h"
0035 #include "messagedialog.h"
0036 #include "kid3settings.h"
0037 #include "mainwindowconfig.h"
0038 
0039 namespace {
0040 
0041 /**
0042  * Get default dialog options according to configuration.
0043  * @return file dialog options.
0044  */
0045 QFileDialog::Options fileDialogOptions()
0046 {
0047   QFileDialog::Options options;
0048   if (MainWindowConfig::instance().dontUseNativeDialogs()) {
0049     options |= QFileDialog::DontUseNativeDialog;
0050   }
0051 #if QT_VERSION >= 0x050200
0052   options |= QFileDialog::DontUseCustomDirectoryIcons;
0053 #endif
0054   return options;
0055 }
0056 
0057 }
0058 
0059 /**
0060  * Constructor.
0061  */
0062 PlatformTools::PlatformTools()
0063 {
0064   // Must not be inline because of forwared declared QScopedPointer.
0065 }
0066 
0067 /**
0068  * Destructor.
0069  */
0070 PlatformTools::~PlatformTools()
0071 {
0072   if (m_helpBrowser) {
0073     // Without close() the application will not quit when the main window is
0074     // closed but the help browser is still open.
0075     m_helpBrowser->close();
0076   }
0077 }
0078 
0079 /**
0080  * Get application settings.
0081  * @return settings instance.
0082  */
0083 ISettings* PlatformTools::applicationSettings()
0084 {
0085   return CorePlatformTools::applicationSettings();
0086 }
0087 
0088 /**
0089  * Get icon provider for tagged files.
0090  * @return icon provider.
0091  */
0092 CoreTaggedFileIconProvider* PlatformTools::iconProvider()
0093 {
0094   return GuiPlatformTools::iconProvider();
0095 }
0096 
0097 /**
0098  * Write text to clipboard.
0099  * @param text text to write
0100  * @return true if operation is supported.
0101  */
0102 bool PlatformTools::writeToClipboard(const QString& text) const
0103 {
0104   return GuiPlatformTools::writeToClipboard(text);
0105 }
0106 
0107 /**
0108  * Read text from clipboard.
0109  * @return text, null if operation not supported.
0110  */
0111 QString PlatformTools::readFromClipboard() const
0112 {
0113   return GuiPlatformTools::readFromClipboard();
0114 }
0115 
0116 /**
0117  * Create an audio player instance.
0118  * @param app application context
0119  * @param dbusEnabled true to enable MPRIS D-Bus interface
0120  * @return audio player, nullptr if not supported.
0121  */
0122 QObject* PlatformTools::createAudioPlayer(Kid3Application* app,
0123                                    bool dbusEnabled) const
0124 {
0125   return GuiPlatformTools::createAudioPlayer(app, dbusEnabled);
0126 }
0127 
0128 /**
0129  * Move file or directory to trash.
0130  *
0131  * @param path path to file or directory
0132  *
0133  * @return true if ok.
0134  */
0135 bool PlatformTools::moveToTrash(const QString& path) const
0136 {
0137   return CorePlatformTools::moveToTrash(path);
0138 }
0139 
0140 /**
0141  * Display help for a topic.
0142  *
0143  * @param anchor anchor in help document
0144  */
0145 void PlatformTools::displayHelp(const QString& anchor)
0146 {
0147   if (!m_helpBrowser) {
0148     const char* const kid3HandbookStr =
0149         QT_TRANSLATE_NOOP("@default", "Kid3 Handbook");
0150     QString caption(QCoreApplication::translate("@default", kid3HandbookStr));
0151     m_helpBrowser.reset(new BrowserDialog(nullptr, caption));
0152   }
0153   m_helpBrowser->goToAnchor(anchor);
0154   m_helpBrowser->setModal(!anchor.isEmpty());
0155   if (m_helpBrowser->isHidden()) {
0156     m_helpBrowser->show();
0157   }
0158 }
0159 
0160 /**
0161  * Get a themed icon by name.
0162  * @param name name of icon
0163  * @return icon.
0164  */
0165 QIcon PlatformTools::iconFromTheme(const QString& name) const
0166 {
0167   return QIcon::fromTheme(name,
0168       QIcon(QLatin1String(":/images/") + name + QLatin1String(".png")));
0169 }
0170 
0171 /**
0172  * Construct a name filter string suitable for file dialogs.
0173  * @param nameFilters list of description, filter pairs, e.g.
0174  * [("Images", "*.jpg *.jpeg *.png *.webp"), ("All Files", "*")].
0175  * @return name filter string.
0176  */
0177 QString PlatformTools::fileDialogNameFilter(
0178     const QList<QPair<QString, QString> >& nameFilters) const
0179 {
0180   return CorePlatformTools::fileDialogNameFilter(nameFilters);
0181 }
0182 
0183 /**
0184  * Get file pattern part of m_nameFilter.
0185  * @param nameFilter name filter string
0186  * @return file patterns, e.g. "*.mp3".
0187  */
0188 QString PlatformTools::getNameFilterPatterns(const QString& nameFilter) const
0189 {
0190   return CorePlatformTools::getNameFilterPatterns(nameFilter);
0191 }
0192 
0193 /**
0194  * Display error dialog with item list.
0195  * @param parent parent widget
0196  * @param text text
0197  * @param strlist list of items
0198  * @param caption caption
0199  */
0200 void PlatformTools::errorList(QWidget* parent, const QString& text,
0201     const QStringList& strlist, const QString& caption)
0202 {
0203   MessageDialog::warningList(parent, caption, text, strlist, QMessageBox::Ok);
0204 }
0205 
0206 /**
0207  * Display warning dialog with yes, no, cancel buttons.
0208  * @param parent parent widget
0209  * @param text text
0210  * @param caption caption
0211  * @return QMessageBox::Yes, QMessageBox::No or QMessageBox::Cancel.
0212  */
0213 int PlatformTools::warningYesNoCancel(QWidget* parent, const QString& text,
0214     const QString& caption)
0215 {
0216   return QMessageBox::warning(parent, caption, text,
0217                               QMessageBox::Yes | QMessageBox::No |
0218                               QMessageBox::Cancel,
0219                               QMessageBox::Yes);
0220 }
0221 
0222 /**
0223  * Display warning dialog with item list.
0224  * @param parent parent widget
0225  * @param text text
0226  * @param strlist list of items
0227  * @param caption caption
0228  * @return QMessageBox::Yes or QMessageBox::No.
0229  */
0230 int PlatformTools::warningYesNoList(QWidget* parent, const QString& text,
0231     const QStringList& strlist, const QString& caption)
0232 {
0233   return MessageDialog::warningList(parent, caption, text, strlist,
0234                                     QMessageBox::Yes | QMessageBox::No);
0235 }
0236 
0237 /**
0238  * Display dialog to select an existing file.
0239  * @param parent parent widget
0240  * @param caption caption
0241  * @param dir directory
0242  * @param filter filter
0243  * @param selectedFilter the selected filter is returned here
0244  * @return selected file, empty if canceled.
0245  */
0246 QString PlatformTools::getOpenFileName(QWidget* parent, const QString& caption,
0247     const QString& dir, const QString& filter, QString* selectedFilter)
0248 {
0249   return QFileDialog::getOpenFileName(
0250         parent, caption, dir, filter, selectedFilter, fileDialogOptions());
0251 }
0252 
0253 /**
0254  * Display dialog to select existing files.
0255  * @param parent parent widget
0256  * @param caption caption
0257  * @param dir directory
0258  * @param filter filter
0259  * @param selectedFilter the selected filter is returned here
0260  * @return selected files, empty if canceled.
0261  */
0262 QStringList PlatformTools::getOpenFileNames(QWidget* parent,
0263     const QString& caption, const QString& dir,
0264     const QString& filter, QString* selectedFilter)
0265 {
0266   return QFileDialog::getOpenFileNames(
0267         parent, caption, dir, filter, selectedFilter, fileDialogOptions());
0268 }
0269 
0270 /**
0271  * Display dialog to select a file to save.
0272  * @param parent parent widget
0273  * @param caption caption
0274  * @param dir directory
0275  * @param filter filter
0276  * @param selectedFilter the selected filter is returned here
0277  * @return selected file, empty if canceled.
0278  */
0279 QString PlatformTools::getSaveFileName(QWidget* parent, const QString& caption,
0280     const QString& dir, const QString& filter, QString* selectedFilter)
0281 {
0282   return QFileDialog::getSaveFileName(
0283         parent, caption, dir, filter, selectedFilter, fileDialogOptions());
0284 }
0285 
0286 /**
0287  * Display dialog to select an existing directory.
0288  * @param parent parent widget
0289  * @param caption caption
0290  * @param startDir start directory
0291  * @return selected directory, empty if canceled.
0292  */
0293 QString PlatformTools::getExistingDirectory(QWidget* parent,
0294     const QString& caption, const QString& startDir)
0295 {
0296   return QFileDialog::getExistingDirectory(parent, caption, startDir,
0297       fileDialogOptions() | QFileDialog::ShowDirsOnly);
0298 }
0299 
0300 /**
0301  * Check if platform has a graphical user interface.
0302  * @return true if platform has GUI.
0303  */
0304 bool PlatformTools::hasGui() const
0305 {
0306   return true;
0307 }
0308 
0309 /**
0310  * Display warning dialog.
0311  * @param parent parent widget
0312  * @param text text
0313  * @param details detailed message
0314  * @param caption caption
0315  */
0316 void PlatformTools::warningDialog(QWidget* parent,
0317     const QString& text, const QString& details, const QString& caption)
0318 {
0319   MessageDialog dialog(parent);
0320   dialog.setWindowTitle(caption);
0321   dialog.setText(text);
0322   dialog.setInformativeText(details);
0323   dialog.setIcon(QMessageBox::Warning);
0324   dialog.exec();
0325 }
0326 
0327 /**
0328  * Display warning dialog with options to continue or cancel.
0329  * @param parent parent widget
0330  * @param text text
0331  * @param strlist list of items
0332  * @param caption caption
0333  * @return true if continue was selected.
0334  */
0335 bool PlatformTools::warningContinueCancelList(QWidget* parent,
0336     const QString& text, const QStringList& strlist, const QString& caption)
0337 {
0338   return MessageDialog::warningList(parent, caption, text, strlist) ==
0339       QMessageBox::Ok;
0340 }