OSDN Git Service

Ver0.26
[gefu/Gefu.git] / folderpanel.cpp
1 #include "global.h"
2 #include "preferences.h"
3 #include "mainwindow.h"
4 #include "folderpanel.h"
5 #include "ui_folderpanel.h"
6
7 #include <QDebug>
8 #include <QSettings>
9 #include <QStatusBar>
10
11 ///////////////////////////////////////////////////////////////////////////////
12 /// \brief FolderPanel::FolderPanel
13 /// \param parent   親ウィジェット
14 ///
15 /// コンストラクタ
16 ///
17 FolderPanel::FolderPanel(QWidget *parent) :
18     QWidget(parent),
19     ui(new Ui::FolderPanel),
20     m_mainWnd(NULL)
21 {
22     ui->setupUi(this);
23     ui->searchBox->setText("");
24     ui->searchBox->setVisible(false);
25     ui->thumbnailView->setVisible(false);
26 }
27
28 ///////////////////////////////////////////////////////////////////////////////
29 /// \brief FolderPanel::~FolderPanel
30 ///
31 /// デストラクタ
32 ///
33 FolderPanel::~FolderPanel()
34 {
35     delete ui;
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
39 /// \brief FolderPanel::initialize
40 /// \param w    メインウィンドウオブジェクト
41 ///
42 /// 初期化処理を行います。
43 ///
44 void FolderPanel::initialize(MainWindow *w)
45 {
46     qDebug() << "FolderPanel::initialize();";
47
48     // フォルダビューを初期化する
49     ui->folderView->initialize(w);
50     // サムネイルビューを初期化する
51     ui->thumbnailView->initialize(w);
52
53     connect(ui->bookmarkBtn, SIGNAL(clicked()), w, SLOT(onAddBookmark()));
54 }
55
56 ///////////////////////////////////////////////////////////////////////////////
57 /// \brief FolderPanel::itemView
58 /// \return アイテムビューを返します。
59 ///
60 QAbstractItemView *FolderPanel::itemView() const
61 {
62     if (ui->thumbnailView->isVisible()) {
63         return ui->thumbnailView;
64     }
65     return ui->folderView;
66 }
67
68 ///////////////////////////////////////////////////////////////////////////////
69 /// \brief FolderPanel::model
70 /// \return 関連付けられたフォルダモデルを返します。
71 ///
72 FolderModel *FolderPanel::model() const
73 {
74     return static_cast<FolderModel*>(ui->folderView->model());
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
78 /// \brief FolderPanel::setModel
79 /// \param m    設定するモデル
80 ///
81 /// モデルを設定します。
82 ///
83 void FolderPanel::setModel(FolderModel *m)
84 {
85     qDebug() << "FolderPanel::setModel()" << m;
86     if (model()) {
87         model()->disconnect(this);
88     }
89
90     ui->folderView->setModel(m);
91     ui->thumbnailView->setModel(m);
92
93     if (model()) {
94         connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
95                 this, SLOT(model_dataChanged(QModelIndex,QModelIndex)));
96         connect(model(), SIGNAL(modelReset()), this, SLOT(model_Reset()));
97
98         model_Reset();
99         model_dataChanged(QModelIndex(), QModelIndex());
100     }
101 }
102
103 ///////////////////////////////////////////////////////////////////////////////
104 /// \brief FolderPanel::toggleSearch
105 /// \param checked  メニューのチェック状態
106 ///
107 /// 検索ボックスの表示/非表示を切り替えます。
108 ///
109 void FolderPanel::toggleSearch(bool checked)
110 {
111     qDebug() << "FolderPanel::toggleSearch()" << checked;
112     if (!isVisible()) {
113         return;
114     }
115
116     if (checked) {
117         if (!model()->isActive()) {
118             return;
119         }
120
121         ui->searchBox->setVisible(true);
122         ui->searchBox->setFocus();
123         ui->searchBox->selectAll();
124     }
125     else {
126         if (!ui->searchBox->isVisible()) {
127             return;
128         }
129
130         if (ui->searchBox->hasFocus()) {
131             itemView()->setFocus();
132         }
133         ui->searchBox->setVisible(false);
134     }
135 }
136
137 ///////////////////////////////////////////////////////////////////////////////
138 /// \brief FolderPanel::toggleView
139 /// \param checked  メニューのチェック状態
140 ///
141 /// リスト/サムネイルを切り替えます。
142 ///
143 void FolderPanel::toggleView(bool checked)
144 {
145     QModelIndex index = itemView()->currentIndex();
146     if (checked) {
147         ui->folderView->setVisible(false);
148         ui->thumbnailView->setVisible(true);
149         ui->thumbnailView->setFocus();
150     }
151     else {
152         ui->thumbnailView->setVisible(false);
153         ui->folderView->setVisible(true);
154         ui->folderView->setFocus();
155     }
156     itemView()->setCurrentIndex(index);
157 }
158
159 ///////////////////////////////////////////////////////////////////////////////
160 /// \brief FolderPanel::updateAppearance
161 ///
162 /// 外観を変更します。
163 ///
164 void FolderPanel::updateAppearance(const Preferences &prefs)
165 {
166     qDebug() << "FolderPanel::updateAppearance()";
167
168     QPalette pal;
169
170     pal = ui->filterLabel->palette();
171     pal.setColor(ui->filterLabel->backgroundRole(), prefs.folderViewBgColor(model()->isActive()));
172     pal.setColor(ui->filterLabel->foregroundRole(), prefs.folderViewFgColor(model()->isActive()));
173     ui->filterLabel->setAutoFillBackground(true);
174     ui->filterLabel->setPalette(pal);
175
176     pal = ui->locationBox->palette();
177     pal.setColor(QPalette::Base, prefs.locationBoxBgColor(model()->isActive()));
178     pal.setColor(QPalette::Text, prefs.locationBoxFgColor(model()->isActive()));
179     ui->locationBox->setPalette(pal);
180     ui->locationBox->setFont(prefs.getLocationBoxFont());
181
182     pal = ui->searchBox->palette();
183     pal.setColor(QPalette::Base, prefs.getSearchBoxBgColor());
184     pal.setColor(QPalette::Text, prefs.getSearchBoxFgColor());
185     ui->searchBox->setPalette(pal);
186     ui->searchBox->setFont(prefs.getSearchBoxFont());
187
188     pal = ui->folderView->palette();
189     pal.setColor(QPalette::Base, prefs.folderViewBgColor(model()->isActive()));
190     ui->folderView->setPalette(pal);
191
192     pal = ui->thumbnailView->palette();
193     pal.setColor(QPalette::Base, prefs.folderViewBgColor(model()->isActive()));
194     ui->thumbnailView->setPalette(pal);
195
196     QHeaderView *header = ui->folderView->verticalHeader();
197     QFont font = prefs.getFolderViewFont();
198     header->setDefaultSectionSize(QFontMetrics(font).height() * prefs.getLineHeight());
199 }
200
201 ///////////////////////////////////////////////////////////////////////////////
202 /// \brief FolderPanel::searchNext
203 /// \param step 次なら1, 前なら-1
204 ///
205 /// 次または前のアイテムを検索します。
206 ///
207 void FolderPanel::searchNext(int step)
208 {
209     qDebug() << "FolderPanel::searchNext()" << step;
210
211     QModelIndex index = model()->search(ui->searchBox->text(),
212                                         itemView()->currentIndex(),
213                                         step);
214     showSearchResult(index);
215 }
216
217 ///////////////////////////////////////////////////////////////////////////////
218 /// \brief FolderPanel::setItemView
219 /// \param name "folderView"または"thumbnailView"
220 ///
221 /// 指定されたビューを可視状態にします。
222 ///
223 void FolderPanel::setItemView(const QString &name)
224 {
225     ui->folderView->setVisible(false);
226     ui->thumbnailView->setVisible(false);
227     findChild<QWidget*>(name)->setVisible(true);
228 }
229
230 ///////////////////////////////////////////////////////////////////////////////
231 /// \brief FolderPanel::showSearchResult
232 /// \param index    検索されたアイテム
233 ///
234 /// 検索結果を表示します。
235 ///
236 void FolderPanel::showSearchResult(const QModelIndex &index)
237 {
238     Preferences prefs(this);
239
240     QPalette pal = ui->searchBox->palette();
241     if (index.isValid()) {
242         itemView()->setCurrentIndex(index);
243         pal.setColor(QPalette::Base, prefs.getSearchBoxBgColor());
244         pal.setColor(QPalette::Text, prefs.getSearchBoxFgColor());
245     }
246     else {
247         pal.setColor(QPalette::Base, prefs.getSearchBoxUnmatchBgColor());
248         pal.setColor(QPalette::Text, prefs.getSearchBoxUnmatchFgColor());
249     }
250     ui->searchBox->setPalette(pal);
251 }
252
253 ///////////////////////////////////////////////////////////////////////////////
254 /// \brief FolderPanel::model_dataChanged
255 /// \param topLeft      (使用しません)
256 /// \param bottomRight  (使用しません)
257 ///
258 /// アイテムの選択状態変更に伴う処理を行います。
259 ///
260 void FolderPanel::model_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
261 {
262     Q_UNUSED(topLeft);
263     Q_UNUSED(bottomRight);
264
265     QFileInfoList list = model()->markedItems();
266     if (list.isEmpty()) {
267         ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
268     }
269     else {
270         int numFolders = 0;
271         int numFiles = 0;
272         qint64 totalSize = 0;
273         foreach (const QFileInfo &fi, list) {
274             if (fi.isDir()) {
275                 ++numFolders;
276             }
277             else {
278                 ++numFiles;
279                 totalSize += fi.size();
280             }
281         }
282
283         QString text;
284         if (numFolders > 0) {
285             text += tr("%1個のフォルダ ").arg(numFolders);
286         }
287         if (numFiles > 0) {
288             text += tr("%1個のファイル ").arg(numFiles);
289         }
290         if (!text.isEmpty()) {
291             text += tr("を選択 合計%1").arg(fileSizeToString(totalSize));
292         }
293         ui->filterLabel->setText(text);
294     }
295 }
296
297 ///////////////////////////////////////////////////////////////////////////////
298 /// \brief FolderPanel::model_Reset
299 ///
300 /// モデルリセット後の処理を行います。
301 ///
302 void FolderPanel::model_Reset()
303 {
304     ui->locationBox->setText(model()->rootPath());
305     ui->filterLabel->setText(tr("フィルタ:%1").arg(model()->nameFilters().join(" ")));
306 }
307
308 ///////////////////////////////////////////////////////////////////////////////
309 /// \brief FolderPanel::on_searchBox_textEdited
310 /// \param arg1 入力テキスト
311 ///
312 /// 検索ボックスのテキストが変更された場合の処理を行います。
313 ///
314 void FolderPanel::on_searchBox_textEdited(const QString &arg1)
315 {
316     if (arg1.right(1) == "/") {
317         ui->searchBox->setText(arg1.left(arg1.size() - 1));
318         // 検索終了
319         itemView()->setFocus();
320     }
321     else {
322         showSearchResult(model()->search(arg1));
323     }
324 }