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 BookmarkDialog::BookmarkDialog(QWidget *parent) :
11     QDialog(parent),
12     ui(new Ui::BookmarkDialog),
13     m_isReadOnly(false)
14 {
15     ui->setupUi(this);
16     resize(parent->width() * 0.8, height());
17
18     ui->tableWidget->setColumnCount(2);
19 }
20
21 BookmarkDialog::~BookmarkDialog()
22 {
23     delete ui;
24 }
25
26 void BookmarkDialog::setEditMode(bool edit)
27 {
28     ui->buttonAdd->setVisible(edit);
29     ui->buttonDelete->setVisible(edit);
30     ui->buttonDown->setVisible(edit);
31     ui->buttonUp->setVisible(edit);
32
33     m_isReadOnly = !edit;
34     if (m_isReadOnly) {
35         setWindowTitle(tr("ブックマークを開く"));
36         connect(ui->tableWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
37     }
38
39     Preferences prefs(this);
40     for (int i = 1; ; i++) {
41         QString name = prefs.getBookmarkEntry(i);
42         if (name.isEmpty()) {
43             break;
44         }
45
46         QString path = prefs.getBookmarkPath(i);
47         insertData(i - 1, name, path);
48     }
49     ui->tableWidget->resizeColumnsToContents();
50     ui->tableWidget->resizeRowsToContents();
51     ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
52     ui->tableWidget->setCurrentCell(0, 0);
53 }
54
55 int BookmarkDialog::selectedIndex() const
56 {
57     return ui->tableWidget->currentRow();
58 }
59
60 void BookmarkDialog::insertData(int row, const QString &name, const QString &path)
61 {
62     ui->tableWidget->insertRow(row);
63
64     QTableWidgetItem *iName = new QTableWidgetItem(name);
65     if (m_isReadOnly) {
66         iName->setFlags(iName->flags() ^ Qt::ItemIsEditable);
67     }
68     ui->tableWidget->setItem(row, 0, iName);
69
70     QTableWidgetItem *iPath = new QTableWidgetItem(path);
71     iPath->setFlags(iPath->flags() ^ Qt::ItemIsEditable);
72     ui->tableWidget->setItem(row, 1, iPath);
73 }
74
75 void BookmarkDialog::moveSelectedRows(bool up)
76 {
77     qDebug() << "BookmarkDialog::moveSelectedRows();" << up;
78
79     QList<QTableWidgetItem*> selected = ui->tableWidget->selectedItems();
80     if (selected.isEmpty()) {
81         qDebug() << "Not selected.";
82         return;
83     }
84
85     QVector<QString> before;
86     QVector<QString> after;
87
88     for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
89         QString name = ui->tableWidget->item(i, 0)->data(Qt::DisplayRole).toString();
90         QString path = ui->tableWidget->item(i, 1)->data(Qt::DisplayRole).toString();
91         before << name + "¥t" + path;
92     }
93     qDebug() << "set before" << before;
94
95     after.resize(before.size());
96
97     QVector<int> newRows;
98     foreach (QTableWidgetItem *item, selected) {
99         if (item->column() != 0) {
100             int newRow = (up) ? item->row() - 1 : item->row() + 1;
101             if (newRow < 0) {
102                 return;
103             }
104             else if (newRow >= ui->tableWidget->rowCount()) {
105                 return;
106             }
107
108             after[newRow] = before[item->row()];
109             before[item->row()] = QString::null;
110             newRows << newRow;
111         }
112     }
113
114     qDebug() << "set after(1)" << after;
115     int n = 0;
116     for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
117         if (!before[i].isEmpty()) {
118             while (!after[n].isEmpty()) {
119                 n++;
120             }
121             after[n] = before[i];
122         }
123     }
124     qDebug() << "set after(2)" << after;
125
126     while (ui->tableWidget->rowCount() > 0) {
127         ui->tableWidget->removeRow(0);
128     }
129
130     n = 0;
131     foreach (const QString &val, after) {
132         QStringList list = val.split("¥t");
133         insertData(n, list[0], list[1]);
134         n++;
135     }
136
137     ui->tableWidget->resizeColumnsToContents();
138     ui->tableWidget->resizeRowsToContents();
139
140     foreach (int i, newRows) {
141         for (n = 0; n < 2; n++) {
142             QModelIndex index = ui->tableWidget->model()->index(i, n);
143             ui->tableWidget->selectionModel()->select(index, QItemSelectionModel::Select);
144         }
145     }
146
147 }
148
149 void BookmarkDialog::accept()
150 {
151     Preferences prefs(this);
152     prefs.clearBookmark();
153
154     QModelIndex index;
155     int i;
156     for (i = 0; i < ui->tableWidget->rowCount(); i++) {
157         index = ui->tableWidget->model()->index(i, 0);
158         QString name = ui->tableWidget->model()->data(index).toString();
159         if (name.isEmpty()) {
160             ui->tableWidget->setCurrentCell(i, 0);
161             QMessageBox::critical(this, tr("エラー"), tr("名前が未入力です。"));
162             return;
163         }
164
165         index = ui->tableWidget->model()->index(i, 1);
166         QString path = ui->tableWidget->model()->data(index).toString();
167
168         prefs.addBookmark(name, path);
169     }
170
171     QDialog::accept();
172 }
173
174 void BookmarkDialog::on_buttonDelete_clicked()
175 {
176     foreach (const QTableWidgetItem *item, ui->tableWidget->selectedItems()) {
177         if (item->column() == 0) {
178             ui->tableWidget->removeRow(item->row());
179         }
180     }
181 }
182
183 void BookmarkDialog::on_buttonUp_clicked()
184 {
185     moveSelectedRows(true);
186 }
187
188 void BookmarkDialog::on_buttonDown_clicked()
189 {
190     moveSelectedRows(false);
191 }
192
193 void BookmarkDialog::on_buttonAdd_clicked()
194 {
195     static QString initPath = QDir::homePath();
196
197     QString path = QFileDialog::getExistingDirectory(
198                 this, tr("フォルダを選択"), initPath);
199     if (!path.isEmpty()) {
200         QFileInfo info(path);
201         QString name(info.fileName());
202         if (name.isEmpty()) {
203             name = "root";
204         }
205         insertData(ui->tableWidget->rowCount(), name, path);
206         initPath = path;
207
208         ui->tableWidget->resizeColumnsToContents();
209         ui->tableWidget->resizeRowsToContents();
210     }
211 }