1 #include "preferences.h"
2 #include "bookmarkdialog.h"
3 #include "ui_bookmarkdialog.h"
10 ///////////////////////////////////////////////////////////////////////////////
11 /// \brief BookmarkDialog::BookmarkDialog
12 /// \param parent 親ウィジェット
16 BookmarkDialog::BookmarkDialog(QWidget *parent) :
18 ui(new Ui::BookmarkDialog),
22 resize(parent->width() * 0.8, height());
24 ui->tableWidget->setColumnCount(2);
27 ///////////////////////////////////////////////////////////////////////////////
28 /// \brief BookmarkDialog::~BookmarkDialog
32 BookmarkDialog::~BookmarkDialog()
37 ///////////////////////////////////////////////////////////////////////////////
38 /// \brief BookmarkDialog::setEditMode
39 /// \param edit 編集モードの場合はtrue
41 void BookmarkDialog::setEditMode(bool edit)
43 ui->buttonAdd->setVisible(edit);
44 ui->buttonDelete->setVisible(edit);
45 ui->buttonDown->setVisible(edit);
46 ui->buttonUp->setVisible(edit);
50 setWindowTitle(tr("ブックマークを開く"));
51 connect(ui->tableWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
54 Preferences prefs(this);
55 for (int i = 1; ; i++) {
56 QString name = prefs.getBookmarkEntry(i);
61 QString path = prefs.getBookmarkPath(i);
62 insertData(i - 1, name, path);
64 ui->tableWidget->resizeColumnsToContents();
65 ui->tableWidget->resizeRowsToContents();
66 ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
67 ui->tableWidget->setCurrentCell(0, 0);
70 ///////////////////////////////////////////////////////////////////////////////
71 /// \brief BookmarkDialog::selectedIndex
74 int BookmarkDialog::selectedIndex() const
76 return ui->tableWidget->currentRow();
79 ///////////////////////////////////////////////////////////////////////////////
80 /// \brief BookmarkDialog::insertData
85 /// テーブルウィジェットにデータを追加します。
87 void BookmarkDialog::insertData(int row, const QString &name, const QString &path)
89 ui->tableWidget->insertRow(row);
91 QTableWidgetItem *iName = new QTableWidgetItem(name);
93 iName->setFlags(iName->flags() ^ Qt::ItemIsEditable);
95 ui->tableWidget->setItem(row, 0, iName);
97 QTableWidgetItem *iPath = new QTableWidgetItem(path);
98 iPath->setFlags(iPath->flags() ^ Qt::ItemIsEditable);
99 ui->tableWidget->setItem(row, 1, iPath);
102 ///////////////////////////////////////////////////////////////////////////////
103 /// \brief BookmarkDialog::moveSelectedRows
104 /// \param up 上げる場合はtrue, 下げる場合はfalse
108 void BookmarkDialog::moveSelectedRows(bool up)
110 qDebug() << "BookmarkDialog::moveSelectedRows();" << up;
112 QList<QTableWidgetItem*> selected = ui->tableWidget->selectedItems();
113 if (selected.isEmpty()) {
117 QVector<QString> before;
118 QVector<QString> after;
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;
126 after.resize(before.size());
128 QVector<int> newRows;
129 foreach (QTableWidgetItem *item, selected) {
130 if (item->column() != 0) {
131 int newRow = (up) ? item->row() - 1 : item->row() + 1;
135 else if (newRow >= ui->tableWidget->rowCount()) {
139 after[newRow] = before[item->row()];
140 before[item->row()] = QString::null;
146 for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
147 if (!before[i].isEmpty()) {
148 while (!after[n].isEmpty()) {
151 after[n] = before[i];
155 while (ui->tableWidget->rowCount() > 0) {
156 ui->tableWidget->removeRow(0);
160 foreach (const QString &val, after) {
161 QStringList list = val.split("¥t");
162 insertData(n, list[0], list[1]);
166 ui->tableWidget->resizeColumnsToContents();
167 ui->tableWidget->resizeRowsToContents();
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);
178 ///////////////////////////////////////////////////////////////////////////////
179 /// \brief BookmarkDialog::accept
181 /// OKボタンクリック時の処理を行います。
183 void BookmarkDialog::accept()
185 Preferences prefs(this);
186 prefs.clearBookmark();
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("名前が未入力です。"));
199 index = ui->tableWidget->model()->index(i, 1);
200 QString path = ui->tableWidget->model()->data(index).toString();
202 prefs.addBookmark(name, path);
208 ///////////////////////////////////////////////////////////////////////////////
209 /// \brief BookmarkDialog::on_buttonDelete_clicked
211 /// 削除ボタンクリック時の処理を行います。
213 void BookmarkDialog::on_buttonDelete_clicked()
215 foreach (const QTableWidgetItem *item, ui->tableWidget->selectedItems()) {
216 if (item->column() == 0) {
217 ui->tableWidget->removeRow(item->row());
222 ///////////////////////////////////////////////////////////////////////////////
223 /// \brief BookmarkDialog::on_buttonUp_clicked
225 /// ↑ボタンクリック時の処理を行います。
227 void BookmarkDialog::on_buttonUp_clicked()
229 moveSelectedRows(true);
232 ///////////////////////////////////////////////////////////////////////////////
233 /// \brief BookmarkDialog::on_buttonDown_clicked
235 /// ↓ボタンクリック時の処理を行います。
237 void BookmarkDialog::on_buttonDown_clicked()
239 moveSelectedRows(false);
242 ///////////////////////////////////////////////////////////////////////////////
243 /// \brief BookmarkDialog::on_buttonAdd_clicked
245 /// 追加ボタンクリック時の処理を行います。
247 void BookmarkDialog::on_buttonAdd_clicked()
249 static QString initPath = QDir::homePath();
251 QString path = QFileDialog::getExistingDirectory(
252 this, tr("フォルダを選択"), initPath);
253 if (!path.isEmpty()) {
254 QString name(QFileInfo(path).fileName());
255 if (name.isEmpty()) {
258 insertData(ui->tableWidget->rowCount(), name, path);
261 ui->tableWidget->resizeColumnsToContents();
262 ui->tableWidget->resizeRowsToContents();