File indexing completed on 2024-05-19 04:56:13

0001 /**
0002  * \file icoreplatformtools.cpp
0003  * Interface for GUI independent platform specific tools.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 06 Apr 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 "icoreplatformtools.h"
0028 #include <QString>
0029 
0030 /**
0031  * Destructor.
0032  */
0033 ICorePlatformTools::~ICorePlatformTools()
0034 {
0035 }
0036 
0037 /**
0038  * Display dialog to select an existing file.
0039  * This default implementation only displays a warning, it is only supported
0040  * when having a GUI.
0041  * @param parent parent widget
0042  * @param caption caption
0043  * @param dir directory
0044  * @param filter filter
0045  * @param selectedFilter the selected filter is returned here
0046  * @return selected file, empty if canceled.
0047  */
0048 QString ICorePlatformTools::getOpenFileName(QWidget* parent,
0049     const QString& caption, const QString& dir, const QString& filter,
0050     QString* selectedFilter)
0051 {
0052   Q_UNUSED(parent)
0053   Q_UNUSED(selectedFilter)
0054   qWarning("getOpenFileName(%s, %s, %s) not implemented without GUI.",
0055            qPrintable(caption), qPrintable(dir), qPrintable(filter));
0056   return QString();
0057 }
0058 
0059 /**
0060  * Display dialog to select a file to save.
0061  * This default implementation only displays a warning, it is only supported
0062  * when having a GUI.
0063  * @param parent parent widget
0064  * @param caption caption
0065  * @param dir directory
0066  * @param filter filter
0067  * @param selectedFilter the selected filter is returned here
0068  * @return selected file, empty if canceled.
0069  */
0070 QString ICorePlatformTools::getSaveFileName(QWidget* parent,
0071     const QString& caption, const QString& dir, const QString& filter,
0072     QString* selectedFilter)
0073 {
0074   Q_UNUSED(parent)
0075   Q_UNUSED(selectedFilter)
0076   qWarning("getSaveFileName(%s, %s, %s) not implemented without GUI.",
0077            qPrintable(caption), qPrintable(dir), qPrintable(filter));
0078   return QString();
0079 }
0080 
0081 /**
0082  * Display dialog to select an existing directory.
0083  * This default implementation only displays a warning, it is only supported
0084  * when having a GUI.
0085  * @param parent parent widget
0086  * @param caption caption
0087  * @param startDir start directory
0088  * @return selected directory, empty if canceled.
0089  */
0090 QString ICorePlatformTools::getExistingDirectory(QWidget* parent,
0091     const QString& caption, const QString& startDir)
0092 {
0093   Q_UNUSED(parent)
0094   qWarning("getExistingDirectory(%s, %s) not implemented without GUI.",
0095            qPrintable(caption), qPrintable(startDir));
0096   return QString();
0097 }
0098 
0099 /**
0100  * Check if platform has a graphical user interface.
0101  * @return true if platform has GUI.
0102  */
0103 bool ICorePlatformTools::hasGui() const
0104 {
0105   return false;
0106 }
0107 
0108 /**
0109  * Construct a name filter string suitable for file dialogs.
0110  * This function can be used to implement fileDialogNameFilter()
0111  * for QFileDialog.
0112  * @param nameFilters list of description, filter pairs, e.g.
0113  * [("Images", "*.jpg *.jpeg *.png *.webp"), ("All Files", "*")].
0114  * @return name filter string.
0115  */
0116 QString ICorePlatformTools::qtFileDialogNameFilter(
0117     const QList<QPair<QString, QString> >& nameFilters)
0118 {
0119   QString filter;
0120   for (auto it = nameFilters.constBegin(); it != nameFilters.constEnd(); ++it) {
0121     if (!filter.isEmpty()) {
0122       filter += QLatin1String(";;");
0123     }
0124     filter += it->first;
0125     filter += QLatin1String(" (");
0126     filter += it->second;
0127     filter += QLatin1Char(')');
0128   }
0129   return filter;
0130 }
0131 
0132 /**
0133  * Get file pattern part of m_nameFilter.
0134  * This function can be used to implement getNameFilterPatterns()
0135  * for QFileDialog.
0136  * @param nameFilter name filter string
0137  * @return file patterns, e.g. "*.mp3".
0138  */
0139 QString ICorePlatformTools::qtNameFilterPatterns(const QString& nameFilter)
0140 {
0141   int start = nameFilter.indexOf(QLatin1Char('(')),
0142       end = nameFilter.indexOf(QLatin1Char(')'));
0143   return start != -1 && end != -1 && end > start
0144       ? nameFilter.mid(start + 1, end - start - 1)
0145       : QString();
0146 }