]> git.sur5r.net Git - minitube/blob - src/snapshotsettings.cpp
Upload 3.9.3-2 to unstable
[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 #ifdef APP_MAC
24 #include "macutils.h"
25 #endif
26 #include "constants.h"
27 #include <QDesktopServices>
28
29 SnapshotSettings::SnapshotSettings(QWidget *parent) : QWidget(parent) {
30     QBoxLayout *layout = new QHBoxLayout(this);
31     layout->setContentsMargins(5, 0, 10, 0);
32     layout->setSpacing(5);
33
34     thumb = new QToolButton();
35     thumb->setStyleSheet("border:0");
36     thumb->setCursor(Qt::PointingHandCursor);
37     connect(thumb, SIGNAL(clicked()), SLOT(openFile()));
38     layout->addWidget(thumb);
39
40     message = new QLabel();
41     connect(message, SIGNAL(linkActivated(QString)), this, SLOT(showFile()));
42     layout->addWidget(message);
43
44     changeFolderButton = new QPushButton();
45     changeFolderButton->setAttribute(Qt::WA_MacMiniSize);
46     changeFolderButton->setText(tr("Change location..."));
47     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
48     layout->addWidget(changeFolderButton);
49 }
50
51 void SnapshotSettings::setSnapshot(const QPixmap &pixmap, const QString &filename) {
52     QPixmap p = pixmap.scaledToHeight(message->sizeHint().height());
53     QIcon icon(p);
54     thumb->setIcon(icon);
55     thumb->setIconSize(p.size());
56     // thumb->setPixmap(pixmap.scaledToHeight(message->sizeHint().height()));
57
58     this->filename = filename;
59     QFileInfo fileInfo(filename);
60     QString path = fileInfo.absolutePath();
61     QString display = displayPath(path);
62
63     QString msg = tr("Snapshot saved to %1")
64             .arg("<a href='showFile' style='text-decoration:none; color:palette(text); font-weight:bold'>%1</a>")
65             .arg(display);
66     message->setText(msg);
67 }
68
69 void SnapshotSettings::setCurrentLocation(const QString &location) {
70     QSettings settings;
71     settings.setValue("snapshotsFolder", location);
72 }
73
74 QString SnapshotSettings::getCurrentLocation() {
75     QSettings settings;
76     QString location = settings.value("snapshotsFolder").toString();
77     if (location.isEmpty() || !QFile::exists(location)) {
78         location = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
79 #ifdef APP_MAC_STORE
80         location += "/MinitubeforYouTube";
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     const QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
92     QString displayPath = path;
93     displayPath = displayPath.remove(home + "/");
94     return displayPath;
95 }
96
97 void SnapshotSettings::changeFolder() {
98     const QString path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
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     dialog->setDirectory(path);
104     dialog->open(this, SLOT(folderChosen(const QString &)));
105 #else
106     QString folder = QFileDialog::getExistingDirectory(window(), QString(),
107                                                        path,
108                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
109     folderChosen(folder);
110 #endif
111 }
112
113 void SnapshotSettings::folderChosen(const QString &dir) {
114     if (!dir.isEmpty()) {
115         setCurrentLocation(dir);
116         QString status = tr("Snapshots location changed.");
117         MainWindow::instance()->showMessage(status);
118     }
119 }
120
121 void SnapshotSettings::showFile() {
122     QFileInfo info(filename);
123 #ifdef APP_MAC
124     mac::showInFinder(info.absoluteFilePath());
125 #else
126     QUrl url = QUrl::fromLocalFile(info.absolutePath());
127     QDesktopServices::openUrl(url);
128 #endif
129 }
130
131 void SnapshotSettings::openFile() {
132     QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
133 }