1 /**
2  * projectM-qt -- Qt4 based projectM GUI
3  * Copyright (C)2003-2004 projectM Team
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  * See 'LICENSE.txt' included within this release
19  *
20  */
21 
22 
23 
24 #ifndef QPLAYLIST_FILEDIALOG_HPP
25 #define QPLAYLIST_FILEDIALOG_HPP
26 
27 #include <QFileDialog>
28 #include <QStringList>
29 #include <QtDebug>
30 
31  class QPlaylistFileDialog : public QFileDialog
32  {
33      Q_OBJECT        // must include this if you use Qt signals/slots
34 
35  public:
36      static QString OPEN_PLAYLIST_TITLE;
37      static QString OPEN_PLAYLIST_OR_DIRECTORY_TITLE;
38      static QString SAVE_PLAYLIST_TITLE;
39 
QPlaylistFileDialog(QWidget * parent=0)40      inline QPlaylistFileDialog(QWidget * parent = 0):
41 		QFileDialog(parent, OPEN_PLAYLIST_OR_DIRECTORY_TITLE, QString()), m_directorySelectAllowed(true), m_fileSelectAllowed(true) {
42 
43 	     this->setOption(QFileDialog::DontUseNativeDialog);
44 
45 	     updateFileMode(selectedFiles());
46 
47 	     //connect(this, SIGNAL(filesSelected(const QStringList&)),
48 		//     this, SLOT(updateFileMode(const QStringList&)));
49 
50 	     connect(this, SIGNAL(currentChanged(const QString&)),
51 		     this, SLOT(updateFileMode(const QString&)));
52 	}
53 
isPlaylistSaveMode()54 	inline bool isPlaylistSaveMode() {
55 		return this->acceptMode() == QFileDialog::AcceptSave;
56 	}
57 
setPlaylistSaveMode(bool isSaveMode)58 	inline void setPlaylistSaveMode(bool isSaveMode) {
59 		if (isSaveMode) {
60 			this->setAcceptMode(QFileDialog::AcceptSave);
61 			updateWindowTitle();
62 			updateFileMode(selectedFiles());
63 		}
64 		else {
65 			this->setAcceptMode(QFileDialog::AcceptOpen);
66 			updateWindowTitle();
67 			updateFileMode(selectedFiles());
68 		}
69 	}
70 
setAllowDirectorySelect(bool isAllowed)71 	inline void setAllowDirectorySelect(bool isAllowed) {
72 		m_directorySelectAllowed = isAllowed;
73 		updateFileMode(selectedFiles());
74 		updateWindowTitle();
75 	}
76 
setAllowFileSelect(bool isAllowed)77 	inline void setAllowFileSelect(bool isAllowed) {
78 		m_fileSelectAllowed = isAllowed;
79 		updateFileMode(selectedFiles());
80 		updateWindowTitle();
81 	}
82 
83 
isFileSelectAllowed() const84 	inline bool isFileSelectAllowed() const {
85 		return m_fileSelectAllowed;
86 	}
87 
88 
isDirectorySelectAllowed() const89 	inline bool isDirectorySelectAllowed() const {
90 		return m_directorySelectAllowed;
91 	}
92 
93 
94 
~QPlaylistFileDialog()95        ~QPlaylistFileDialog() { }
96 
97 	 private:
98 
99 		 bool m_directorySelectAllowed;
100 		 bool m_fileSelectAllowed;
getFilter()101 		 QString getFilter() {
102 			 QString filter;
103 			 if (isDirectorySelectAllowed()) {
104 
105 				 filter += "Directories";
106 			 }
107 
108 			 if (isFileSelectAllowed()) {
109 				 if (filter != QString())
110 					 filter += " and ";
111 
112 				 filter += "Preset Playlists (*.ppl)";
113 			 }
114 
115 			 return filter;
116 		 }
117 
updateWindowTitle()118 		 void updateWindowTitle() {
119 			 if (isPlaylistSaveMode())
120 				 setWindowTitle(SAVE_PLAYLIST_TITLE);
121 			 else {
122 				 if (isDirectorySelectAllowed() && isFileSelectAllowed())
123 					 setWindowTitle(OPEN_PLAYLIST_OR_DIRECTORY_TITLE);
124 				 else
125 					 setWindowTitle(OPEN_PLAYLIST_TITLE);
126 			 }
127 		 }
128 
129  private slots:
130 
updateFileMode(const QString fileName)131 	 void updateFileMode(const QString fileName) {
132 
133 
134 		if (fileName == QString()) {
135 			 if (isPlaylistSaveMode())
136 				 this->setFileMode(QFileDialog::AnyFile);
137 			 else
138 				 this->setFileMode(QFileDialog::ExistingFile);
139 		}
140 
141 		else if (QFileInfo(fileName).isDir()) {
142 			if (isPlaylistSaveMode())
143 				this->setFileMode(QFileDialog::AnyFile);
144 			 else if (isDirectorySelectAllowed())
145 			 	this->setFileMode(QFileDialog::Directory);
146 			 else
147 				 this->setFileMode(QFileDialog::ExistingFile);
148 		}
149          	else if (QFileInfo(fileName).isFile()) {
150 
151 			 if (isPlaylistSaveMode())
152 				 this->setFileMode(QFileDialog::AnyFile);
153 			 else if (isFileSelectAllowed())
154 				this->setFileMode(QFileDialog::ExistingFile);
155 			 else
156 				 this->setFileMode(QFileDialog::Directory);
157 		}
158 
159         this->setFilter(filter());
160 	 }
161 
updateFileMode(const QStringList & selectedFiles)162 	 void updateFileMode(const QStringList & selectedFiles) {
163 		 if (selectedFiles.empty())
164 		 	updateFileMode(QString());
165 		 else
166 		 	updateFileMode(selectedFiles[0]);
167 	}
168 };
169 
170 
171 #endif
172