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