OSDN Git Service

パネル自体の背景色を変更
[gefu/Gefu.git] / folderpanel.cpp
index 1c8eb90..775079d 100644 (file)
-#include "common.h"\r
-#include "filetablemodel.h"\r
-#include "folderpanel.h"\r
-#include "mainwindow.h"\r
-#include "ui_folderpanel.h"\r
-#include <QCheckBox>\r
-#include <QMessageBox>\r
-#include <QKeyEvent>\r
-#include <QDebug>\r
-#include <QDateTime>\r
-#include <QDesktopServices>\r
-#include <QSettings>\r
-#ifdef Q_OS_WIN32\r
-    #include <windows.h>\r
-#endif\r
-\r
-QString FilesizeToString(quint64 size)\r
-{\r
-    if (size >= 1024 * 1024 * 1024) {\r
-        return QString("%1GB").arg(int(10 * size / (1024 * 1024 * 1024)) / 10.0);\r
-    }\r
-    if (size >= 1024 * 1024) {\r
-        return QString("%1MB").arg(int(10 * size / (1024 * 1024)) / 10.0);\r
-    }\r
-    if (size >= 1024) {\r
-        return QString("%1KB").arg(int(10 * size / 1024) / 10.0);\r
-    }\r
-    return QString("%1B").arg(size);\r
-}\r
-\r
-FolderPanel::FolderPanel(QWidget *parent) :\r
-    QWidget(parent),\r
-    ui(new Ui::FolderPanel)\r
-{\r
-    ui->setupUi(this);\r
-    ui->fileTable->setModel(new FileTableModel(this));\r
-\r
-    // リサイズ時の動作を設定する\r
-    QHeaderView *header = ui->fileTable->horizontalHeader();\r
-    header->setSectionResizeMode(0, QHeaderView::ResizeToContents);\r
-    header->setSectionResizeMode(1, QHeaderView::Stretch);\r
-    header->setSectionResizeMode(2, QHeaderView::ResizeToContents);\r
-    header->setSectionResizeMode(3, QHeaderView::ResizeToContents);\r
-\r
-    header->setDefaultSectionSize(header->minimumSectionSize());\r
-}\r
-\r
-FolderPanel::~FolderPanel()\r
-{\r
-    delete ui;\r
-}\r
-\r
-QTableView* FolderPanel::fileTable()\r
-{\r
-    return ui->fileTable;\r
-}\r
-\r
-const QTableView *FolderPanel::fileTable() const\r
-{\r
-    return ui->fileTable;\r
-}\r
-\r
-const QString FolderPanel::side() const\r
-{\r
-    return ui->fileTable->side();\r
-}\r
-\r
-void FolderPanel::setSide(const QString &side)\r
-{\r
-    ui->fileTable->setSide(side);\r
-\r
-    QSettings settings;\r
-    FileTableModel *model = new FileTableModel();\r
-    connect(model, SIGNAL(rootChanged(QString)),\r
-            ui->locationField, SLOT(setText(QString)));\r
-    connect(model, SIGNAL(stateChanged(int,int,quint64)),\r
-            this, SLOT(onStateChanged(int,int,quint64)));\r
-\r
-    //>>>>> フィルタ初期化\r
-    model->setFilter(QDir::NoDot | QDir::AllDirs | QDir::Files);\r
-    if (settings.value(IniKey_ShowHidden, false).toBool()) {\r
-        model->setFilter(model->filter() | QDir::Hidden);\r
-    }\r
-    if (settings.value(IniKey_ShowSystem, false).toBool()) {\r
-        model->setFilter(model->filter() | QDir::System);\r
-    }\r
-    //>>>>> ソート順初期化\r
-    model->setSorting(QDir::Name);  // 0\r
-    int sortBy = settings.value(side + slash + IniKey_SortBy, SortByName).toInt();\r
-    switch (sortBy) {\r
-    case SortByDate:    model->setSorting(model->sorting() | QDir::Time); break;\r
-    case SortBySize:    model->setSorting(model->sorting() | QDir::Size); break;\r
-    case SortByType:    model->setSorting(model->sorting() | QDir::Type); break;\r
-    default:            model->setSorting(model->sorting() | QDir::Name); break;\r
-    }\r
-    // デフォルトだと文字列は昇順で、数値は降順…orz\r
-    int orderBy = settings.value(side + slash + IniKey_OrderBy, OrderByDesc).toInt();\r
-    if (((sortBy == SortByName || sortBy == SortByType) && orderBy == OrderByDesc) ||\r
-        ((sortBy == SortByDate || sortBy == SortBySize) && orderBy == OrderByAsc))\r
-    {\r
-        model->setSorting(model->sorting() | QDir::Reversed);\r
-    }\r
-    // フォルダの位置\r
-    switch (settings.value(side + slash + IniKey_PutDirs, PutDirsFirst).toInt()) {\r
-    case PutDirsFirst:  model->setSorting(model->sorting() | QDir::DirsFirst); break;\r
-    case PutDirsLast:   model->setSorting(model->sorting() | QDir::DirsLast); break;\r
-    }\r
-    // 大文字小文字の区別\r
-    if (settings.value(side + slash + IniKey_IgnoreCase, true).toBool()) {\r
-        model->setSorting(model->sorting() | QDir::IgnoreCase);\r
-    }\r
-    //>>>>> 監視フォルダ初期化\r
-    QString key = side + slash + IniKey_Dir;\r
-    QString path = settings.value(key, QDir::homePath()).toString();\r
-    model->setPath(path);\r
-\r
-    ui->fileTable->setModel(model);\r
-    ui->fileTable->selectRow(0);\r
-    ui->fileTable->resizeColumnsToContents();\r
-    ui->fileTable->resizeRowsToContents();\r
-}\r
-\r
-void FolderPanel::onStateChanged(int checkedFolders, int checkedFiles, quint64 totalSize)\r
-{\r
-    QString msg = "";\r
-    if (checkedFolders > 0) {\r
-        msg += tr("%1個のフォルダ ").arg(checkedFolders);\r
-    }\r
-    if (checkedFiles > 0) {\r
-        msg += tr("%1個のファイル ").arg(checkedFiles);\r
-    }\r
-    if (msg.length() > 0) {\r
-        msg += "を選択 合計 ";\r
-        msg += FilesizeToString(totalSize);\r
-    }\r
-\r
-    ui->label->setText(msg);\r
-\r
-}\r
-\r
-void FolderPanel::on_locationField_editingFinished()\r
-{\r
-    ui->locationField->blockSignals(true);\r
-\r
-    QString path = ui->locationField->text();\r
-    ui->fileTable->setRootPath(path);\r
-\r
-    ui->locationField->blockSignals(false);\r
-}\r
+#include "global.h"
+#include "preferences.h"
+#include "mainwindow.h"
+#include "folderpanel.h"
+#include "ui_folderpanel.h"
+
+#include <QDebug>
+#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;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::initialize
+/// \param w    メインウィンドウオブジェクト
+///
+/// 初期化処理を行います。
+///
+void FolderPanel::initialize(MainWindow *w)
+{
+    qDebug() << "FolderPanel::initialize();";
+
+    // フォルダビューを初期化する
+    ui->folderView->initialize(w);
+    // サムネイルビューを初期化する
+    ui->thumbnailView->initialize(w);
+
+    connect(ui->bookmarkBtn, SIGNAL(clicked()), w, SLOT(onAddBookmark()));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::itemView
+/// \return アイテムビューを返します。
+///
+QAbstractItemView *FolderPanel::itemView() const
+{
+    if (ui->thumbnailView->isVisible()) {
+        return ui->thumbnailView;
+    }
+    return ui->folderView;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model
+/// \return 関連付けられたフォルダモデルを返します。
+///
+FolderModel *FolderPanel::model() const
+{
+    return static_cast<FolderModel*>(ui->folderView->model());
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::setModel
+/// \param m    設定するモデル
+///
+/// モデルを設定します。
+///
+void FolderPanel::setModel(FolderModel *m)
+{
+    qDebug() << "FolderPanel::setModel()" << m;
+    if (model()) {
+        model()->disconnect(this);
+    }
+
+    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());
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::toggleSearch
+/// \param checked  メニューのチェック状態
+///
+/// 検索ボックスの表示/非表示を切り替えます。
+///
+void FolderPanel::toggleSearch(bool checked)
+{
+    qDebug() << "FolderPanel::toggleSearch()" << checked;
+    if (!isVisible()) {
+        return;
+    }
+
+    if (checked) {
+        if (!model()->isActive()) {
+            return;
+        }
+
+        ui->searchBox->setVisible(true);
+        ui->searchBox->setFocus();
+        ui->searchBox->selectAll();
+    }
+    else {
+        if (!ui->searchBox->isVisible()) {
+            return;
+        }
+
+        if (ui->searchBox->hasFocus()) {
+            itemView()->setFocus();
+        }
+        ui->searchBox->setVisible(false);
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \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);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::updateAppearance
+///
+/// 外観を変更します。
+///
+void FolderPanel::updateAppearance(const Preferences &prefs)
+{
+    qDebug() << "FolderPanel::updateAppearance()";
+
+    QPalette pal;
+
+    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());
+
+    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());
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::searchNext
+/// \param step 次なら1, 前なら-1
+///
+/// 次または前のアイテムを検索します。
+///
+void FolderPanel::searchNext(int step)
+{
+    qDebug() << "FolderPanel::searchNext()" << step;
+
+    QModelIndex index = model()->search(ui->searchBox->text(),
+                                        itemView()->currentIndex(),
+                                        step);
+    showSearchResult(index);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::setItemView
+/// \param name "folderView"または"thumbnailView"
+///
+/// 指定されたビューを可視状態にします。
+///
+void FolderPanel::setItemView(const QString &name)
+{
+    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();
+    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);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model_dataChanged
+/// \param topLeft      (使用しません)
+/// \param bottomRight  (使用しません)
+///
+/// アイテムの選択状態変更に伴う処理を行います。
+///
+void FolderPanel::model_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+{
+    Q_UNUSED(topLeft);
+    Q_UNUSED(bottomRight);
+
+    QFileInfoList list = model()->markedItems();
+    if (list.isEmpty()) {
+        ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
+    }
+    else {
+        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);
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \brief FolderPanel::model_Reset
+///
+/// モデルリセット後の処理を行います。
+///
+void FolderPanel::model_Reset()
+{
+    ui->locationBox->setText(model()->rootPath());
+    ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+/// \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));
+    }
+}