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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
/*
Gwenview - A simple image viewer for TDE
Copyright 2000-2004 Aur�ien G�eau
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "treeview.moc"
// TQt
#include <tqheader.h>
#include <tqtimer.h>
// KDE
#include <kdebug.h>
#include <kiconloader.h>
#include <kmimetype.h>
#include <kurldrag.h>
// Local
#include "../gvcore/fileoperation.h"
namespace Gwenview {
#undef ENABLE_LOG
#undef LOG
//#define ENABLE_LOG
#ifdef ENABLE_LOG
#define LOG(x) kdDebug() << k_funcinfo << x << endl
#else
#define LOG(x) ;
#endif
const int AUTO_OPEN_DELAY=1000;
const int DND_ICON_COUNT=8;
const char* DND_PREFIX="dnd";
struct TreeView::Private {
TreeView* mView;
KFileTreeBranch* mBranch;
KFileTreeViewItem* mDropTarget;
TQTimer* mAutoOpenTimer;
KFileTreeViewItem* findViewItem(KFileTreeViewItem* parent,const TQString& text) {
TQListViewItem* item;
for (item=parent->firstChild();item;item=item->nextSibling()) {
if (item->text(0)==text) {
return static_cast<KFileTreeViewItem*>(item);
}
}
return 0L;
}
void setURLInternal(const KURL& url) {
LOG(url.prettyURL() );
TQString path=url.path();
if (!mBranch || !mBranch->rootUrl().isParentOf(url)) {
mView->createBranch(url);
return;
}
// The request URL is a child of the branch, expand the branch to reach
// the child
LOG("Expanding to reach child");
if(mBranch->rootUrl().path()!="/") {
path.remove(0,mBranch->rootUrl().path().length());
}
LOG("Path=" << path);
// Finds the deepest existing view item
TQStringList folderParts=TQStringList::split('/',path);
TQStringList::Iterator folderIter=folderParts.begin();
TQStringList::Iterator endFolderIter=folderParts.end();
KFileTreeViewItem* viewItem=mBranch->root();
for(;folderIter!=endFolderIter;++folderIter) {
KFileTreeViewItem* nextViewItem=findViewItem(viewItem,*folderIter);
if (!nextViewItem) break;
viewItem=nextViewItem;
}
LOG("Deepest existing view item=" << viewItem->url());
// If this is the wanted item, select it,
// otherwise set the url as the next to select
if (viewItem->url().equals(url,true)) {
LOG("We are done");
mView->setCurrentItem(viewItem);
mView->ensureItemVisible(viewItem);
mView->slotSetNextUrlToSelect(KURL());
} else {
LOG("We continue");
mView->slotSetNextUrlToSelect(url);
}
LOG("Opening deepest existing view item");
viewItem->setOpen(true);
}
};
TreeView::TreeView(TQWidget* parent)
: KFileTreeView(parent) {
d=new Private;
d->mView=this;
d->mBranch=0;
d->mDropTarget=0;
d->mAutoOpenTimer=new TQTimer(this);
// Look
addColumn(TQString());
header()->hide();
setAllColumnsShowFocus(true);
setRootIsDecorated(false);
setFullWidth(true);
// Drag'n'drop
setDragEnabled(true);
setDropVisualizer(false);
setDropHighlighter(true);
setAcceptDrops(true);
connect(d->mAutoOpenTimer, TQT_SIGNAL(timeout()),
TQT_TQOBJECT(this), TQT_SLOT(autoOpenDropTarget()));
}
TreeView::~TreeView() {
delete d;
}
void TreeView::setURL(const KURL& url) {
LOG(url.prettyURL());
if (currentURL().equals(url,true)) return;
if (m_nextUrlToSelect.equals(url,true)) return;
slotSetNextUrlToSelect(url);
// Do not update the view if it's hidden, the url has been stored with
// slotSetNextUrlToSelect. The view will expand to it next time it's shown.
if (!isVisible()) {
LOG("We are hidden, just store the url");
return;
}
d->setURLInternal(url);
}
void TreeView::slotTreeViewPopulateFinished(KFileTreeViewItem* item) {
TQListViewItem* child;
if (!item) return;
KURL url=item->url();
if (d->mDropTarget) {
startAnimation(d->mDropTarget,DND_PREFIX,DND_ICON_COUNT);
}
LOG("itemURL=" << url);
LOG("m_nextUrlToSelect=" << m_nextUrlToSelect);
// We reached the URL to select, get out
if (url.equals(m_nextUrlToSelect, true)) {
slotSetNextUrlToSelect(KURL());
return;
}
// This URL is not a parent of a wanted URL, get out
if (!url.isParentOf(m_nextUrlToSelect)) return;
// Find the next child item and open it
LOG("Looking for next child");
for (child=item->firstChild(); child; child=child->nextSibling()) {
url=static_cast<KFileTreeViewItem*>(child)->url();
if (url.isParentOf(m_nextUrlToSelect)) {
LOG("Opening child with URL=" << url);
ensureItemVisible(child);
child->setOpen(true);
return;
}
}
}
void TreeView::createBranch(const KURL& url) {
if (d->mBranch) {
removeBranch(d->mBranch);
}
TQString title=url.prettyURL(0, KURL::StripFileProtocol);
d->mBranch=addBranch(url, title, SmallIcon(KMimeType::iconForURL(url)) );
setDirOnlyMode(d->mBranch, true);
d->mBranch->setChildRecurse(false);
d->mBranch->root()->setOpen(true);
connect(d->mBranch, TQT_SIGNAL(populateFinished(KFileTreeViewItem*) ),
TQT_TQOBJECT(this), TQT_SLOT(slotTreeViewPopulateFinished(KFileTreeViewItem*)) );
}
/**
* Override this method to make sure that the item is selected, opened and
* visible
*/
void TreeView::slotNewTreeViewItems(KFileTreeBranch* branch, const KFileTreeViewItemList& itemList) {
if( ! branch ) return;
LOG("");
if(m_nextUrlToSelect.isEmpty()) return;
KFileTreeViewItemListIterator it( itemList );
for(;it.current(); ++it ) {
KURL url = (*it)->url();
// This is an URL to select
// (We block signals to avoid simulating a click on the dir item)
if (m_nextUrlToSelect.equals(url,true)) {
blockSignals(true);
setCurrentItem(*it);
blockSignals(false);
ensureItemVisible(*it);
(*it)->setOpen(true);
m_nextUrlToSelect = KURL();
return;
}
}
}
/**
* Override showEvent to make sure the view shows the correct
* dir when it's shown. Since the view doesn't update if it's
* hidden
*/
void TreeView::showEvent(TQShowEvent* event) {
LOG("m_nextUrlToSelect=" << m_nextUrlToSelect.pathOrURL());
if (m_nextUrlToSelect.isValid() && !currentURL().equals(m_nextUrlToSelect,true)) {
d->setURLInternal(m_nextUrlToSelect);
}
TQWidget::showEvent(event);
}
void TreeView::contentsDragMoveEvent(TQDragMoveEvent* event) {
if (!KURLDrag::canDecode(event)) {
event->ignore();
return;
}
// Get a pointer to the new drop item
TQPoint point(0,event->pos().y());
KFileTreeViewItem* newDropTarget=static_cast<KFileTreeViewItem*>( itemAt(contentsToViewport(point)) );
if (!newDropTarget) {
event->ignore();
d->mAutoOpenTimer->stop();
if (d->mDropTarget) {
stopAnimation(d->mDropTarget);
d->mDropTarget=0L;
}
return;
}
event->accept();
if (newDropTarget==d->mDropTarget) return;
if (d->mDropTarget) {
stopAnimation(d->mDropTarget);
}
// Restart auto open timer if we are over a new item
d->mAutoOpenTimer->stop();
d->mDropTarget=newDropTarget;
startAnimation(newDropTarget,DND_PREFIX,DND_ICON_COUNT);
d->mAutoOpenTimer->start(AUTO_OPEN_DELAY,true);
}
void TreeView::contentsDragLeaveEvent(TQDragLeaveEvent*) {
d->mAutoOpenTimer->stop();
if (d->mDropTarget) {
stopAnimation(d->mDropTarget);
d->mDropTarget=0L;
}
}
void TreeView::contentsDropEvent(TQDropEvent* event) {
d->mAutoOpenTimer->stop();
// Get data from drop (do it before showing menu to avoid mDropTarget changes)
if (!d->mDropTarget) return;
KURL dest=d->mDropTarget->url();
KURL::List urls;
if (!KURLDrag::decode(event,urls)) return;
// Show popup
bool wasMoved;
FileOperation::openDropURLMenu(this, urls, dest, &wasMoved);
if (wasMoved) {
// If the current url was in the list, set the drop target as the new
// current item
KURL current=currentURL();
KURL::List::ConstIterator it=urls.begin();
for (; it!=urls.end(); ++it) {
if (current.equals(*it,true)) {
setCurrentItem(d->mDropTarget);
break;
}
}
}
// Reset drop target
if (d->mDropTarget) {
stopAnimation(d->mDropTarget);
d->mDropTarget=0L;
}
}
void TreeView::autoOpenDropTarget() {
if (d->mDropTarget) {
d->mDropTarget->setOpen(true);
}
}
} // namespace
|