OSDN Git Service

Smarter method to edit file informartion of files in the list
[lamexp/LameXP.git] / src / Model_FileList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
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 program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_FileList.h"
23
24 #include <QFileInfo>
25
26 ////////////////////////////////////////////////////////////
27 // Constructor & Destructor
28 ////////////////////////////////////////////////////////////
29
30 FileListModel::FileListModel(void)
31         : m_fileIcon(":/icons/page_white_cd.png")
32 {
33         m_fileList.append(AudioFileModel("C:/Music/Buckethead - Crime Slunk Scene/The Fairy and the Devil.ogg", "The Fairy and the Devil"));
34 }
35
36 FileListModel::~FileListModel(void)
37 {
38 }
39
40 ////////////////////////////////////////////////////////////
41 // Public Functions
42 ////////////////////////////////////////////////////////////
43
44 int FileListModel::columnCount(const QModelIndex &parent) const
45 {
46         return 2;
47 }
48
49 int FileListModel::rowCount(const QModelIndex &parent) const
50 {
51         return m_fileList.count();
52 }
53
54 QVariant FileListModel::data(const QModelIndex &index, int role) const
55 {
56         if((role == Qt::DisplayRole || role == Qt::ToolTipRole) && index.row() < m_fileList.count() && index.row() >= 0)
57         {
58                 switch(index.column())
59                 {
60                 case 0:
61                         return m_fileList.at(index.row()).fileName();
62                         break;
63                 case 1:
64                         return m_fileList.at(index.row()).filePath();
65                         break;
66                 default:
67                         return QVariant();
68                         break;
69                 }               
70         }
71         else if(role == Qt::DecorationRole && index.column() == 0)
72         {
73                 return m_fileIcon;
74         }
75         else
76         {
77                 return QVariant();
78         }
79 }
80
81 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
82 {
83         if(role == Qt::DisplayRole)
84         {
85                 if(orientation == Qt::Horizontal)
86                 {
87                         switch(section)
88                         {
89                         case 0:
90                                 return QVariant("Title");
91                                 break;
92                         case 1:
93                                 return QVariant("Full Path");
94                                 break;
95                         default:
96                                 return QVariant();
97                                 break;
98                         }
99                 }
100                 else
101                 {
102                         if(m_fileList.count() < 10)
103                         {
104                                 return QVariant(QString().sprintf("%d", section + 1));
105                         }
106                         else if(m_fileList.count() < 100)
107                         {
108                                 return QVariant(QString().sprintf("%02d", section + 1));
109                         }
110                         else if(m_fileList.count() < 1000)
111                         {
112                                 return QVariant(QString().sprintf("%03d", section + 1));
113                         }
114                         else
115                         {
116                                 return QVariant(QString().sprintf("%04d", section + 1));
117                         }
118                 }
119         }
120         else
121         {
122                 return QVariant();
123         }
124 }
125
126 void FileListModel::addFile(const QString &filePath)
127 {
128         QFileInfo fileInfo(filePath);
129
130         for(int i = 0; i < m_fileList.count(); i++)
131         {
132                 if(m_fileList.at(i).filePath().compare(fileInfo.absoluteFilePath(), Qt::CaseInsensitive) == 0)
133                 {
134                         return;
135                 }
136         }
137         
138         beginResetModel();
139         m_fileList.append(AudioFileModel(fileInfo.absoluteFilePath(), fileInfo.baseName()));
140         endResetModel();
141 }
142
143 void FileListModel::addFile(const AudioFileModel &file)
144 {
145         for(int i = 0; i < m_fileList.count(); i++)
146         {
147                 if(m_fileList.at(i).filePath().compare(file.filePath(), Qt::CaseInsensitive) == 0)
148                 {
149                         return;
150                 }
151         }
152         
153         beginResetModel();
154         m_fileList.append(file);
155         endResetModel();
156 }
157
158 bool FileListModel::removeFile(const QModelIndex &index)
159 {
160         if(index.row() >= 0 && index.row() < m_fileList.count())
161         {
162                 beginResetModel();
163                 m_fileList.removeAt(index.row());
164                 endResetModel();
165                 return true;
166         }
167         else
168         {
169                 return false;
170         }
171 }
172
173 void FileListModel::clearFiles(void)
174 {
175         beginResetModel();
176         m_fileList.clear();
177         endResetModel();
178 }
179
180 bool FileListModel::moveFile(const QModelIndex &index, int delta)
181 {
182         if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
183         {
184                 beginResetModel();
185                 m_fileList.move(index.row(), index.row() + delta);
186                 endResetModel();
187                 return true;
188         }
189         else
190         {
191                 return false;
192         }
193 }
194
195 AudioFileModel FileListModel::getFile(const QModelIndex &index)
196 {
197         if(index.row() >= 0 && index.row() < m_fileList.count())
198         {
199                 return m_fileList.at(index.row());
200         }
201         else
202         {
203                 return AudioFileModel();
204         }
205 }
206
207 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
208 {
209         return m_fileList[index.row()];
210 }
211
212 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
213 {
214         if(index.row() >= 0 && index.row() < m_fileList.count())
215         {
216                 beginResetModel();
217                 m_fileList.replace(index.row(), audioFile);
218                 endResetModel();
219                 return true;
220         }
221         else
222         {
223                 return false;
224         }
225 }