]> git.sur5r.net Git - minitube/blob - src/snapshotsettings.cpp
bfe5275718665f9b6a005531fd9a58a6a907a923
[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 #if QT_VERSION >= 0x050000
88     QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
89 #else
90     QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
91 #endif
92     QString displayPath = path;
93     displayPath = displayPath.remove(home + "/");
94     return displayPath;
95 }
96
97 void SnapshotSettings::changeFolder() {
98     QString path;
99 #ifdef APP_MAC
100     QFileDialog* dialog = new QFileDialog(this);
101     dialog->setFileMode(QFileDialog::Directory);
102     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
103 #if QT_VERSION >= 0x050000
104     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
105 #else
106     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
107 #endif
108     dialog->setDirectory(path);
109     dialog->open(this, SLOT(folderChosen(const QString &)));
110 #else
111
112 #if QT_VERSION >= 0x050000
113     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
114 #else
115     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
116 #endif
117     QString folder = QFileDialog::getExistingDirectory(window(), QString(),
118                                                        path,
119                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
120     folderChosen(folder);
121 #endif
122 }
123
124 void SnapshotSettings::folderChosen(const QString &dir) {
125     if (!dir.isEmpty()) {
126         setCurrentLocation(dir);
127         QString status = tr("Snapshots location changed.");
128         MainWindow::instance()->showMessage(status);
129     }
130 }
131
132 void SnapshotSettings::showFile() {
133     QFileInfo info(filename);
134 #ifdef APP_MAC
135     mac::showInFinder(info.absoluteFilePath());
136 #else
137     QUrl url = QUrl::fromLocalFile(info.absolutePath());
138     QDesktopServices::openUrl(url);
139 #endif
140 }
141
142 void SnapshotSettings::openFile() {
143     QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
144 }