OSDN Git Service

ファイル読込の中止処理を修正
[gefu/Gefu.git] / folderpanel.cpp
index becb113..775079d 100644 (file)
@@ -1,7 +1,6 @@
-#include "common.h"
+#include "global.h"
+#include "preferences.h"
 #include "mainwindow.h"
-#include "searchbox.h"
-#include "locationbox.h"
 #include "folderpanel.h"
 #include "ui_folderpanel.h"
 
 #include <QSettings>
 #include <QStatusBar>
 
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::FolderPanel
+/// \param parent   親ウィジェット
+///
+/// コンストラクタ
+///
 FolderPanel::FolderPanel(QWidget *parent) :
     QWidget(parent),
     ui(new Ui::FolderPanel),
     m_mainWnd(NULL)
 {
     ui->setupUi(this);
+    ui->searchBox->setText("");
+    ui->searchBox->setVisible(false);
+    ui->thumbnailView->setVisible(false);
 }
 
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::~FolderPanel
+///
+/// デストラクタ
+///
 FolderPanel::~FolderPanel()
 {
     delete ui;
 }
 
-void FolderPanel::initialize(MainWindow *mainWnd)
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::initialize
+/// \param w    メインウィンドウオブジェクト
+///
+/// 初期化処理を行います。
+///
+void FolderPanel::initialize(MainWindow *w)
 {
     qDebug() << "FolderPanel::initialize();";
-    m_mainWnd = mainWnd;
-
-    // シグナル&スロット
-    connect(ui->searchBox, SIGNAL(textEdited(QString)), this, SLOT(searchItem(QString)));
-    connect(ui->folderView, SIGNAL(doubleClicked(QModelIndex)), mainWnd, SLOT(open(QModelIndex)));
-    connect(ui->folderView, SIGNAL(dropAccepted(QFileInfoList)), mainWnd, SLOT(dropAccept(QFileInfoList)));
-    connect(ui->folderView, SIGNAL(currentChanged(QFileInfo)), mainWnd, SLOT(currentChange(QFileInfo)));
-    connect(ui->folderView, SIGNAL(requestContextMenu(QContextMenuEvent*)), mainWnd, SLOT(showContextMenu(QContextMenuEvent*)));
-    connect(ui->folderView, SIGNAL(retrieveStarted(QString)), ui->locationBox, SLOT(setText(QString)));
-    connect(ui->folderView, SIGNAL(dataChanged()), this, SLOT(dataChange()));
-    connect(ui->folderView, SIGNAL(itemFound()), this, SLOT(itemFound()));
-    connect(ui->folderView, SIGNAL(itemNotFound()), this, SLOT(itemNotFound()));
-    connect(ui->bookmarkBtn, SIGNAL(clicked()), this, SLOT(addBookmark()));
-
-    // 初期状態では検索ボックスは非表示
-    ui->searchBox->setVisible(false);
-
-    // ロケーションボックスを初期化する
-    ui->locationBox->initialize();
 
     // フォルダビューを初期化する
-    ui->folderView->initialize(mainWnd);
-}
+    ui->folderView->initialize(w);
+    // サムネイルビューを初期化する
+    ui->thumbnailView->initialize(w);
 
-LocationBox *FolderPanel::locationBox() const
-{
-    return ui->locationBox;
+    connect(ui->bookmarkBtn, SIGNAL(clicked()), w, SLOT(onAddBookmark()));
 }
 
-FolderView *FolderPanel::folderView() const
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::itemView
+/// \return アイテムビューを返します。
+///
+QAbstractItemView *FolderPanel::itemView() const
 {
+    if (ui->thumbnailView->isVisible()) {
+        return ui->thumbnailView;
+    }
     return ui->folderView;
 }
 
-SearchBox *FolderPanel::searchBox() const
-{
-    return ui->searchBox;
-}
-
-QLabel *FolderPanel::filterLabel() const
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model
+/// \return 関連付けられたフォルダモデルを返します。
+///
+FolderModel *FolderPanel::model() const
 {
-    return ui->filterLabel;
+    return static_cast<FolderModel*>(ui->folderView->model());
 }
 
-void FolderPanel::setNameFilters(const QString &filters)
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::setModel
+/// \param m    設定するモデル
+///
+/// モデルを設定します。
+///
+void FolderPanel::setModel(FolderModel *m)
 {
-    QStringList list = filters.split(" ", QString::SkipEmptyParts);
-    if (list.isEmpty()) {
-        list << "*";
+    qDebug() << "FolderPanel::setModel()" << m;
+    if (model()) {
+        model()->disconnect(this);
     }
-    ui->folderView->setNameFilters(list);
-    showNameFilters();
-}
 
-void FolderPanel::showNameFilters()
-{
-    ui->filterLabel->setText(tr("フィルタ:") + ui->folderView->nameFilters().join(" "));
+    ui->folderView->setModel(m);
+    ui->thumbnailView->setModel(m);
+
+    if (model()) {
+        connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
+                this, SLOT(model_dataChanged(QModelIndex,QModelIndex)));
+        connect(model(), SIGNAL(modelReset()), this, SLOT(model_Reset()));
+
+        model_Reset();
+        model_dataChanged(QModelIndex(), QModelIndex());
+    }
 }
 
-void FolderPanel::dataChange()
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::toggleSearch
+/// \param checked  メニューのチェック状態
+///
+/// 検索ボックスの表示/非表示を切り替えます。
+///
+void FolderPanel::toggleSearch(bool checked)
 {
-    qDebug() << "FolderPanel::dataChange();";
+    qDebug() << "FolderPanel::toggleSearch()" << checked;
+    if (!isVisible()) {
+        return;
+    }
 
-    FolderView *view = static_cast<FolderView*>(sender());
-    Q_CHECK_PTR(view);
+    if (checked) {
+        if (!model()->isActive()) {
+            return;
+        }
 
-    QFileInfoList list = view->checkedItems();
-    if (list.isEmpty()) {
-        showNameFilters();
+        ui->searchBox->setVisible(true);
+        ui->searchBox->setFocus();
+        ui->searchBox->selectAll();
     }
     else {
-        int numFolders = 0;
-        int numFiles = 0;
-        quint64 size = 0;
-        foreach (const QFileInfo &info, list) {
-            if (info.isDir()) {
-                numFolders++;
-            }
-            else {
-                numFiles++;
-                size += info.size();
-            }
+        if (!ui->searchBox->isVisible()) {
+            return;
         }
 
-        QString msg = QString::null;
-        if (numFolders > 0) {
-            msg += tr("%1個のフォルダ ").arg(numFolders);
-        }
-        if (numFiles > 0) {
-            msg += tr("%1個のファイル ").arg(numFiles);
-        }
-
-        if (!msg.isEmpty()) {
-            msg += tr("を選択 合計%1").arg(FilesizeToString(size));
+        if (ui->searchBox->hasFocus()) {
+            itemView()->setFocus();
         }
+        ui->searchBox->setVisible(false);
+    }
+}
 
-        ui->filterLabel->setText(msg);
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::toggleView
+/// \param checked  メニューのチェック状態
+///
+/// リスト/サムネイルを切り替えます。
+///
+void FolderPanel::toggleView(bool checked)
+{
+    QModelIndex index = itemView()->currentIndex();
+    if (checked) {
+        ui->folderView->setVisible(false);
+        ui->thumbnailView->setVisible(true);
+        ui->thumbnailView->setFocus();
+    }
+    else {
+        ui->thumbnailView->setVisible(false);
+        ui->folderView->setVisible(true);
+        ui->folderView->setFocus();
     }
+    itemView()->setCurrentIndex(index);
 }
 
-void FolderPanel::addBookmark()
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::updateAppearance
+///
+/// 外観を変更します。
+///
+void FolderPanel::updateAppearance(const Preferences &prefs)
 {
-    qDebug() << "FolderPanel::addBookmark();";
+    qDebug() << "FolderPanel::updateAppearance()";
 
-    QSettings settings;
-    int i = 0;
-    while (!settings.value(IniKey_BookmarkEntryName(i), "").toString().isEmpty()) {
-        i++;
-    }
+    QPalette pal;
 
-    QFileInfo info(ui->folderView->dir());
-    QString name = info.fileName();
-    if (name.isEmpty()) {
-        name = "root";
-    }
-    settings.setValue(IniKey_BookmarkEntryName(i), name);
-    settings.setValue(IniKey_BookmarkEntryPath(i), info.absoluteFilePath());
+    pal = ui->filterLabel->palette();
+    pal.setColor(ui->filterLabel->backgroundRole(), prefs.folderViewBgColor(model()->isActive()));
+    pal.setColor(ui->filterLabel->foregroundRole(), prefs.folderViewFgColor(model()->isActive()));
+    ui->filterLabel->setAutoFillBackground(true);
+    ui->filterLabel->setPalette(pal);
+
+    pal = ui->locationBox->palette();
+    pal.setColor(QPalette::Base, prefs.locationBoxBgColor(model()->isActive()));
+    pal.setColor(QPalette::Text, prefs.locationBoxFgColor(model()->isActive()));
+    ui->locationBox->setPalette(pal);
+    ui->locationBox->setFont(prefs.getLocationBoxFont());
+
+    pal = ui->searchBox->palette();
+    pal.setColor(QPalette::Base, prefs.getSearchBoxBgColor());
+    pal.setColor(QPalette::Text, prefs.getSearchBoxFgColor());
+    ui->searchBox->setPalette(pal);
+    ui->searchBox->setFont(prefs.getSearchBoxFont());
 
-    m_mainWnd->statusBar()->showMessage(tr("%1をブックマークに追加しました").arg(name));
+    pal = ui->folderView->palette();
+    pal.setColor(QPalette::Base, prefs.folderViewBgColor(model()->isActive()));
+    ui->folderView->setPalette(pal);
+
+    pal = ui->thumbnailView->palette();
+    pal.setColor(QPalette::Base, prefs.folderViewBgColor(model()->isActive()));
+    ui->thumbnailView->setPalette(pal);
+
+    QHeaderView *header = ui->folderView->verticalHeader();
+    QFont font = prefs.getFolderViewFont();
+    header->setDefaultSectionSize(QFontMetrics(font).height() * prefs.getLineHeight());
 }
 
-void FolderPanel::itemFound()
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::searchNext
+/// \param step 次なら1, 前なら-1
+///
+/// 次または前のアイテムを検索します。
+///
+void FolderPanel::searchNext(int step)
 {
-    qDebug() << "FolderPanel::itemFound";
+    qDebug() << "FolderPanel::searchNext()" << step;
 
-    QPalette pal = ui->searchBox->palette();
-    pal.setColor(QPalette::Text, QPalette().text().color());
-    ui->searchBox->setPalette(pal);
+    QModelIndex index = model()->search(ui->searchBox->text(),
+                                        itemView()->currentIndex(),
+                                        step);
+    showSearchResult(index);
 }
 
-void FolderPanel::itemNotFound()
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::setItemView
+/// \param name "folderView"または"thumbnailView"
+///
+/// 指定されたビューを可視状態にします。
+///
+void FolderPanel::setItemView(const QString &name)
 {
-    qDebug() << "FolderPanel::itemNotFound";
+    ui->folderView->setVisible(false);
+    ui->thumbnailView->setVisible(false);
+    findChild<QWidget*>(name)->setVisible(true);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::showSearchResult
+/// \param index    検索されたアイテム
+///
+/// 検索結果を表示します。
+///
+void FolderPanel::showSearchResult(const QModelIndex &index)
+{
+    Preferences prefs(this);
 
     QPalette pal = ui->searchBox->palette();
-    pal.setColor(QPalette::Text, Qt::red);
+    if (index.isValid()) {
+        itemView()->setCurrentIndex(index);
+        pal.setColor(QPalette::Base, prefs.getSearchBoxBgColor());
+        pal.setColor(QPalette::Text, prefs.getSearchBoxFgColor());
+    }
+    else {
+        pal.setColor(QPalette::Base, prefs.getSearchBoxUnmatchBgColor());
+        pal.setColor(QPalette::Text, prefs.getSearchBoxUnmatchFgColor());
+    }
     ui->searchBox->setPalette(pal);
 }
 
-void FolderPanel::searchItem(const QString &text)
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model_dataChanged
+/// \param topLeft      (使用しません)
+/// \param bottomRight  (使用しません)
+///
+/// アイテムの選択状態変更に伴う処理を行います。
+///
+void FolderPanel::model_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
 {
-    qDebug() << "FolderPanel::searchItem" << text;
+    Q_UNUSED(topLeft);
+    Q_UNUSED(bottomRight);
 
-    if (text.right(1) == "/") {
-        // '/'が入力されたら、検索終了
-        ui->searchBox->setText(text.left(text.length() - 1));
-        ui->folderView->setFocus();
+    QFileInfoList list = model()->markedItems();
+    if (list.isEmpty()) {
+        ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
     }
     else {
-        ui->folderView->searchItem(ui->searchBox->text());
+        int numFolders = 0;
+        int numFiles = 0;
+        qint64 totalSize = 0;
+        foreach (const QFileInfo &fi, list) {
+            if (fi.isDir()) {
+                ++numFolders;
+            }
+            else {
+                ++numFiles;
+                totalSize += fi.size();
+            }
+        }
+
+        QString text;
+        if (numFolders > 0) {
+            text += tr("%1個のフォルダ ").arg(numFolders);
+        }
+        if (numFiles > 0) {
+            text += tr("%1個のファイル ").arg(numFiles);
+        }
+        if (!text.isEmpty()) {
+            text += tr("を選択 合計%1").arg(FileSizeToString(totalSize));
+        }
+        ui->filterLabel->setText(text);
     }
 }
 
-void FolderPanel::focusInEvent(QFocusEvent *)
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model_Reset
+///
+/// モデルリセット後の処理を行います。
+///
+void FolderPanel::model_Reset()
 {
-    qDebug() << "FolderPanel::focusInEvent();";
+    ui->locationBox->setText(model()->rootPath());
+    ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
+}
 
-    ui->folderView->setFocus();
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::on_searchBox_textEdited
+/// \param arg1 入力テキスト
+///
+/// 検索ボックスのテキストが変更された場合の処理を行います。
+///
+void FolderPanel::on_searchBox_textEdited(const QString &arg1)
+{
+    if (arg1.right(1) == "/") {
+        ui->searchBox->setText(arg1.left(arg1.size() - 1));
+        // 検索終了
+        itemView()->setFocus();
+    }
+    else {
+        showSearchResult(model()->search(arg1));
+    }
 }