1
/**************************************************************************
3
* Copyright 2014 Canonical Ltd.
4
* Copyright 2014 Carlos J Mazieri <carlos.mazieri@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation; version 3.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
* File: dirselection.cpp
22
#include "dirselection.h"
23
#include "diritemabstractlistmodel.h"
28
#define VALID_INDEX(index) (index >= 0 && index < m_model->rowCount())
30
DirSelection::DirSelection(QObject *parent) : QObject(parent)
34
DirSelection::DirSelection(DirItemAbstractListModel *parent, DirItemInfoList *listItems) :
38
,m_listItems(listItems)
40
,m_lastSelectedItem(-1)
46
QStringList DirSelection::selectedAbsFilePaths() const
49
int counter = m_model->rowCount();
50
for(int index = 0 ; index < counter; ++index)
52
if (m_listItems->at(index).isSelected())
54
ret.append(m_listItems->at(index).absoluteFilePath());
60
QStringList DirSelection::selectedNames() const
63
int counter = m_model->rowCount();
64
for(int index = 0 ; index < counter; ++index)
66
if (m_listItems->at(index).isSelected())
68
ret.append(m_listItems->at(index).fileName());
76
QList<int> DirSelection::selectedIndexes() const
79
int counter = m_model->rowCount();
80
for(int index = 0 ; index < counter; ++index)
82
if (m_listItems->at(index).isSelected())
91
void DirSelection::clear()
100
bool DirSelection::priv_clear()
102
bool notify = m_selectedCounter != 0;
105
int counter = m_model->rowCount();
106
DirItemInfo *data = m_listItems->data();
107
while (m_selectedCounter > 0 && counter-- )
109
if ( data[counter].setSelection(false) )
112
m_model->notifyItemChanged(counter);
116
//force it to zero, works when cleaning the buffer first
117
m_selectedCounter = 0;
118
m_lastSelectedItem = -1;
123
void DirSelection::selectAll()
125
int counter = m_model->rowCount();
126
bool notify = m_selectedCounter != counter;
129
DirItemInfo *data = m_listItems->data();
132
if ( data[counter].setSelection(true) )
135
m_model->notifyItemChanged(counter);
143
int DirSelection::counter() const
145
return m_selectedCounter;
149
DirSelection::Mode DirSelection::mode() const
155
void DirSelection::itemGoingToBeRemoved(const DirItemInfo &item)
157
if (m_selectedCounter > 0 && item.isSelected())
162
// item is going to be removed, no QAbstractItemModel::dataChanged() signal is necessary to refresh views
166
void DirSelection::setIndex(int index, bool selected)
168
if (VALID_INDEX(index))
170
int old_selectedCounter = m_selectedCounter;
171
if (selected && m_mode == Single && m_selectedCounter > 0)
175
if ( priv_setIndex(index, selected)
176
|| old_selectedCounter != m_selectedCounter
185
void DirSelection::toggleIndex(int index)
187
if (VALID_INDEX(index))
189
setIndex(index, !m_listItems->at(index).isSelected());
194
void DirSelection::setMode(Mode m)
199
emit modeChanged(m_mode);
204
void DirSelection::notifyChanges()
206
emit selectionChanged(m_selectedCounter);
211
* \brief DirSelection::itemGoingToBeReplaced() it is supposed to control selection writable and readabble states
213
* So far it does nothing
218
void DirSelection::itemGoingToBeReplaced(const DirItemInfo &oldItemInfo,
219
const DirItemInfo &newItemInfo)
221
if (oldItemInfo.isSelected())
223
// we may add selection writable state in the future
224
Q_UNUSED(newItemInfo);
229
void DirSelection::selectRange(int indexClicked)
231
bool changed = false;
232
if ( VALID_INDEX(indexClicked)
233
&& m_selectedCounter > 0
234
&& indexClicked != m_lastSelectedItem
235
&& VALID_INDEX(m_lastSelectedItem)
236
&& !m_listItems->at(indexClicked).isSelected()
239
//go from indexClicked to m_lastSelectedItem
240
int increment = indexClicked > m_lastSelectedItem? -1 : 1;
241
int nextItem = indexClicked;
242
int saved_lastSelectedItem = m_lastSelectedItem;
243
while (priv_setIndex(nextItem, true) && nextItem != saved_lastSelectedItem)
245
nextItem += increment;
256
bool DirSelection::priv_setIndex(int index, bool selected)
258
DirItemInfo *data = m_listItems->data();
259
bool changed = false;
260
if ((changed = data[index].setSelection(selected)))
262
m_model->notifyItemChanged(index);
266
m_lastSelectedItem = index;
277
void DirSelection::select(int index, bool range, bool multiSelection )
279
if (range && VALID_INDEX(m_lastSelectedItem))
285
if (multiSelection || m_mode == Multi)
287
Mode saveMode = m_mode;
288
//set Multi selection do not call clear()
295
setIndex(index, true);
301
void DirSelection::setMultiSelection(bool enable)
303
Mode m = enable ? Multi : Single;