2 #include "filetablemodel.h"
\r
6 #include <QApplication>
\r
10 #include <windows.h>
\r
13 FileTableModel::FileTableModel(QObject *parent) :
\r
14 QAbstractTableModel(parent),
\r
22 m_NormalTextBrush(),
\r
31 bool FileTableModel::setPath(const QString &path)
\r
33 if (!QFileInfo::exists(path)) {
\r
34 qDebug() << "パスが存在しません";
\r
38 QFileInfo info(path);
\r
39 if (!info.isDir()) {
\r
40 qDebug() << "フォルダではありません";
\r
47 m_dir.setPath(path);
\r
48 m_fileInfoList = m_dir.entryInfoList();
\r
49 m_checkStates.clear();
\r
51 if (m_fileInfoList.isEmpty()) {
\r
52 qDebug() << "ファイルリストを取得できません";
\r
56 QFileInfoList::iterator it;
\r
57 bool removeDotDot = false;
\r
58 for (it = m_fileInfoList.begin(); it != m_fileInfoList.end(); ) {
\r
60 if (!(filter() & QDir::System)){
\r
61 DWORD dwFlags = ::GetFileAttributes(
\r
62 it->absoluteFilePath().toStdWString().c_str());
\r
63 if (dwFlags != DWORD(-1) && it->fileName() != ".." &&
\r
64 ((dwFlags & FILE_ATTRIBUTE_SYSTEM) == FILE_ATTRIBUTE_SYSTEM))
\r
66 qDebug() << it->fileName() << " is system file.";
\r
67 it = m_fileInfoList.erase(it);
\r
72 if (it->fileName() == "..") {
\r
73 if (m_dir.isRoot()) {
\r
74 qDebug() << m_dir.absolutePath() << " is root.";
\r
75 it = m_fileInfoList.erase(it);
\r
76 removeDotDot = true;
\r
82 // ソート方法によらず、".."は必ず先頭にする
\r
83 if (!removeDotDot) {
\r
84 QFileInfoList::iterator itRoot = m_fileInfoList.end();
\r
85 for (it = m_fileInfoList.begin(); it != m_fileInfoList.end(); it++) {
\r
86 if (it->fileName() == "..") {
\r
91 if (itRoot != m_fileInfoList.end()) {
\r
92 QFileInfo info(*itRoot);
\r
93 m_fileInfoList.erase(itRoot);
\r
94 m_fileInfoList.push_front(info);
\r
99 m_checkStates.resize(m_fileInfoList.size());
\r
100 m_checkStates.fill(Qt::Unchecked);
\r
103 delete m_fsWatcher;
\r
105 m_fsWatcher = new QFileSystemWatcher(this);
\r
106 m_fsWatcher->addPath(path);
\r
107 connect(m_fsWatcher, SIGNAL(directoryChanged(QString)),
\r
108 this, SIGNAL(listUpdated()));
\r
112 emit rootChanged(m_dir.absolutePath());
\r
115 return !m_fileInfoList.isEmpty();
\r
118 Qt::CheckState FileTableModel::checkState(const QModelIndex &index) const
\r
120 return m_checkStates[index.row()];
\r
123 void FileTableModel::setCheckState(const QModelIndex &index, Qt::CheckState state)
\r
126 m_checkStates[index.row()] = state;
\r
127 if (state == Qt::Checked && index.row() == 0 &&
\r
128 m_fileInfoList[0].fileName() == "..")
\r
130 m_checkStates[0] = Qt::Unchecked;
\r
133 emit dataChanged(index, this->index(index.row(), 3));
\r
138 void FileTableModel::setCheckStateAll(Qt::CheckState state)
\r
141 m_checkStates.fill(state);
\r
142 if (state == Qt::Checked && m_fileInfoList.size() > 1 &&
\r
143 m_fileInfoList[0].fileName() == "..")
\r
145 m_checkStates[0] = Qt::Unchecked;
\r
153 bool FileTableModel::isDir(const QModelIndex &index) const
\r
155 return m_fileInfoList[index.row()].isDir();
\r
158 const QString FileTableModel::absoluteFilePath(const QModelIndex &index) const
\r
160 if (!index.isValid()) {
\r
161 qDebug() << "index is invalid.";
\r
164 return m_dir.absoluteFilePath(m_fileInfoList[index.row()].fileName());
\r
167 QFileInfo FileTableModel::fileInfo(const QModelIndex &index) const
\r
169 return m_fileInfoList[index.row()];
\r
172 void FileTableModel::updateAppearance()
\r
174 QSettings settings;
\r
175 QPalette palette(QApplication::palette("QTableView"));
\r
177 m_font = settings.value(IniKey_ViewFont).value<QFont>();
\r
178 m_NormalBrush = QBrush(settings.value(IniKey_ViewColorBgNormal).value<QColor>());
\r
179 m_NormalTextBrush = QBrush(settings.value(IniKey_ViewColorFgNormal).value<QColor>());
\r
180 m_MarkBrush = QBrush(settings.value(IniKey_ViewColorBgMark).value<QColor>());
\r
181 m_MarkTextBrush = QBrush(settings.value(IniKey_ViewColorFgMark).value<QColor>());
\r
182 m_SystemBrush = QBrush(settings.value(IniKey_ViewColorFgSystem).value<QColor>());
\r
183 m_HiddenBrush = QBrush(settings.value(IniKey_ViewColorFgHidden).value<QColor>());
\r
184 m_ReadonlyBrush = QBrush(settings.value(IniKey_ViewColorFgReadonly).value<QColor>());
\r
185 m_ReadonlyBrush = QBrush(settings.value(IniKey_ViewColorFgReadonly).value<QColor>());
\r
191 void FileTableModel::stateChanged()
\r
195 quint64 totalSize = 0;
\r
196 for (int n = 0; n < m_checkStates.size(); n++) {
\r
197 if (m_checkStates[n] == Qt::Checked) {
\r
198 if (m_fileInfoList[n].isDir()) {
\r
203 totalSize += m_fileInfoList[n].size();
\r
208 emit stateChanged(numFolder, numFile, totalSize);
\r
211 QFileInfoList FileTableModel::checkedItems() const
\r
213 QFileInfoList list;
\r
214 for (int n = 0; n < m_checkStates.size(); n++) {
\r
215 if (m_fileInfoList[n].fileName() != ".." &&
\r
216 m_checkStates[n] == Qt::Checked)
\r
218 list.append(m_fileInfoList[n]);
\r
224 int FileTableModel::rowCount(const QModelIndex &parent) const
\r
227 return m_fileInfoList.size();
\r
230 int FileTableModel::columnCount(const QModelIndex &parent) const
\r
236 QVariant FileTableModel::data(const QModelIndex &index, int role) const
\r
238 if (!index.isValid()) {
\r
242 const QFileInfo &info = m_fileInfoList.at(index.row());
\r
243 int checked = m_checkStates.at(index.row());
\r
246 case Qt::DisplayRole:
\r
247 switch (index.column()) {
\r
248 case 0: // チェックボックス
\r
249 return QString("");
\r
251 return info.fileName();
\r
253 if (info.isDir()) {
\r
254 return QString("<DIR>");
\r
257 return FilesizeToString(info.size());
\r
261 return info.lastModified().toString("yy/MM/dd hh:mm");
\r
265 case Qt::DecorationRole:
\r
266 if (index.column() == 1) {
\r
267 if (info.fileName() == "..") {
\r
268 return QIcon(":/images/Up.png");
\r
271 // infoを使うと、正しいアイコンが取れない場合がある…なぜ?
\r
272 return m_IconFactory.icon(QFileInfo(info.absoluteFilePath()));
\r
280 case Qt::TextAlignmentRole:
\r
281 switch (index.column()) {
\r
284 return Qt::AlignLeft + Qt::AlignVCenter;
\r
287 return Qt::AlignRight + Qt::AlignVCenter;
\r
291 case Qt::BackgroundRole:
\r
293 return m_MarkBrush;
\r
295 return m_NormalBrush;
\r
297 case Qt::ForegroundRole:
\r
299 return m_MarkTextBrush;
\r
303 DWORD dwFlags = ::GetFileAttributes(
\r
304 info.absoluteFilePath().toStdWString().c_str());
\r
305 if (dwFlags != DWORD(-1) && (dwFlags & FILE_ATTRIBUTE_SYSTEM) == FILE_ATTRIBUTE_SYSTEM)
\r
307 return m_SystemBrush;
\r
311 if (info.isHidden() && info.fileName() != "..") {
\r
312 return m_HiddenBrush;
\r
314 if (!info.isWritable()) {
\r
315 return m_ReadonlyBrush;
\r
317 return m_NormalTextBrush;
\r
319 case Qt::CheckStateRole:
\r
320 if (index.column() == 0 && info.fileName() != "..") {
\r
329 QVariant FileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
\r
331 if (role == Qt::DisplayRole) {
\r
332 if (orientation == Qt::Horizontal) {
\r
334 case 0: return QString("");
\r
335 case 1: return tr("名前");
\r
336 case 2: return tr("サイズ");
\r
337 case 3: return tr("更新日時");
\r
344 Qt::ItemFlags FileTableModel::flags(const QModelIndex &index) const
\r
346 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
\r
347 if (!index.isValid()) {
\r
348 flags |= Qt::ItemIsDropEnabled;
\r
350 else if (m_fileInfoList[index.row()].fileName() != "..") {
\r
351 if (index.column() == 0) {
\r
352 flags |= Qt::ItemIsUserCheckable;
\r
354 flags |= Qt::ItemIsDropEnabled;
\r
359 bool FileTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
\r
361 if (!index.isValid()) {
\r
366 case Qt::CheckStateRole:
\r
367 if (index.column() == 0) {
\r
368 m_checkStates[index.row()] = static_cast<Qt::CheckState>(value.toInt());
\r
369 emit dataChanged(index, this->index(index.row(), 3));
\r
379 Qt::DropActions FileTableModel::supportedDropActions() const
\r
381 return Qt::CopyAction | Qt::MoveAction;
\r
384 QStringList FileTableModel::mimeTypes() const
\r
388 types << "text/uri-list";
\r