1 /*
2 * utils.h
3 * Copyright 2009-2010, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4 *
5 * This file is part of Tiled.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include "rangeset.h"
24
25 #include <QIcon>
26 #include <QSettings>
27 #include <QString>
28
29 #include <memory>
30
31 #include "qtcompat_p.h"
32
33 class QAction;
34 class QKeyEvent;
35 class QMenu;
36
37 namespace Tiled {
38 namespace Utils {
39
40 QString readableImageFormatsFilter();
41 QString writableImageFormatsFilter();
42
43 QStringList cleanFilterList(const QString &filter);
44 bool fileNameMatchesNameFilter(const QString &fileName,
45 const QString &nameFilter);
46 QString firstExtension(const QString &nameFilter);
47
48 int matchingScore(const QStringList &words, QStringRef string);
49 RangeSet<int> matchingRanges(const QStringList &words, QStringRef string);
50
51 /**
52 * Looks up the icon with the specified \a name from the system theme and set
53 * it on the instance \a t when found.
54 *
55 * This is a template method which is used on instances of QAction, QMenu,
56 * QToolButton, etc.
57 *
58 * Does nothing when the platform is not Linux.
59 */
60 template <class T>
setThemeIcon(T * t,const QString & name)61 void setThemeIcon(T *t, const QString &name)
62 {
63 #ifdef Q_OS_LINUX
64 QIcon themeIcon = QIcon::fromTheme(name);
65 if (!themeIcon.isNull())
66 t->setIcon(themeIcon);
67 #else
68 Q_UNUSED(t)
69 Q_UNUSED(name)
70 #endif
71 }
72
73 template <class T>
setThemeIcon(T * t,const char * name)74 void setThemeIcon(T *t, const char *name)
75 {
76 setThemeIcon(t, QLatin1String(name));
77 }
78
79 void restoreGeometry(QWidget *widget);
80 void saveGeometry(QWidget *widget);
81
82 int defaultDpi();
83 qreal defaultDpiScale();
84 int dpiScaled(int value);
85 qreal dpiScaled(qreal value);
86 QSize dpiScaled(QSize value);
87 QPoint dpiScaled(QPoint value);
88 QRectF dpiScaled(QRectF value);
89 QSize smallIconSize();
90
91 bool isZoomInShortcut(QKeyEvent *event);
92 bool isZoomOutShortcut(QKeyEvent *event);
93 bool isResetZoomShortcut(QKeyEvent *event);
94
95 void addFileManagerActions(QMenu &menu, const QString &fileName);
96 void addOpenContainingFolderAction(QMenu &menu, const QString &fileName);
97 void addOpenWithSystemEditorAction(QMenu &menu, const QString &fileName);
98
99 QSettings::Format jsonSettingsFormat();
100 std::unique_ptr<QSettings> jsonSettings(const QString &fileName);
101
102 } // namespace Utils
103 } // namespace Tiled
104