X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=folderpanel.cpp;h=0b168c837bf6881af61328fd978912ac46babfe0;hb=606ff8dc1c1abcd474d99f32eab21c6869554ed7;hp=cc6494ae4fc12cef30e174e7793d2500d0fcce59;hpb=40fa7ea29483100e8b345ffac05cd16cd57a7613;p=gefu%2FGefu.git diff --git a/folderpanel.cpp b/folderpanel.cpp index cc6494a..0b168c8 100644 --- a/folderpanel.cpp +++ b/folderpanel.cpp @@ -1,149 +1,324 @@ -#include "common.h" -#include "filetablemodel.h" -#include "folderpanel.h" -#include "mainwindow.h" -#include "ui_folderpanel.h" -#include -#include -#include -#include -#include -#include -#include -#ifdef Q_OS_WIN32 - #include -#endif - -QString FilesizeToString(quint64 size) -{ - if (size >= 1024 * 1024 * 1024) { - return QString("%1GB").arg(int(10 * size / (1024 * 1024 * 1024)) / 10.0); - } - if (size >= 1024 * 1024) { - return QString("%1MB").arg(int(10 * size / (1024 * 1024)) / 10.0); - } - if (size >= 1024) { - return QString("%1KB").arg(int(10 * size / 1024) / 10.0); - } - return QString("%1B").arg(size); -} - -FolderPanel::FolderPanel(QWidget *parent) : - QWidget(parent), - ui(new Ui::FolderPanel) -{ - ui->setupUi(this); - ui->fileTable->setModel(new FileTableModel(this)); - - // リサイズ時の動作を設定する - QHeaderView *header = ui->fileTable->horizontalHeader(); - header->setSectionResizeMode(0, QHeaderView::ResizeToContents); - header->setSectionResizeMode(1, QHeaderView::Stretch); - header->setSectionResizeMode(2, QHeaderView::ResizeToContents); - header->setSectionResizeMode(3, QHeaderView::ResizeToContents); - header->setDefaultSectionSize(header->minimumSectionSize()); - - QHeaderView *vHeader = ui->fileTable->verticalHeader(); - vHeader->setDefaultSectionSize(vHeader->minimumSectionSize()); -} - -FolderPanel::~FolderPanel() -{ - delete ui; -} - -QTableView* FolderPanel::fileTable() -{ - return ui->fileTable; -} - -const QTableView *FolderPanel::fileTable() const -{ - return ui->fileTable; -} - -const QString FolderPanel::side() const -{ - return ui->fileTable->side(); -} - -void FolderPanel::setSide(const QString &side) -{ - ui->fileTable->setSide(side); - - QSettings settings; - FileTableModel *model = new FileTableModel(); - connect(model, SIGNAL(rootChanged(QString)), - ui->locationField, SLOT(setText(QString))); - connect(model, SIGNAL(stateChanged(int,int,quint64)), - this, SLOT(onStateChanged(int,int,quint64))); - - //>>>>> フィルタ初期化 - model->setFilter(QDir::NoDot | QDir::AllDirs | QDir::Files); - if (settings.value(IniKey_ShowHidden, false).toBool()) { - model->setFilter(model->filter() | QDir::Hidden); - } - if (settings.value(IniKey_ShowSystem, false).toBool()) { - model->setFilter(model->filter() | QDir::System); - } - //>>>>> ソート順初期化 - model->setSorting(QDir::Name); // 0 - int sortBy = settings.value(side + slash + IniKey_SortBy, SortByName).toInt(); - switch (sortBy) { - case SortByDate: model->setSorting(model->sorting() | QDir::Time); break; - case SortBySize: model->setSorting(model->sorting() | QDir::Size); break; - case SortByType: model->setSorting(model->sorting() | QDir::Type); break; - default: model->setSorting(model->sorting() | QDir::Name); break; - } - // デフォルトだと文字列は昇順で、数値は降順…orz - int orderBy = settings.value(side + slash + IniKey_OrderBy, OrderByDesc).toInt(); - if (((sortBy == SortByName || sortBy == SortByType) && orderBy == OrderByDesc) || - ((sortBy == SortByDate || sortBy == SortBySize) && orderBy == OrderByAsc)) - { - model->setSorting(model->sorting() | QDir::Reversed); - } - // フォルダの位置 - switch (settings.value(side + slash + IniKey_PutDirs, PutDirsFirst).toInt()) { - case PutDirsFirst: model->setSorting(model->sorting() | QDir::DirsFirst); break; - case PutDirsLast: model->setSorting(model->sorting() | QDir::DirsLast); break; - } - // 大文字小文字の区別 - if (settings.value(side + slash + IniKey_IgnoreCase, true).toBool()) { - model->setSorting(model->sorting() | QDir::IgnoreCase); - } - //>>>>> 監視フォルダ初期化 - QString key = side + slash + IniKey_Dir; - QString path = settings.value(key, QDir::homePath()).toString(); - - ui->fileTable->setModel(model); - ui->fileTable->setRootPath(path, true); - ui->fileTable->selectRow(0); -} - -void FolderPanel::onStateChanged(int checkedFolders, int checkedFiles, quint64 totalSize) -{ - QString msg = ""; - if (checkedFolders > 0) { - msg += tr("%1個のフォルダ ").arg(checkedFolders); - } - if (checkedFiles > 0) { - msg += tr("%1個のファイル ").arg(checkedFiles); - } - if (msg.length() > 0) { - msg += "を選択 合計 "; - msg += FilesizeToString(totalSize); - } - - ui->label->setText(msg); - -} - -void FolderPanel::on_locationField_editingFinished() -{ - ui->locationField->blockSignals(true); - - QString path = ui->locationField->text(); - ui->fileTable->setRootPath(path, true); - - ui->locationField->blockSignals(false); -} +#include "global.h" +#include "preferences.h" +#include "mainwindow.h" +#include "folderpanel.h" +#include "ui_folderpanel.h" + +#include +#include +#include + +/////////////////////////////////////////////////////////////////////////////// +/// \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(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(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)); + } +}