-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hid.cpp
268 lines (234 loc) · 6.69 KB
/
Hid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "Hid.hpp"
#include "App.hpp"
#include "gui/IconView.hpp"
#include "gui/Tab.hpp"
#include "gui/Table.hpp"
#include "gui/TableModel.hpp"
#include "io/File.hpp"
#include "io/Files.hpp"
namespace cornus {
Hid::Hid(App *app) : app_(app)
{
Q_UNUSED(app_);
}
Hid::~Hid()
{}
void Hid::HandleKeySelect(gui::Tab *tab, const VDirection vdir,
const int key, const Qt::KeyboardModifiers modifiers)
{
if (modifiers & Qt::ControlModifier)
return;
io::Files &files = tab->view_files();
const int file_count = files.cached_files_count;
gui::ShiftSelect *shift_select = tab->ShiftSelect();
MTL_CHECK_VOID(shift_select != nullptr);
int old_file_index = files.GetFirstSelectedFile(Lock::Yes);
if (old_file_index == -1)
old_file_index = shift_select->base_row;
cbool was_set_to_zero = (old_file_index == -1);
if (was_set_to_zero)
old_file_index = 0;
if (vdir == VDirection::Up && (!was_set_to_zero && old_file_index == 0))
return;
if (vdir == VDirection::Down && old_file_index >= file_count - 1)
return;
cbool is_icon_view = tab->view_mode() == gui::ViewMode::Icons;
cbool up_or_down = key == Qt::Key_Up || key == Qt::Key_Down;
int new_file_index = -1;
QSet<int> indices;
{
MutexGuard guard = files.guard();
files.SelectAllFiles(Lock::No, Selected::No, indices);
if (was_set_to_zero)
{
new_file_index = (vdir == VDirection::Up) ? 0 : file_count - 1;
if (new_file_index >= 0)
{
indices.insert(new_file_index);
files.data.vec[new_file_index]->set_selected(true);
}
} else {
if (is_icon_view && up_or_down)
{
const int n = was_set_to_zero ? -1 : old_file_index;
new_file_index = tab->icon_view()->CellIndexInNextRow(n, vdir);
} else {
new_file_index = old_file_index + ((vdir == VDirection::Up) ? -1 : 1);
}
if (new_file_index >= 0 && new_file_index < file_count)
{
io::File *file = files.data.vec[old_file_index];
file->set_selected(false);
files.data.vec[new_file_index]->set_selected(true);
indices.insert(new_file_index);
indices.insert(old_file_index);
}
}
}
if (new_file_index != -1) {
tab->ScrollToFile(new_file_index);
}
tab->UpdateIndices(indices);
}
void Hid::HandleKeyShiftSelect(gui::Tab *tab, const VDirection vdir,
const int key)
{
auto &files = tab->view_files();
int row = files.GetFirstSelectedFile(Lock::Yes);
if (row == -1)
return;
QSet<int> indices;
gui::ShiftSelect *shift_select = tab->ShiftSelect();
if (shift_select->head_row == -1)
shift_select->head_row = shift_select->base_row;
if (shift_select->base_row == -1) {
shift_select->base_row = row;
shift_select->head_row = row;
MutexGuard guard = files.guard();
files.data.vec[row]->set_selected(true);
indices.insert(row);
} else {
if (vdir == VDirection::Up) {
if (shift_select->head_row == 0)
return;
shift_select->head_row--;
} else {
const int count = tab->table_model()->rowCount();
if (shift_select->head_row == count -1)
return;
shift_select->head_row++;
}
{
MutexGuard guard = files.guard();
files.SelectAllFiles(Lock::No, Selected::No, indices);
files.SelectFileRange(Lock::No, shift_select->base_row,
shift_select->head_row, indices);
}
}
if (shift_select->head_row != -1) {
tab->ScrollToFile(shift_select->head_row);
}
tab->UpdateIndices(indices);
}
void Hid::HandleMouseSelectionShift(gui::Tab *tab, const QPoint &pos,
QSet<int> &indices)
{
io::Files &files = tab->view_files();
MutexGuard guard = files.guard();
io::File *file = nullptr;
int file_index = -1;
gui::ShiftSelect *shift_select = nullptr;
if (tab->view_mode() == gui::ViewMode::Details)
{
file_index = tab->table()->GetFileAt_NoLock(pos, PickedBy::VisibleName, &file);
shift_select = tab->table()->shift_select();
} else {
file = tab->icon_view()->GetFileAt_NoLock(pos, Clone::No, &file_index);
shift_select = tab->icon_view()->shift_select();
}
bool on_file = file_index != -1;
if (on_file)
{
if (shift_select->base_row == -1)
{
shift_select->base_row = file_index;
file->set_selected(true);
indices.insert(file_index);
} else {
files.SelectAllFiles(Lock::No, Selected::No, indices);
files.SelectFileRange(Lock::No, shift_select->base_row, file_index, indices);
}
}
}
void Hid::HandleMouseSelectionCtrl(gui::Tab *tab, const QPoint &pos,
QSet<int> *indices)
{
const gui::ViewMode view_mode = tab->view_mode();
io::Files &files = tab->view_files();
MutexGuard guard = files.guard();
io::File *file = nullptr;
int row = -1;
if (view_mode == gui::ViewMode::Details)
{
row = tab->table()->GetFileAt_NoLock(pos, PickedBy::VisibleName, &file);
} else if (view_mode == gui::ViewMode::Icons) {
file = tab->icon_view()->GetFileAt_NoLock(pos, Clone::No, &row);
}
if (file != nullptr)
{
file->toggle_selected();
if (indices != nullptr && row != -1)
indices->insert(row);
}
}
void Hid::HandleMouseSelectionNoModif(gui::Tab *tab, const QPoint &pos, QSet<int> &indices,
bool mouse_pressed, gui::ShiftSelect *shift_select)
{
const gui::ViewMode view_mode = tab->view_mode();
io::Files &files = tab->view_files();
auto g = files.guard();
io::File *file = nullptr;
int file_index = -1;
if (view_mode == gui::ViewMode::Details)
{
file_index = tab->table()->GetFileAt_NoLock(pos, PickedBy::VisibleName, &file);
if (file_index == -1) {
file_index = tab->table()->GetFileAt_NoLock(pos, PickedBy::Icon, &file);
}
} else if (view_mode == gui::ViewMode::Icons) {
file = tab->icon_view()->GetFileAt_NoLock(pos, Clone::No, &file_index);
}
if (shift_select)
shift_select->base_row = file_index;
if (file_index != -1)
{
if (mouse_pressed)
{
if (file == nullptr || !file->is_selected()) {
files.SelectAllFiles(Lock::No, Selected::No, indices);
}
if (file != nullptr && !file->is_selected() && file_index != -1) {
file->selected(Selected::Yes);
indices.insert(file_index);
}
}
} else {
files.SelectAllFiles(Lock::No, Selected::No, indices);
}
}
void Hid::SelectFileByIndex(gui::Tab *tab, const int file_index,
const DeselectOthers des)
{
io::Files &files = tab->view_files();
QSet<int> indices;
{
MutexGuard guard = files.guard();
auto &vec = files.data.vec;
if (des == DeselectOthers::Yes)
{
int i = -1;
for (io::File *next: vec)
{
i++;
if (i != file_index && next->is_selected())
{
next->toggle_selected();
indices.insert(i);
}
}
}
if (file_index >= 0 && file_index < vec.size())
{
vec[file_index]->selected(Selected::Yes);
indices.insert(file_index);
}
}
const gui::ViewMode view_mode = tab->view_mode();
if (view_mode == gui::ViewMode::Details)
{
tab->table_model()->UpdateIndices(indices);
} else if (view_mode == gui::ViewMode::Icons) {
tab->icon_view()->UpdateIndices(indices);
}
}
}