| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "qnapiopendialog.h" |
| 16 | #include "libqnapi.h" |
| 17 | |
| 18 | QNapiOpenDialog::QNapiOpenDialog(QWidget* parent, const QString& caption, |
| 19 | const QString& init_path, |
| 20 | FilterMode filterMode) |
| 21 | : QFileDialog(parent), staticConfig(LibQNapi::staticConfig()) { |
| 22 | setAttribute(Qt::WA_QuitOnClose, false); |
| 23 | setWindowTitle(caption); |
| 24 | |
| 25 | if (filterMode == Movies) { |
| 26 | setNameFilter(tr("Video files (%1);;All files (*.*)") |
| 27 | .arg(staticConfig->movieExtensionsFilter())); |
| 28 | } else if (filterMode == Subtitles) { |
| 29 | setNameFilter(tr("Subtitles files (%1);;All files (*.*)") |
| 30 | .arg(staticConfig->subtitleExtensionsFilter())); |
| 31 | } |
| 32 | |
| 33 | if (QFileInfo(init_path).isDir()) |
| 34 | setDirectory(init_path); |
| 35 | else |
| 36 | setDirectory(QDir::currentPath()); |
| 37 | |
| 38 | QStringList sideUrls; |
| 39 | |
| 40 | #ifdef Q_OS_MAC |
| 41 | sideUrls << "/Volumes"; |
| 42 | #endif |
| 43 | |
| 44 | sideUrls << QString(QDir::homePath() + "/Movies") |
| 45 | << QString(QDir::homePath() + "/movies") |
| 46 | << QString(QDir::homePath() + "/Video") |
| 47 | << QString(QDir::homePath() + "/video") |
| 48 | << QString(QDir::homePath() + "/Videos") |
| 49 | << QString(QDir::homePath() + "/videos") |
| 50 | << QString(QDir::homePath() + "/Filmy") |
| 51 | << QString(QDir::homePath() + "/wideo"); |
| 52 | |
| 53 | QList<QUrl> urls = sidebarUrls(); |
| 54 | |
| 55 | foreach (QString sideUrl, sideUrls) { |
| 56 | if (!QDir().exists(sideUrl)) continue; |
| 57 | QUrl url = QUrl::fromLocalFile(sideUrl); |
| 58 | if (!urls.contains(url)) urls << url; |
| 59 | } |
| 60 | |
| 61 | setSidebarUrls(urls); |
| 62 | } |
| 63 | |
| 64 | bool QNapiOpenDialog::selectFile() { |
| 65 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) |
| 66 | files.clear(); |
| 67 | QString file = getOpenFileName(this, windowTitle(), directory().path(), |
| 68 | nameFilters().join("\n")); |
| 69 | |
| 70 | if (!file.isEmpty()) files << file; |
| 71 | |
| 72 | return !file.isEmpty(); |
| 73 | #else |
| 74 | if (!placeWindow()) return false; |
| 75 | setFileMode(QFileDialog::ExistingFile); |
| 76 | return exec(); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | bool QNapiOpenDialog::selectFiles() { |
| 81 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) |
| 82 | files = getOpenFileNames(this, windowTitle(), directory().path(), |
| 83 | nameFilters().join("\n")); |
| 84 | return !files.isEmpty(); |
| 85 | #else |
| 86 | if (!placeWindow()) return false; |
| 87 | setFileMode(QFileDialog::ExistingFiles); |
| 88 | return exec(); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | bool QNapiOpenDialog::selectDirectory() { |
| 93 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) |
| 94 | files.clear(); |
| 95 | QString dir = getExistingDirectory(this, windowTitle(), directory().path()); |
| 96 | |
| 97 | if (dir == directory().path()) dir = ""; |
| 98 | |
| 99 | if (!dir.isEmpty()) files << dir; |
| 100 | |
| 101 | return !dir.isEmpty(); |
| 102 | #else |
| 103 | if (!placeWindow()) return false; |
| 104 | // QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks ? |
| 105 | setFileMode(QFileDialog::DirectoryOnly); |
| 106 | return exec(); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) |
| 111 | QStringList QNapiOpenDialog::selectedFiles() const { return files; } |
| 112 | #endif |
| 113 | |
| 114 | bool QNapiOpenDialog::placeWindow() { |
| 115 | if (isVisible()) { |
| 116 | raise(); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | QRect position = frameGeometry(); |
| 121 | position.moveCenter(QDesktopWidget().availableGeometry().center()); |
| 122 | move(position.topLeft()); |
| 123 | |
| 124 | return true; |
| 125 | } |