Debian Codegraph @

src/gui/tabledialog.cpp

raw symbols SCIP
1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot
10 *
11 * Ophcrack is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Ophcrack is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Ophcrack; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * This program is released under the GPL with the additional exemption
26 * that compiling, linking, and/or using OpenSSL is allowed.
27 *
28 *
29 *
30 *
31 */
32 #include <QHeaderView>
33 #include <QFileDialog>
34 #include <QMessageBox>
35 #include <assert.h>
36
37 #include "tabledialog.h"
38 #include "table.h"
39 //---------------------------------------------------------------------------
40 TableDialog::TableDialog(TableModel *tableModel, QWidget *parent) :
41 QDialog(parent) {
42
43 // Setup the dialog.
44
45 setupUi(this);
46 this->tableModel = tableModel;
47
48 // Setup the table view.
49
50 tableView->setModel(tableModel);
51 tableView->hideColumn(TableModel::PROGRESS_COL_IDX);
52 selectModel = tableView->selectionModel();
53
54 QHeaderView *header = tableView->header();
55 int len = header->length();
56
57 header->resizeSection(0, 4*len/10);
58 header->resizeSection(1, 6*len/10);
59
60 // ...
61
62 connect(upButton, SIGNAL(clicked()), this, SLOT(moveUp()));
63 connect(downButton, SIGNAL(clicked()), this, SLOT(moveDown()));
64 connect(enableButton, SIGNAL(clicked()), this, SLOT(enable()));
65 connect(disableButton, SIGNAL(clicked()), this, SLOT(disable()));
66 connect(installButton, SIGNAL(clicked()), this, SLOT(chooseDir()));
67 }
68 //---------------------------------------------------------------------------
69 void TableDialog::swap(int row1, int row2) {
70 if (row1 == row2) return;
71
72 int max_row = row1 > row2 ? row1 : row2;
73 int min_row = row1 < row2 ? row1 : row2;
74
75 // Take the elements at max_row first because otherwise the row
76 // index changes.
77
78 QList<QStandardItem*> max_items = tableModel->takeRow(max_row);
79 QList<QStandardItem*> min_items = tableModel->takeRow(min_row);
80
81 // Insert the elements at min_row first because otherwise the index
82 // is not correct.
83
84 tableModel->insertRow(min_row, max_items);
85 tableModel->insertRow(max_row, min_items);
86 }
87 //---------------------------------------------------------------------------
88 void TableDialog::moveUp(void) {
89 QList<QModelIndex> indexes = selectModel->selectedRows();
90
91 if (indexes.isEmpty()) return;
92
93 // Retrieve the row of the last selected item.
94
95 QModelIndex index = indexes.last();
96 QStandardItem *item = tableModel->itemFromIndex(index);
97 int row = index.row();
98
99 if (row == 0 || item->parent() != 0) return;
100 selectModel->clearSelection();
101
102 // Record whether the items where expanded or not.
103
104 QModelIndex index_prev = tableModel->index(row-1, TableModel::TABLE_COL_IDX);
105 QModelIndex index_curr = tableModel->index(row, TableModel::TABLE_COL_IDX);
106
107 bool expand_prev = tableView->isExpanded(index_prev);
108 bool expand_curr = tableView->isExpanded(index_curr);
109
110 tableView->setExpanded(index_prev, false);
111 tableView->setExpanded(index_curr, false);
112
113 swap(row-1, row);
114
115 selectModel->select(index_prev,
116 QItemSelectionModel::Select |
117 QItemSelectionModel::Rows);
118
119 tableView->setExpanded(index_curr, expand_prev);
120 tableView->setExpanded(index_prev, expand_curr);
121 }
122 //---------------------------------------------------------------------------
123 void TableDialog::moveDown(void) {
124 QList<QModelIndex> indexes = selectModel->selectedRows();
125
126 if (indexes.isEmpty()) return;
127
128 // Retrieve the row of the last selected item.
129
130 QModelIndex index = indexes.last();
131 QStandardItem *item = tableModel->itemFromIndex(index);
132 int row = index.row();
133
134 if (row == tableModel->rowCount()-1 || item->parent() != 0) return;
135 selectModel->clearSelection();
136
137 // Record whether the items where expanded or not.
138
139 QModelIndex index_curr = tableModel->index(row, 0);
140 QModelIndex index_next = tableModel->index(row+1, 0);
141
142 bool expand_curr = tableView->isExpanded(index_curr);
143 bool expand_next = tableView->isExpanded(index_next);
144
145 tableView->setExpanded(index_next, false);
146 tableView->setExpanded(index_curr, false);
147
148 swap(row, row+1);
149
150 selectModel->select(index_next,
151 QItemSelectionModel::Select |
152 QItemSelectionModel::Rows);
153
154 tableView->setExpanded(index_curr, expand_next);
155 tableView->setExpanded(index_next, expand_curr);
156 }
157 //---------------------------------------------------------------------------
158 void TableDialog::enable(void) {
159 QList<QModelIndex> indexes = selectModel->selectedRows();
160
161 if (indexes.isEmpty()) return;
162
163 while (!indexes.isEmpty()) {
164 QModelIndex index = indexes.takeFirst();
165 QStandardItem *item = tableModel->itemFromIndex(index);
166
167 tableModel->enable(item, true);
168 }
169 }
170 //---------------------------------------------------------------------------
171 void TableDialog::disable(void) {
172 QList<QModelIndex> indexes = selectModel->selectedRows();
173
174 if (indexes.isEmpty()) return;
175
176 while (!indexes.isEmpty()) {
177 QModelIndex index = indexes.takeFirst();
178 QStandardItem *item = tableModel->itemFromIndex(index);
179
180 tableModel->enable(item, false);
181 }
182 }
183 //---------------------------------------------------------------------------
184 void TableDialog::chooseDir(void) {
185 QString path = QFileDialog::getExistingDirectory(this,
186 tr("Select the directory which contains the tables."),
187 QString(),
188 QFileDialog::ShowDirsOnly);
189
190 if (!path.isNull()) emit install(path, true, true);
191 }