OSDN Git Service

Ver0.26
[gefu/Gefu.git] / panel.cpp
1 #include "filereadworker.h"
2 #include "foldermodel.h"
3 #include "folderview.h"
4 #include "global.h"
5 #include "hexview.h"
6 #include "imageview.h"
7 #include "mainwindow.h"
8 #include "preferences.h"
9 #include "textview.h"
10 #include "panel.h"
11 #include "ui_panel.h"
12
13 #include <QDebug>
14 #include <QTextCodec>
15
16 ///////////////////////////////////////////////////////////////////////////////
17 /// \brief Panel::Panel
18 /// \param parent   親ウィジェット
19 ///
20 /// コンストラクタ
21 ///
22 Panel::Panel(QWidget *parent) :
23     QWidget(parent),
24     ui(new Ui::Panel),
25     m_worker()
26 {
27     ui->setupUi(this);
28     ui->progressBar->setVisible(false);
29     ui->scrollArea->setVisible(false);
30 }
31
32 ///////////////////////////////////////////////////////////////////////////////
33 /// \brief Panel::~Panel
34 ///
35 /// デストラクタ
36 ///
37 Panel::~Panel()
38 {
39     delete ui;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////////
43 /// \brief Panel::folderPanel
44 /// \return フォルダパネルを返します。
45 ///
46 FolderPanel *Panel::folderPanel() const
47 {
48     return ui->folderPanel;
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 /// \brief Panel::initialize
53 /// \param w メインウィンドウオブジェクト
54 ///
55 /// パネルを初期化します。
56 ///
57 void Panel::initialize(MainWindow *w)
58 {
59     qDebug() << "Panel::initialize()";
60
61     ui->folderPanel->initialize(w);
62     ui->progressBar->installEventFilter(w);
63     ui->scrollArea->installEventFilter(w);
64 }
65
66 ///////////////////////////////////////////////////////////////////////////////
67 /// \brief Panel::model
68 /// \return フォルダモデルを返します。
69 ///
70 FolderModel *Panel::model() const
71 {
72     return ui->folderPanel->model();
73 }
74
75 ///////////////////////////////////////////////////////////////////////////////
76 /// \brief Panel::setModel
77 /// \param m    設定するモデル
78 ///
79 /// モデルを設定します。
80 ///
81 void Panel::setModel(FolderModel *m)
82 {
83     qDebug() << "Panel::setModel()";
84
85     ui->folderPanel->setModel(m);
86 }
87
88 ///////////////////////////////////////////////////////////////////////////////
89 /// \brief Panel::setViewItem
90 /// \param index    設定するアイテムのインデックス
91 ///
92 /// ビューにアイテムを設定し、表示します。
93 ///
94 void Panel::setViewItem(const QModelIndex &index)
95 {
96     qDebug() << "Panel::setViewItem()" << index;
97
98     if (m_worker) {
99         m_worker->abort();
100     }
101
102     ui->progressBar->setVisible(false);
103     ui->scrollArea->setVisible(false);
104     ui->folderPanel->setVisible(false);
105     if (!index.isValid()) {
106         ui->folderPanel->setVisible(true);
107         return;
108     }
109
110     const FolderModel *m = static_cast<const FolderModel*>(index.model());
111     QString path = m->filePath(index);
112     if (m->isDir(index)) {
113         model()->setRootPath(path);
114         ui->folderPanel->setVisible(true);
115         return;
116     }
117
118     m_worker = new FileReadWorker();
119     connect(m_worker, SIGNAL(size(int)), ui->progressBar, SLOT(setMaximum(int)));
120     connect(m_worker, SIGNAL(progress(int)), ui->progressBar, SLOT(setValue(int)));
121     connect(m_worker, SIGNAL(resultReady(QByteArray)), this, SLOT(onReady(QByteArray)));
122     ui->progressBar->setValue(0);
123     ui->progressBar->setVisible(true);
124
125     m_worker->open(path);
126     m_worker->start();
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 /// \brief Panel::updateAppearance
131 ///
132 /// 外観を変更します。
133 ///
134 void Panel::updateAppearance(const Preferences &prefs)
135 {
136     qDebug() << "Panel::updateAppearance()";
137
138     QPalette pal;
139
140     pal = this->palette();
141     pal.setColor(this->backgroundRole(), prefs.folderViewBgColor(objectName() == "FPanel"));
142     this->setAutoFillBackground(true);
143     this->setPalette(pal);
144
145     ui->folderPanel->updateAppearance(prefs);
146     if (model()) {
147         model()->updateAppearance(prefs);
148     }
149 }
150
151 ///////////////////////////////////////////////////////////////////////////////
152 /// \brief Panel::visibleView
153 /// \return 可視状態のビューを返します。
154 ///
155 QWidget *Panel::visibleView() const
156 {
157     if (ui->scrollArea->isVisible()) {
158         return ui->scrollArea/*->widget()*/;
159     }
160     if (ui->folderPanel->isVisible()) {
161         return ui->folderPanel->itemView();
162     }
163     if (ui->progressBar->isVisible()) {
164         return ui->progressBar;
165     }
166
167     qDebug() << ">>>>>>>>>> visibleView() Logic error <<<<<<<<<<";
168     return NULL;
169 }
170
171 ///////////////////////////////////////////////////////////////////////////////
172 /// \brief Panel::onReady
173 /// \param data ファイルの内容
174 ///
175 /// ファイルの内容を表示します。
176 ///
177 void Panel::onReady(const QByteArray &data)
178 {
179     MainWindow *mainWnd;
180     if (parent()->objectName() == "splitter") {
181         mainWnd = static_cast<MainWindow*>(parent()->parent()->parent());
182     }
183     else {
184         mainWnd = static_cast<MainWindow*>(parent()->parent());
185     }
186     ui->progressBar->setVisible(false);
187     ui->folderPanel->setVisible(false);
188     ui->scrollArea->setVisible(true);
189     QWidget *w = ui->scrollArea->widget();
190     if (w) {
191         delete w;
192     }
193
194     QPixmap pixmap;
195     if (pixmap.loadFromData(data)) {
196         ImageView *view = new ImageView(ui->scrollArea);
197         connect(view, SIGNAL(statusChanged(QString)), mainWnd, SLOT(view_statusChanged(QString)));
198         view->installEventFilter(mainWnd);
199         view->setData(pixmap);
200         emit showed(ui->scrollArea);
201         return;
202     }
203
204     std::string code = detectCode(data.left(1024));
205     QTextCodec *codec = QTextCodec::codecForName(code.c_str());
206     if (codec) {
207         TextView *view = new TextView(ui->scrollArea);
208         connect(view, SIGNAL(copyAvailable(bool)), mainWnd, SLOT(view_copyAvailable(bool)));
209         connect(view, SIGNAL(statusChanged(QString)), mainWnd, SLOT(view_statusChanged(QString)));
210         view->installEventFilter(mainWnd);
211         view->setData(data);
212         emit showed(ui->scrollArea);
213         return;
214     }
215
216     HexView *view = new HexView(ui->scrollArea);
217     connect(view, SIGNAL(copyAvailable(bool)), mainWnd, SLOT(view_copyAvailable(bool)));
218     connect(view, SIGNAL(statusChanged(QString)), mainWnd, SLOT(view_statusChanged(QString)));
219     view->installEventFilter(mainWnd);
220     view->setData(data);
221     emit showed(ui->scrollArea);
222 }