OSDN Git Service

Ver0.14
[gefu/Gefu.git] / filetablemodel.cpp
index 0143924..6676531 100644 (file)
@@ -5,10 +5,7 @@
 #include <QDebug>\r
 #include <QApplication>\r
 #include <QSettings>\r
-#include <QMenuBar>\r
-#include <QMimeData>\r
-#include <QUrl>\r
-#include <QMessageBox>\r
+#include <QPalette>\r
 #ifdef Q_OS_WIN32\r
     #include <windows.h>\r
 #endif\r
@@ -29,6 +26,8 @@ FileTableModel::FileTableModel(QObject *parent) :
     m_HiddenBrush(),\r
     m_ReadonlyBrush()\r
 {\r
+    // デフォルトフィルタを設定する\r
+    setFilter(QDir::NoDot | QDir::AllDirs | QDir::Files);\r
 }\r
 \r
 bool FileTableModel::setPath(const QString &path)\r
@@ -56,18 +55,8 @@ bool FileTableModel::setPath(const QString &path)
         qDebug() << path;\r
     }\r
     else {\r
-        QFileInfoList::iterator it, itRoot = m_fileInfoList.end();\r
+        QFileInfoList::iterator it;\r
         for (it = m_fileInfoList.begin(); it != m_fileInfoList.end(); ) {\r
-            if (it->fileName() == "..") {\r
-                if (m_dir.isRoot()) {\r
-                   it = m_fileInfoList.erase(it);\r
-                    itRoot = m_fileInfoList.end();\r
-                    continue;\r
-                }\r
-                else {\r
-                    itRoot = it;\r
-                }\r
-            }\r
 #ifdef Q_OS_WIN32\r
             if (!(filter() & QDir::System)){\r
                 DWORD dwFlags = ::GetFileAttributes(\r
@@ -81,10 +70,24 @@ bool FileTableModel::setPath(const QString &path)
                 }\r
             }\r
 #endif\r
+            if (it->fileName() == "..") {\r
+                if (m_dir.isRoot()) {\r
+                    qDebug() << m_dir.absolutePath() << " is root.";\r
+                    it = m_fileInfoList.erase(it);\r
+                    continue;\r
+                }\r
+            }\r
             it++;\r
         }\r
         // ソート方法によらず、".."は必ず先頭にする\r
-        if (itRoot != m_fileInfoList.begin() && itRoot != m_fileInfoList.end()) {\r
+        QFileInfoList::iterator itRoot = m_fileInfoList.end();\r
+        for (it = m_fileInfoList.begin(); it != m_fileInfoList.end(); it++) {\r
+            if (it->fileName() == "..") {\r
+                itRoot = it;\r
+                break;\r
+            }\r
+        }\r
+        if (itRoot != m_fileInfoList.end()) {\r
             QFileInfo info(*itRoot);\r
             m_fileInfoList.erase(itRoot);\r
             m_fileInfoList.push_front(info);\r
@@ -100,14 +103,11 @@ bool FileTableModel::setPath(const QString &path)
     m_fsWatcher = new QFileSystemWatcher(this);\r
     m_fsWatcher->addPath(path);\r
     connect(m_fsWatcher, SIGNAL(directoryChanged(QString)),\r
-//            this, SLOT(refresh()));\r
-            this, SIGNAL(listUpdated()));\r
+            this, SLOT(directoryChange(QString)));\r
 \r
     endResetModel();\r
 \r
-    emit rootChanged(m_dir.absolutePath());\r
-    stateChanged();\r
-\r
+    emit dataChanged(QModelIndex(), QModelIndex());\r
     return !m_fileInfoList.isEmpty();\r
 }\r
 \r
@@ -127,8 +127,6 @@ void FileTableModel::setCheckState(const QModelIndex &index, Qt::CheckState stat
     }\r
     endResetModel();;\r
     emit dataChanged(index, this->index(index.row(), 3));\r
-\r
-    stateChanged();\r
 }\r
 \r
 void FileTableModel::setCheckStateAll(Qt::CheckState state)\r
@@ -141,71 +139,39 @@ void FileTableModel::setCheckStateAll(Qt::CheckState state)
         m_checkStates[0] = Qt::Unchecked;\r
     }\r
     endResetModel();\r
-\r
-    stateChanged();\r
-}\r
-\r
-\r
-bool FileTableModel::isDir(const QModelIndex &index) const\r
-{\r
-    return m_fileInfoList[index.row()].isDir();\r
-}\r
-\r
-const QString FileTableModel::absoluteFilePath(const QModelIndex &index) const\r
-{\r
-    return m_dir.absoluteFilePath(m_fileInfoList[index.row()].fileName());\r
+    emit dataChanged(QModelIndex(), QModelIndex());\r
 }\r
 \r
 QFileInfo FileTableModel::fileInfo(const QModelIndex &index) const\r
 {\r
+    if (!index.isValid()) {\r
+        qDebug() << "fileInfo() : index is invalid.";\r
+        return QFileInfo();\r
+    }\r
     return m_fileInfoList[index.row()];\r
 }\r
 \r
-#define Brush(x, y, z)  QBrush((x).value((y), (z)).value<QColor>());\r
-\r
-\r
 void FileTableModel::updateAppearance()\r
 {\r
     QSettings settings;\r
     QPalette palette(QApplication::palette("QTableView"));\r
 \r
-    m_font = settings.value(IniKey_ViewFont, QApplication::font()).value<QFont>();\r
-    m_NormalBrush = Brush(settings, IniKey_ViewColorBgNormal, palette.base());\r
-    m_NormalTextBrush = Brush(settings, IniKey_ViewColorFgNormal, palette.text());\r
-    m_MarkBrush = Brush(settings, IniKey_ViewColorBgMark, DefaultMarkBgColor);\r
-    m_MarkTextBrush = Brush(settings, IniKey_ViewColorFgMark, DefaultMarkFgColor);\r
-    m_SystemBrush = Brush(settings, IniKey_ViewColorFgSystem, DefaultSystemColor);\r
-    m_HiddenBrush = Brush(settings, IniKey_ViewColorFgHidden, DefaultHiddenColor);\r
-    m_ReadonlyBrush = Brush(settings, IniKey_ViewColorFgReadonly, DefaultReadonlyColor);\r
-\r
-    beginResetModel();\r
-    endResetModel();\r
+    m_font = settings.value(IniKey_ViewFont).value<QFont>();\r
+    m_NormalBrush = QBrush(settings.value(IniKey_ViewColorBgNormal).value<QColor>());\r
+    m_NormalTextBrush = QBrush(settings.value(IniKey_ViewColorFgNormal).value<QColor>());\r
+    m_MarkBrush = QBrush(settings.value(IniKey_ViewColorBgMark).value<QColor>());\r
+    m_MarkTextBrush = QBrush(settings.value(IniKey_ViewColorFgMark).value<QColor>());\r
+    m_SystemBrush = QBrush(settings.value(IniKey_ViewColorFgSystem).value<QColor>());\r
+    m_HiddenBrush = QBrush(settings.value(IniKey_ViewColorFgHidden).value<QColor>());\r
+    m_ReadonlyBrush = QBrush(settings.value(IniKey_ViewColorFgReadonly).value<QColor>());\r
+    m_ReadonlyBrush = QBrush(settings.value(IniKey_ViewColorFgReadonly).value<QColor>());\r
 }\r
 \r
-//void FileTableModel::refresh()\r
-//{\r
-//    setPath(m_dir.absolutePath());\r
-\r
-//}\r
-\r
-void FileTableModel::stateChanged()\r
+void FileTableModel::directoryChange(const QString &path)\r
 {\r
-    int numFolder = 0;\r
-    int numFile = 0;\r
-    quint64 totalSize = 0;\r
-    for (int n = 0; n < m_checkStates.size(); n++) {\r
-        if (m_checkStates[n] == Qt::Checked) {\r
-            if (m_fileInfoList[n].isDir()) {\r
-                numFolder++;\r
-            }\r
-            else {\r
-                numFile++;\r
-                totalSize += m_fileInfoList[n].size();\r
-            }\r
-        }\r
-    }\r
+    qDebug() << "FileTableModel::directoryChange";\r
 \r
-    emit stateChanged(numFolder, numFile, totalSize);\r
+    setPath(path);\r
 }\r
 \r
 QFileInfoList FileTableModel::checkedItems() const\r
@@ -230,7 +196,7 @@ int FileTableModel::rowCount(const QModelIndex &parent) const
 int FileTableModel::columnCount(const QModelIndex &parent) const\r
 {\r
     Q_UNUSED(parent);\r
-    return 4;\r
+    return 5;\r
 }\r
 \r
 QVariant FileTableModel::data(const QModelIndex &index, int role) const\r
@@ -248,8 +214,30 @@ QVariant FileTableModel::data(const QModelIndex &index, int role) const
         case 0: // チェックボックス\r
             return QString("");\r
         case 1: // ファイル名\r
-            return info.fileName();\r
-        case 2: // サイズ\r
+            if (info.isDir()) {\r
+                return info.fileName();\r
+            }\r
+            if (info.fileName().left(1) == ".") {   // "."で始まるファイル\r
+                int pos = info.fileName().lastIndexOf(".");\r
+                if (pos == 0) {\r
+                    return info.fileName();\r
+                }\r
+            }\r
+            return info.completeBaseName();\r
+\r
+        case 2: // 拡張子\r
+            if (info.isDir()) {\r
+                return QString();\r
+            }\r
+            if (info.fileName().left(1) == ".") {   // "."で始まるファイル\r
+                int pos = info.fileName().lastIndexOf(".");\r
+                if (pos == 0) {\r
+                    return QString();\r
+                }\r
+            }\r
+            return info.suffix();\r
+\r
+        case 3: // サイズ\r
             if (info.isDir()) {\r
                 return QString("<DIR>");\r
             }\r
@@ -257,7 +245,8 @@ QVariant FileTableModel::data(const QModelIndex &index, int role) const
                 return FilesizeToString(info.size());\r
             }\r
             break;\r
-        case 3:\r
+\r
+        case 4: // 更新日時\r
             return info.lastModified().toString("yy/MM/dd hh:mm");\r
         }\r
         break;\r
@@ -279,12 +268,11 @@ QVariant FileTableModel::data(const QModelIndex &index, int role) const
 \r
     case Qt::TextAlignmentRole:\r
         switch (index.column()) {\r
-        case 0:\r
-        case 1:\r
-            return Qt::AlignLeft + Qt::AlignVCenter;\r
-        case 2:\r
         case 3:\r
+        case 4:\r
             return Qt::AlignRight + Qt::AlignVCenter;\r
+        default:\r
+            return Qt::AlignLeft + Qt::AlignVCenter;\r
         }\r
         break;\r
 \r
@@ -333,8 +321,9 @@ QVariant FileTableModel::headerData(int section, Qt::Orientation orientation, in
             switch (section) {\r
             case 0: return QString("");\r
             case 1: return tr("名前");\r
-            case 2: return tr("サイズ");\r
-            case 3: return tr("更新日時");\r
+            case 2: return tr("拡張子");\r
+            case 3: return tr("サイズ");\r
+            case 4: return tr("更新日時");\r
             }\r
         }\r
     }\r
@@ -351,8 +340,6 @@ Qt::ItemFlags FileTableModel::flags(const QModelIndex &index) const
         if (index.column() == 0) {\r
             flags |= Qt::ItemIsUserCheckable;\r
         }\r
-//        TODO\r
-//        flags |= Qt::ItemIsDragEnabled;\r
         flags |= Qt::ItemIsDropEnabled;\r
     }\r
     return flags;\r
@@ -369,7 +356,6 @@ bool FileTableModel::setData(const QModelIndex &index, const QVariant &value, in
         if (index.column() == 0) {\r
             m_checkStates[index.row()] = static_cast<Qt::CheckState>(value.toInt());\r
             emit dataChanged(index, this->index(index.row(), 3));\r
-            stateChanged();\r
             return true;\r
         }\r
         break;\r
@@ -391,29 +377,3 @@ QStringList FileTableModel::mimeTypes() const
 \r
     return types;\r
 }\r
-\r
-bool FileTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)\r
-{\r
-    Q_UNUSED(parent);\r
-\r
-    qDebug() << action << row << column;\r
-\r
-    if (action == Qt::IgnoreAction)\r
-        return true;\r
-\r
-    if (!data->hasFormat("text/uri-list"))\r
-        return false;\r
-\r
-    if (column > 0)\r
-        return false;\r
-\r
-    QString msg;\r
-    msg = "ドロップを検知しました。<br/>";\r
-    foreach (const QUrl &url, data->urls()) {\r
-        msg += url.toLocalFile() + "<br/>";\r
-        qDebug() << url.toLocalFile();\r
-    }\r
-    QMessageBox::information(NULL, "info", msg);\r
-\r
-    return true;\r
-}\r