OSDN Git Service

内蔵テキストビューアの行間を設定できるようにした
[gefu/Gefu.git] / bookmarkdialog.cpp
1 #include "preferences.h"
2 #include "bookmarkdialog.h"
3 #include "ui_bookmarkdialog.h"
4
5 #include <QDebug>
6 #include <QFileDialog>
7 #include <QFileInfo>
8 #include <QMessageBox>
9
10 ///////////////////////////////////////////////////////////////////////////////
11 /// \brief BookmarkDialog::BookmarkDialog
12 /// \param parent   親ウィジェット
13 ///
14 /// コンストラクタ
15 ///
16 BookmarkDialog::BookmarkDialog(QWidget *parent) :
17     QDialog(parent),
18     ui(new Ui::BookmarkDialog),
19     m_isReadOnly(false)
20 {
21     ui->setupUi(this);
22     resize(parent->width() * 0.8, height());
23
24     ui->tableWidget->setColumnCount(2);
25 }
26
27 ///////////////////////////////////////////////////////////////////////////////
28 /// \brief BookmarkDialog::~BookmarkDialog
29 ///
30 /// デストラクタ
31 ///
32 BookmarkDialog::~BookmarkDialog()
33 {
34     delete ui;
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38 /// \brief BookmarkDialog::setEditMode
39 /// \param edit 編集モードの場合はtrue
40 ///
41 void BookmarkDialog::setEditMode(bool edit)
42 {
43     ui->buttonAdd->setVisible(edit);
44     ui->buttonDelete->setVisible(edit);
45     ui->buttonDown->setVisible(edit);
46     ui->buttonUp->setVisible(edit);
47
48     m_isReadOnly = !edit;
49     if (m_isReadOnly) {
50         setWindowTitle(tr("ブックマークを開く"));
51         connect(ui->tableWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
52     }
53
54     Preferences prefs(this);
55     for (int i = 1; ; i++) {
56         QString name = prefs.getBookmarkEntry(i);
57         if (name.isEmpty()) {
58             break;
59         }
60
61         QString path = prefs.getBookmarkPath(i);
62         insertData(i - 1, name, path);
63     }
64     ui->tableWidget->resizeColumnsToContents();
65     ui->tableWidget->resizeRowsToContents();
66     ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
67     ui->tableWidget->setCurrentCell(0, 0);
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71 /// \brief BookmarkDialog::selectedIndex
72 /// \return 現在の行番号
73 ///
74 int BookmarkDialog::selectedIndex() const
75 {
76     return ui->tableWidget->currentRow();
77 }
78
79 ///////////////////////////////////////////////////////////////////////////////
80 /// \brief BookmarkDialog::insertData
81 /// \param row  行番号
82 /// \param name 名前
83 /// \param path パス
84 ///
85 /// テーブルウィジェットにデータを追加します。
86 ///
87 void BookmarkDialog::insertData(int row, const QString &name, const QString &path)
88 {
89     ui->tableWidget->insertRow(row);
90
91     QTableWidgetItem *iName = new QTableWidgetItem(name);
92     if (m_isReadOnly) {
93         iName->setFlags(iName->flags() ^ Qt::ItemIsEditable);
94     }
95     ui->tableWidget->setItem(row, 0, iName);
96
97     QTableWidgetItem *iPath = new QTableWidgetItem(path);
98     iPath->setFlags(iPath->flags() ^ Qt::ItemIsEditable);
99     ui->tableWidget->setItem(row, 1, iPath);
100 }
101
102 ///////////////////////////////////////////////////////////////////////////////
103 /// \brief BookmarkDialog::moveSelectedRows
104 /// \param up   上げる場合はtrue, 下げる場合はfalse
105 ///
106 /// 選択行を上または下に移動します。
107 ///
108 void BookmarkDialog::moveSelectedRows(bool up)
109 {
110     qDebug() << "BookmarkDialog::moveSelectedRows();" << up;
111
112     QList<QTableWidgetItem*> selected = ui->tableWidget->selectedItems();
113     if (selected.isEmpty()) {
114         return;
115     }
116
117     QVector<QString> before;
118     QVector<QString> after;
119
120     for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
121         QString name = ui->tableWidget->item(i, 0)->data(Qt::DisplayRole).toString();
122         QString path = ui->tableWidget->item(i, 1)->data(Qt::DisplayRole).toString();
123         before << name + "¥t" + path;
124     }
125
126     after.resize(before.size());
127
128     QVector<int> newRows;
129     foreach (QTableWidgetItem *item, selected) {
130         if (item->column() != 0) {
131             int newRow = (up) ? item->row() - 1 : item->row() + 1;
132             if (newRow < 0) {
133                 return;
134             }
135             else if (newRow >= ui->tableWidget->rowCount()) {
136                 return;
137             }
138
139             after[newRow] = before[item->row()];
140             before[item->row()] = QString::null;
141             newRows << newRow;
142         }
143     }
144
145     int n = 0;
146     for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
147         if (!before[i].isEmpty()) {
148             while (!after[n].isEmpty()) {
149                 n++;
150             }
151             after[n] = before[i];
152         }
153     }
154
155     while (ui->tableWidget->rowCount() > 0) {
156         ui->tableWidget->removeRow(0);
157     }
158
159     n = 0;
160     foreach (const QString &val, after) {
161         QStringList list = val.split("¥t");
162         insertData(n, list[0], list[1]);
163         n++;
164     }
165
166     ui->tableWidget->resizeColumnsToContents();
167     ui->tableWidget->resizeRowsToContents();
168
169     foreach (int i, newRows) {
170         for (n = 0; n < 2; n++) {
171             QModelIndex index = ui->tableWidget->model()->index(i, n);
172             ui->tableWidget->selectionModel()->select(index, QItemSelectionModel::Select);
173         }
174     }
175
176 }
177
178 ///////////////////////////////////////////////////////////////////////////////
179 /// \brief BookmarkDialog::accept
180 ///
181 /// OKボタンクリック時の処理を行います。
182 ///
183 void BookmarkDialog::accept()
184 {
185     Preferences prefs(this);
186     prefs.clearBookmark();
187
188     QModelIndex index;
189     int i;
190     for (i = 0; i < ui->tableWidget->rowCount(); i++) {
191         index = ui->tableWidget->model()->index(i, 0);
192         QString name = ui->tableWidget->model()->data(index).toString();
193         if (name.isEmpty()) {
194             ui->tableWidget->setCurrentCell(i, 0);
195             QMessageBox::critical(this, tr("エラー"), tr("名前が未入力です。"));
196             return;
197         }
198
199         index = ui->tableWidget->model()->index(i, 1);
200         QString path = ui->tableWidget->model()->data(index).toString();
201
202         prefs.addBookmark(name, path);
203     }
204
205     QDialog::accept();
206 }
207
208 ///////////////////////////////////////////////////////////////////////////////
209 /// \brief BookmarkDialog::on_buttonDelete_clicked
210 ///
211 /// 削除ボタンクリック時の処理を行います。
212 ///
213 void BookmarkDialog::on_buttonDelete_clicked()
214 {
215     foreach (const QTableWidgetItem *item, ui->tableWidget->selectedItems()) {
216         if (item->column() == 0) {
217             ui->tableWidget->removeRow(item->row());
218         }
219     }
220 }
221
222 ///////////////////////////////////////////////////////////////////////////////
223 /// \brief BookmarkDialog::on_buttonUp_clicked
224 ///
225 /// ↑ボタンクリック時の処理を行います。
226 ///
227 void BookmarkDialog::on_buttonUp_clicked()
228 {
229     moveSelectedRows(true);
230 }
231
232 ///////////////////////////////////////////////////////////////////////////////
233 /// \brief BookmarkDialog::on_buttonDown_clicked
234 ///
235 /// ↓ボタンクリック時の処理を行います。
236 ///
237 void BookmarkDialog::on_buttonDown_clicked()
238 {
239     moveSelectedRows(false);
240 }
241
242 ///////////////////////////////////////////////////////////////////////////////
243 /// \brief BookmarkDialog::on_buttonAdd_clicked
244 ///
245 /// 追加ボタンクリック時の処理を行います。
246 ///
247 void BookmarkDialog::on_buttonAdd_clicked()
248 {
249     static QString initPath = QDir::homePath();
250
251     QString path = QFileDialog::getExistingDirectory(
252                 this, tr("フォルダを選択"), initPath);
253     if (!path.isEmpty()) {
254         QString name(QFileInfo(path).fileName());
255         if (name.isEmpty()) {
256             name = "root";
257         }
258         insertData(ui->tableWidget->rowCount(), name, path);
259         initPath = path;
260
261         ui->tableWidget->resizeColumnsToContents();
262         ui->tableWidget->resizeRowsToContents();
263     }
264 }