forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
119 lines (113 loc) · 3.82 KB
/
util.cpp
File metadata and controls
119 lines (113 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2014-2018 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util.h"
#include <QFileInfo>
#include <QWidget>
#include <QStringList>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QUrl>
#include <QDesktopServices>
#include <QMessageBox>
#include <MltProducer.h>
#include "shotcut_mlt_properties.h"
QString Util::baseName(const QString &filePath)
{
QString s = filePath;
// Only if absolute path and not a URI.
if (s.startsWith('/') || s.midRef(1, 2) == ":/" || s.midRef(1, 2) == ":\\")
s = QFileInfo(s).fileName();
return s;
}
void Util::setColorsToHighlight(QWidget* widget, QPalette::ColorRole role)
{
QPalette palette = widget->palette();
palette.setColor(role, palette.color(palette.Highlight));
palette.setColor(role == QPalette::Button ? QPalette::ButtonText : QPalette::WindowText,
palette.color(palette.HighlightedText));
widget->setPalette(palette);
widget->setAutoFillBackground(true);
}
void Util::showInFolder(const QString& path)
{
QFileInfo info(path);
#if defined(Q_OS_WIN)
QStringList args;
if (!info.isDir())
args << "/select,";
args << QDir::toNativeSeparators(path);
if (QProcess::startDetached("explorer", args))
return;
#elif defined(Q_OS_MAC)
QStringList args;
args << "-e";
args << "tell application \"Finder\"";
args << "-e";
args << "activate";
args << "-e";
args << "select POSIX file \"" + path + "\"";
args << "-e";
args << "end tell";
#if !defined(QT_DEBUG)
args << "-e";
args << "return";
#endif
if (!QProcess::execute("/usr/bin/osascript", args))
return;
#endif
QDesktopServices::openUrl(QUrl::fromLocalFile(info.isDir()? path : info.path()));
}
bool Util::warnIfNotWritable(const QString& filePath, QWidget* parent, const QString& caption)
{
// Returns true if not writable.
if (!filePath.isEmpty()) {
// Do a hard check by writing to the file.
QFile file(filePath);
file.open(QIODevice::WriteOnly);
if (file.write("") < 0) {
QFileInfo fi(filePath);
QMessageBox::warning(parent, caption,
QObject::tr("Unable to write file %1\n"
"Perhaps you do not have permission.\n"
"Try again with a different folder.")
.arg(fi.fileName()));
return true;
} else {
file.remove();
}
}
return false;
}
QString Util::producerTitle(const Mlt::Producer& producer)
{
QString result;
Mlt::Producer& p = const_cast<Mlt::Producer&>(producer);
if (!p.is_valid()) return result;
if (p.get(kShotcutTransitionProperty))
return QObject::tr("Transition");
if (p.get(kTrackNameProperty))
return QObject::tr("Track: %1").arg(QString::fromUtf8(p.get(kTrackNameProperty)));
if (tractor_type == p.type())
return QObject::tr("Master");
if (p.get(kShotcutCaptionProperty))
return QString::fromUtf8(p.get(kShotcutCaptionProperty));
if (p.get("resource"))
return Util::baseName(QString::fromUtf8(p.get("resource")));
return result;
}