]> git.sur5r.net Git - minitube/blob - src/snapshotsettings.cpp
Imported Upstream version 2.3.1
[minitube] / src / snapshotsettings.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "snapshotsettings.h"
22 #include "mainwindow.h"
23 #include <QDesktopServices>
24 #ifdef APP_MAC
25 #include "macutils.h"
26 #endif
27
28 SnapshotSettings::SnapshotSettings(QWidget *parent) : QWidget(parent) {
29     QBoxLayout *layout = new QHBoxLayout(this);
30     layout->setContentsMargins(5, 0, 10, 0);
31     layout->setSpacing(5);
32
33     thumb = new QToolButton();
34     thumb->setStyleSheet("border:0");
35     thumb->setCursor(Qt::PointingHandCursor);
36     connect(thumb, SIGNAL(clicked()), SLOT(openFile()));
37     layout->addWidget(thumb);
38
39     message = new QLabel();
40     connect(message, SIGNAL(linkActivated(QString)), this, SLOT(showFile()));
41     layout->addWidget(message);
42
43     changeFolderButton = new QPushButton();
44     changeFolderButton->setAttribute(Qt::WA_MacMiniSize);
45     changeFolderButton->setText(tr("Change location..."));
46     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
47     layout->addWidget(changeFolderButton);
48 }
49
50 void SnapshotSettings::setSnapshot(const QPixmap &pixmap, const QString &filename) {
51     QPixmap p = pixmap.scaledToHeight(message->sizeHint().height());
52     QIcon icon(p);
53     thumb->setIcon(icon);
54     thumb->setIconSize(p.size());
55     // thumb->setPixmap(pixmap.scaledToHeight(message->sizeHint().height()));
56
57     this->filename = filename;
58     QFileInfo fileInfo(filename);
59     QString path = fileInfo.absolutePath();
60     QString display = displayPath(path);
61
62     QString msg = tr("Snapshot saved to %1")
63                 .arg("<a href='showFile' style='text-decoration:none; color:palette(text); font-weight:bold'>%1</a>")
64                 .arg(display);
65     message->setText(msg);
66 }
67
68 void SnapshotSettings::setCurrentLocation(const QString &location) {
69     QSettings settings;
70     settings.setValue("snapshotsFolder", location);
71 }
72
73 QString SnapshotSettings::getCurrentLocation() {
74     QSettings settings;
75     QString location = settings.value("snapshotsFolder").toString();
76     if (location.isEmpty() || !QFile::exists(location)) {
77 #if QT_VERSION >= 0x050000
78         location = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
79 #else
80         location = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
81 #endif
82     }
83     return location;
84 }
85
86 QString SnapshotSettings::displayPath(const QString &path) {
87 #ifdef APP_MAC
88     return QDir(path).dirName();
89 #endif
90
91 #if QT_VERSION >= 0x050000
92     QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
93 #else
94     QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
95 #endif
96     QString displayPath = path;
97     displayPath = displayPath.remove(home + "/");
98     return displayPath;
99 }
100
101 void SnapshotSettings::changeFolder() {
102     QString path;
103 #ifdef APP_MAC
104     QFileDialog* dialog = new QFileDialog(this);
105     dialog->setFileMode(QFileDialog::Directory);
106     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
107 #if QT_VERSION >= 0x050000
108     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
109 #else
110     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
111 #endif
112     dialog->setDirectory(path);
113     dialog->open(this, SLOT(folderChosen(const QString &)));
114 #else
115
116 #if QT_VERSION >= 0x050000
117     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
118 #else
119     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
120 #endif
121     QString folder = QFileDialog::getExistingDirectory(window(), QString(),
122                                                        path,
123                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
124     folderChosen(folder);
125 #endif
126 }
127
128 void SnapshotSettings::folderChosen(const QString &dir) {
129     if (!dir.isEmpty()) {
130         setCurrentLocation(dir);
131         QString status = tr("Snapshots location changed.");
132         MainWindow::instance()->showMessage(status);
133     }
134 }
135
136 void SnapshotSettings::showFile() {
137     QFileInfo info(filename);
138 #ifdef APP_MAC
139     mac::showInFinder(info.absoluteFilePath());
140 #else
141     QUrl url = QUrl::fromLocalFile(info.absolutePath());
142     QDesktopServices::openUrl(url);
143 #endif
144 }
145
146 void SnapshotSettings::openFile() {
147     QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
148 }