34
34
#include <lib/document/documentfactory.h>
35
35
#include <lib/semanticinfo/sorteddirmodel.h>
39
40
struct ContextManagerPrivate {
40
QList<AbstractContextManagerItem*> mList;
41
SortedDirModel* mDirModel;
42
QItemSelectionModel* mSelectionModel;
47
bool mSelectedFileItemListNeedsUpdate;
48
QSet<QByteArray> mQueuedSignals;
49
KFileItemList mSelectedFileItemList;
51
QTimer* mQueuedSignalsTimer;
53
void queueSignal(const QByteArray& signal) {
54
mQueuedSignals << signal;
55
mQueuedSignalsTimer->start();
58
void updateSelectedFileItemList() {
59
if (!mSelectedFileItemListNeedsUpdate) {
62
mSelectedFileItemList.clear();
63
if (!mOnlyCurrentUrl) {
64
QItemSelection selection = mSelectionModel->selection();
65
Q_FOREACH(const QModelIndex& index, selection.indexes()) {
66
mSelectedFileItemList << mDirModel->itemForIndex(index);
70
// At least add current url if it's valid (it may not be in
71
// the list if we are viewing a non-browsable url, for example
72
// using http protocol)
73
if (mSelectedFileItemList.isEmpty() && mCurrentUrl.isValid()) {
74
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, mCurrentUrl);
75
mSelectedFileItemList << item;
78
mSelectedFileItemListNeedsUpdate = false;
41
QList<AbstractContextManagerItem*> mList;
42
SortedDirModel* mDirModel;
43
QItemSelectionModel* mSelectionModel;
48
bool mSelectedFileItemListNeedsUpdate;
49
QSet<QByteArray> mQueuedSignals;
50
KFileItemList mSelectedFileItemList;
52
QTimer* mQueuedSignalsTimer;
54
void queueSignal(const QByteArray& signal)
56
mQueuedSignals << signal;
57
mQueuedSignalsTimer->start();
60
void updateSelectedFileItemList()
62
if (!mSelectedFileItemListNeedsUpdate) {
65
mSelectedFileItemList.clear();
66
if (!mOnlyCurrentUrl) {
67
QItemSelection selection = mSelectionModel->selection();
68
Q_FOREACH(const QModelIndex & index, selection.indexes()) {
69
mSelectedFileItemList << mDirModel->itemForIndex(index);
73
// At least add current url if it's valid (it may not be in
74
// the list if we are viewing a non-browsable url, for example
75
// using http protocol)
76
if (mSelectedFileItemList.isEmpty() && mCurrentUrl.isValid()) {
77
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, mCurrentUrl);
78
mSelectedFileItemList << item;
81
mSelectedFileItemListNeedsUpdate = false;
83
85
ContextManager::ContextManager(SortedDirModel* dirModel, QItemSelectionModel* selectionModel, QObject* parent)
85
87
, d(new ContextManagerPrivate)
87
d->mQueuedSignalsTimer = new QTimer(this);
88
d->mQueuedSignalsTimer->setInterval(100);
89
d->mQueuedSignalsTimer->setSingleShot(true);
90
connect(d->mQueuedSignalsTimer, SIGNAL(timeout()),
91
SLOT(emitQueuedSignals()) );
93
d->mDirModel = dirModel;
94
connect(d->mDirModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
95
SLOT(slotDirModelDataChanged(const QModelIndex&, const QModelIndex&)) );
97
d->mSelectionModel = selectionModel;
98
connect(d->mSelectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
99
SLOT(slotSelectionChanged()) );
100
connect(d->mSelectionModel, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
101
SLOT(slotCurrentChanged(const QModelIndex&)) );
103
d->mSelectedFileItemListNeedsUpdate = false;
104
d->mOnlyCurrentUrl = false;
108
ContextManager::~ContextManager() {
109
qDeleteAll(d->mList);
114
void ContextManager::addItem(AbstractContextManagerItem* item) {
119
void ContextManager::setCurrentUrl(const KUrl& currentUrl) {
120
if (d->mCurrentUrl == currentUrl) {
124
d->mCurrentUrl = currentUrl;
125
Document::Ptr doc = DocumentFactory::instance()->load(currentUrl);
126
QUndoGroup* undoGroup = DocumentFactory::instance()->undoGroup();
127
undoGroup->addStack(doc->undoStack());
128
undoGroup->setActiveStack(doc->undoStack());
130
if (d->mOnlyCurrentUrl) {
131
d->mSelectedFileItemListNeedsUpdate = true;
134
d->queueSignal("selectionChanged");
138
KFileItemList ContextManager::selectedFileItemList() const {
139
d->updateSelectedFileItemList();
140
return d->mSelectedFileItemList;
144
void ContextManager::setCurrentDirUrl(const KUrl& url) {
145
if (url.equals(d->mCurrentDirUrl, KUrl::CompareWithoutTrailingSlash)) {
148
d->mCurrentDirUrl = url;
149
currentDirUrlChanged();
153
KUrl ContextManager::currentDirUrl() const {
154
return d->mCurrentDirUrl;
158
KUrl ContextManager::currentUrl() const {
159
return d->mCurrentUrl;
163
SortedDirModel* ContextManager::dirModel() const {
168
void ContextManager::slotDirModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) {
169
// Data change can happen in the following cases:
170
// - items have been renamed
171
// - item bytes have been modified
172
// - item meta info has been retrieved or modified
174
// If a selected item is affected, schedule emission of a
175
// selectionDataChanged() signal. Don't emit it directly to avoid spamming
176
// the context items in case of a mass change.
177
QModelIndexList selectionList = d->mSelectionModel->selectedIndexes();
178
if (selectionList.isEmpty()) {
182
QModelIndexList changedList;
183
for (int row=topLeft.row(); row <= bottomRight.row(); ++row) {
184
changedList << d->mDirModel->index(row, 0);
187
QModelIndexList& shortList = selectionList;
188
QModelIndexList& longList = changedList;
189
if (shortList.length() > longList.length()) {
190
qSwap(shortList, longList);
192
Q_FOREACH(const QModelIndex& index, shortList) {
193
if (longList.contains(index)) {
194
d->mSelectedFileItemListNeedsUpdate = true;
195
d->queueSignal("selectionDataChanged");
202
void ContextManager::slotSelectionChanged() {
203
d->mSelectedFileItemListNeedsUpdate = true;
204
d->queueSignal("selectionChanged");
208
void Gwenview::ContextManager::slotCurrentChanged(const QModelIndex& index) {
209
KUrl url = d->mDirModel->urlForIndex(index);
214
void ContextManager::emitQueuedSignals() {
215
Q_FOREACH(const QByteArray& signal, d->mQueuedSignals) {
216
QMetaObject::invokeMethod(this, signal.data());
218
d->mQueuedSignals.clear();
222
void ContextManager::setOnlyCurrentUrl(bool onlyCurrentUrl) {
223
if (d->mOnlyCurrentUrl != onlyCurrentUrl) {
224
d->mOnlyCurrentUrl = onlyCurrentUrl;
225
d->mSelectedFileItemListNeedsUpdate = true;
226
d->queueSignal("selectionChanged");
89
d->mQueuedSignalsTimer = new QTimer(this);
90
d->mQueuedSignalsTimer->setInterval(100);
91
d->mQueuedSignalsTimer->setSingleShot(true);
92
connect(d->mQueuedSignalsTimer, SIGNAL(timeout()),
93
SLOT(emitQueuedSignals()));
95
d->mDirModel = dirModel;
96
connect(d->mDirModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
97
SLOT(slotDirModelDataChanged(QModelIndex, QModelIndex)));
99
d->mSelectionModel = selectionModel;
100
connect(d->mSelectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
101
SLOT(slotSelectionChanged()));
102
connect(d->mSelectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
103
SLOT(slotCurrentChanged(QModelIndex)));
105
d->mSelectedFileItemListNeedsUpdate = false;
106
d->mOnlyCurrentUrl = false;
109
ContextManager::~ContextManager()
111
qDeleteAll(d->mList);
115
void ContextManager::addItem(AbstractContextManagerItem* item)
120
void ContextManager::setCurrentUrl(const KUrl& currentUrl)
122
if (d->mCurrentUrl == currentUrl) {
126
d->mCurrentUrl = currentUrl;
127
Document::Ptr doc = DocumentFactory::instance()->load(currentUrl);
128
QUndoGroup* undoGroup = DocumentFactory::instance()->undoGroup();
129
undoGroup->addStack(doc->undoStack());
130
undoGroup->setActiveStack(doc->undoStack());
132
if (d->mOnlyCurrentUrl) {
133
d->mSelectedFileItemListNeedsUpdate = true;
136
d->queueSignal("selectionChanged");
139
KFileItemList ContextManager::selectedFileItemList() const
141
d->updateSelectedFileItemList();
142
return d->mSelectedFileItemList;
145
void ContextManager::setCurrentDirUrl(const KUrl& url)
147
if (url.equals(d->mCurrentDirUrl, KUrl::CompareWithoutTrailingSlash)) {
150
d->mCurrentDirUrl = url;
151
currentDirUrlChanged();
154
KUrl ContextManager::currentDirUrl() const
156
return d->mCurrentDirUrl;
159
KUrl ContextManager::currentUrl() const
161
return d->mCurrentUrl;
164
SortedDirModel* ContextManager::dirModel() const
169
void ContextManager::slotDirModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
171
// Data change can happen in the following cases:
172
// - items have been renamed
173
// - item bytes have been modified
174
// - item meta info has been retrieved or modified
176
// If a selected item is affected, schedule emission of a
177
// selectionDataChanged() signal. Don't emit it directly to avoid spamming
178
// the context items in case of a mass change.
179
QModelIndexList selectionList = d->mSelectionModel->selectedIndexes();
180
if (selectionList.isEmpty()) {
184
QModelIndexList changedList;
185
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
186
changedList << d->mDirModel->index(row, 0);
189
QModelIndexList& shortList = selectionList;
190
QModelIndexList& longList = changedList;
191
if (shortList.length() > longList.length()) {
192
qSwap(shortList, longList);
194
Q_FOREACH(const QModelIndex & index, shortList) {
195
if (longList.contains(index)) {
196
d->mSelectedFileItemListNeedsUpdate = true;
197
d->queueSignal("selectionDataChanged");
203
void ContextManager::slotSelectionChanged()
205
d->mSelectedFileItemListNeedsUpdate = true;
206
d->queueSignal("selectionChanged");
209
void Gwenview::ContextManager::slotCurrentChanged(const QModelIndex& index)
211
KUrl url = d->mDirModel->urlForIndex(index);
215
void ContextManager::emitQueuedSignals()
217
Q_FOREACH(const QByteArray & signal, d->mQueuedSignals) {
218
QMetaObject::invokeMethod(this, signal.data());
220
d->mQueuedSignals.clear();
223
void ContextManager::setOnlyCurrentUrl(bool onlyCurrentUrl)
225
if (d->mOnlyCurrentUrl != onlyCurrentUrl) {
226
d->mOnlyCurrentUrl = onlyCurrentUrl;
227
d->mSelectedFileItemListNeedsUpdate = true;
228
d->queueSignal("selectionChanged");