OSDN Git Service

Ver0.17
[gefu/Gefu.git] / folderpanel.cpp
1 #include "common.h"
2 #include "mainwindow.h"
3 #include "searchbox.h"
4 #include "locationbox.h"
5 #include "folderpanel.h"
6 #include "ui_folderpanel.h"
7
8 #include <QDebug>
9 #include <QSettings>
10 #include <QStatusBar>
11
12 FolderPanel::FolderPanel(QWidget *parent) :
13     QWidget(parent),
14     ui(new Ui::FolderPanel),
15     m_mainWnd(NULL)
16 {
17     ui->setupUi(this);
18 }
19
20 FolderPanel::~FolderPanel()
21 {
22     delete ui;
23 }
24
25 void FolderPanel::initialize(MainWindow *mainWnd)
26 {
27     qDebug() << "FolderPanel::initialize();";
28     m_mainWnd = mainWnd;
29
30     // シグナル&スロット
31     connect(ui->searchBox, SIGNAL(textEdited(QString)), this, SLOT(searchItem(QString)));
32     connect(ui->folderView, SIGNAL(doubleClicked(QModelIndex)), mainWnd, SLOT(open(QModelIndex)));
33     connect(ui->folderView, SIGNAL(dropAccepted(QFileInfoList)), mainWnd, SLOT(dropAccept(QFileInfoList)));
34     connect(ui->folderView, SIGNAL(currentChanged(QFileInfo)), mainWnd, SLOT(currentChange(QFileInfo)));
35     connect(ui->folderView, SIGNAL(requestContextMenu(QContextMenuEvent*)), mainWnd, SLOT(showContextMenu(QContextMenuEvent*)));
36     connect(ui->folderView, SIGNAL(retrieveStarted(QString)), ui->locationBox, SLOT(setText(QString)));
37     connect(ui->folderView, SIGNAL(dataChanged()), this, SLOT(dataChange()));
38     connect(ui->folderView, SIGNAL(itemFound()), this, SLOT(itemFound()));
39     connect(ui->folderView, SIGNAL(itemNotFound()), this, SLOT(itemNotFound()));
40     connect(ui->bookmarkBtn, SIGNAL(clicked()), this, SLOT(addBookmark()));
41
42     // 初期状態では検索ボックスは非表示
43     ui->searchBox->setVisible(false);
44
45     // ロケーションボックスを初期化する
46     ui->locationBox->initialize();
47
48     // フォルダビューを初期化する
49     ui->folderView->initialize(mainWnd);
50 }
51
52 LocationBox *FolderPanel::locationBox() const
53 {
54     return ui->locationBox;
55 }
56
57 FolderView *FolderPanel::folderView() const
58 {
59     return ui->folderView;
60 }
61
62 SearchBox *FolderPanel::searchBox() const
63 {
64     return ui->searchBox;
65 }
66
67 QLabel *FolderPanel::filterLabel() const
68 {
69     return ui->filterLabel;
70 }
71
72 void FolderPanel::setNameFilters(const QString &filters)
73 {
74     QStringList list = filters.split(" ", QString::SkipEmptyParts);
75     if (list.isEmpty()) {
76         list << "*";
77     }
78     ui->folderView->setNameFilters(list);
79     showNameFilters();
80 }
81
82 void FolderPanel::showNameFilters()
83 {
84     ui->filterLabel->setText(tr("フィルタ:") + ui->folderView->nameFilters().join(" "));
85 }
86
87 void FolderPanel::dataChange()
88 {
89     qDebug() << "FolderPanel::dataChange();";
90
91     FolderView *view = static_cast<FolderView*>(sender());
92     Q_CHECK_PTR(view);
93
94     QFileInfoList list = view->checkedItems();
95     if (list.isEmpty()) {
96         showNameFilters();
97     }
98     else {
99         int numFolders = 0;
100         int numFiles = 0;
101         quint64 size = 0;
102         foreach (const QFileInfo &info, list) {
103             if (info.isDir()) {
104                 numFolders++;
105             }
106             else {
107                 numFiles++;
108                 size += info.size();
109             }
110         }
111
112         QString msg = QString::null;
113         if (numFolders > 0) {
114             msg += tr("%1個のフォルダ ").arg(numFolders);
115         }
116         if (numFiles > 0) {
117             msg += tr("%1個のファイル ").arg(numFiles);
118         }
119
120         if (!msg.isEmpty()) {
121             msg += tr("を選択 合計%1").arg(FilesizeToString(size));
122         }
123
124         ui->filterLabel->setText(msg);
125     }
126 }
127
128 void FolderPanel::addBookmark()
129 {
130     qDebug() << "FolderPanel::addBookmark();";
131
132     QSettings settings;
133     int i = 0;
134     while (!settings.value(IniKey_BookmarkEntryName(i), "").toString().isEmpty()) {
135         i++;
136     }
137
138     QFileInfo info(ui->folderView->dir());
139     QString name = info.fileName();
140     if (name.isEmpty()) {
141         name = "root";
142     }
143     settings.setValue(IniKey_BookmarkEntryName(i), name);
144     settings.setValue(IniKey_BookmarkEntryPath(i), info.absoluteFilePath());
145
146     m_mainWnd->statusBar()->showMessage(tr("%1をブックマークに追加しました").arg(name));
147 }
148
149 void FolderPanel::itemFound()
150 {
151     qDebug() << "FolderPanel::itemFound";
152
153     QPalette pal = ui->searchBox->palette();
154     pal.setColor(QPalette::Text, QPalette().text().color());
155     ui->searchBox->setPalette(pal);
156 }
157
158 void FolderPanel::itemNotFound()
159 {
160     qDebug() << "FolderPanel::itemNotFound";
161
162     QPalette pal = ui->searchBox->palette();
163     pal.setColor(QPalette::Text, Qt::red);
164     ui->searchBox->setPalette(pal);
165 }
166
167 void FolderPanel::searchItem(const QString &text)
168 {
169     qDebug() << "FolderPanel::searchItem" << text;
170
171     if (text.right(1) == "/") {
172         // '/'が入力されたら、検索終了
173         ui->searchBox->setText(text.left(text.length() - 1));
174         ui->folderView->setFocus();
175     }
176     else {
177         ui->folderView->searchItem(ui->searchBox->text());
178     }
179 }
180
181 void FolderPanel::focusInEvent(QFocusEvent *)
182 {
183     qDebug() << "FolderPanel::focusInEvent();";
184
185     ui->folderView->setFocus();
186 }