]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
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 General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "dolphiniconsview.h"
24 #include <QDragMoveEvent>
26 #include <Q3ValueList>
28 #include <QMouseEvent>
29 #include <QDragEnterEvent>
30 #include <kiconeffect.h>
31 #include <kapplication.h>
33 #include <kglobalsettings.h>
35 #include <qclipboard.h>
38 #include <kstdaction.h>
39 #include <kfileitem.h>
41 #include "dolphinview.h"
42 #include "viewproperties.h"
44 #include "dolphinstatusbar.h"
45 #include "dolphinsettings.h"
46 #include "iconsmodesettings.h"
48 DolphinIconsView::DolphinIconsView(DolphinView
* parent
, LayoutMode layoutMode
) :
49 KFileIconView(parent
, 0),
50 m_previewIconSize(-1),
51 m_layoutMode(layoutMode
),
55 setMode(KIconView::Execute
);
56 setSelectionMode(KFile::Extended
);
57 Dolphin
& dolphin
= Dolphin::mainWin();
59 connect(this, SIGNAL(onItem(Q3IconViewItem
*)),
60 this, SLOT(slotOnItem(Q3IconViewItem
*)));
61 connect(this, SIGNAL(onViewport()),
62 this, SLOT(slotOnViewport()));
63 connect(this, SIGNAL(contextMenuRequested(Q3IconViewItem
*, const QPoint
&)),
64 this, SLOT(slotContextMenuRequested(Q3IconViewItem
*, const QPoint
&)));
65 connect(this, SIGNAL(selectionChanged()),
66 &dolphin
, SLOT(slotSelectionChanged()));
67 connect(&dolphin
, SIGNAL(activeViewChanged()),
68 this, SLOT(slotActivationUpdate()));
69 connect(this, SIGNAL(itemRenamed(Q3IconViewItem
*, const QString
&)),
70 this, SLOT(slotItemRenamed(Q3IconViewItem
*, const QString
&)));
71 connect(this, SIGNAL(dropped(QDropEvent
*, const KURL::List
&, const KURL
&)),
72 parent
, SLOT(slotURLListDropped(QDropEvent
*, const KURL::List
&, const KURL
&)));
74 QClipboard
* clipboard
= QApplication::clipboard();
75 connect(clipboard
, SIGNAL(dataChanged()),
76 this, SLOT(slotUpdateDisabledItems()));
78 // KFileIconView creates two actions for zooming, which are directly connected to the
79 // slots KFileIconView::zoomIn() and KFileIconView::zoomOut(). As this behavior is not
80 // wanted and the slots are not virtual, the actions are disabled here.
81 KAction
* zoomInAction
= actionCollection()->action("zoomIn");
82 assert(zoomInAction
!= 0);
83 zoomInAction
->setEnabled(false);
85 KAction
* zoomOutAction
= actionCollection()->action("zoomOut");
86 assert(zoomOutAction
!= 0);
87 zoomOutAction
->setEnabled(false);
89 setItemsMovable(true);
90 setWordWrapIconText(true);
91 if (m_layoutMode
== Previews
) {
97 DolphinIconsView::~DolphinIconsView()
101 void DolphinIconsView::setLayoutMode(LayoutMode mode
)
103 if (m_layoutMode
!= mode
) {
109 void DolphinIconsView::beginItemUpdates()
113 void DolphinIconsView::endItemUpdates()
115 arrangeItemsInGrid();
117 // TODO: KFileIconView does not emit any signal when the preview
118 // has been finished. Using a delay of 300 ms is a temporary workaround
119 // until the DolphinIconsView will implement the previews by it's own in
121 QTimer::singleShot(300, this, SLOT(slotUpdateDisabledItems()));
123 const KFileIconViewItem
* item
= static_cast<const KFileIconViewItem
*>(firstItem());
125 setCurrentItem(item
->fileInfo());
129 const Q3ValueList
<URLNavigator::HistoryElem
> history
= m_dolphinView
->urlHistory(index
);
130 if (!history
.isEmpty()) {
131 KFileView
* fileView
= static_cast<KFileView
*>(this);
132 fileView
->setCurrentItem(history
[index
].currentFileName());
133 setContentsPos(history
[index
].contentsX(), history
[index
].contentsY());
137 void DolphinIconsView::refreshSettings()
139 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
140 assert(settings
!= 0);
142 setIconSize(settings
->iconSize());
144 const Q3IconView::Arrangement arrangement
= settings
->arrangement() == "LeftToRight" ? // TODO: use enum directly in settings
145 Q3IconView::LeftToRight
: Q3IconView::TopToBottom
;
146 const Q3IconView::ItemTextPos textPos
= (arrangement
== Q3IconView::LeftToRight
) ?
149 setArrangement(arrangement
);
150 setItemTextPos(textPos
);
152 // TODO: tempory crash; will get changed anyway for KDE 4
153 /*setGridX(settings->gridWidth());
154 setGridY(settings->gridHeight());
155 setSpacing(settings->gridSpacing());*/
157 QFont
adjustedFont(font());
158 adjustedFont
.setFamily(settings
->fontFamily());
159 adjustedFont
.setPointSize(settings
->fontSize());
160 setFont(adjustedFont
);
161 setIconTextHeight(settings
->numberOfTexlines());
163 if (m_layoutMode
== Previews
) {
164 // There is no getter method for the current size in KFileIconView. To
165 // prevent a flickering the current size is stored in m_previewIconSize and
166 // setPreviewSize is only invoked if the size really has changed.
169 const int size
= settings
->previewSize();
170 if (size
!= m_previewIconSize
) {
171 m_previewIconSize
= size
;
172 setPreviewSize(size
);
177 void DolphinIconsView::zoomIn()
179 if (isZoomInPossible()) {
180 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
181 const int textWidthHint
= DolphinSettings::instance().textWidthHint(); // TODO: remove for KDE4
183 const int iconSize
= increasedIconSize(settings
->iconSize());
184 settings
->setIconSize(iconSize
);
186 if (m_layoutMode
== Previews
) {
187 const int previewSize
= increasedIconSize(settings
->previewSize());
188 settings
->setPreviewSize(previewSize
);
191 DolphinSettings::instance().calculateGridSize(textWidthHint
); // TODO: remove for KDE4
192 ItemEffectsManager::zoomIn();
196 void DolphinIconsView::zoomOut()
198 if (isZoomOutPossible()) {
199 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
200 const int textWidthHint
= DolphinSettings::instance().textWidthHint(); // TODO: remove for KDE4
202 const int iconSize
= decreasedIconSize(settings
->iconSize());
203 settings
->setIconSize(iconSize
);
205 if (m_layoutMode
== Previews
) {
206 const int previewSize
= decreasedIconSize(settings
->previewSize());
207 settings
->setPreviewSize(previewSize
);
210 DolphinSettings::instance().calculateGridSize(textWidthHint
); // TODO: remove for KDE4
211 ItemEffectsManager::zoomOut();
215 bool DolphinIconsView::isZoomInPossible() const
217 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
218 const int size
= (m_layoutMode
== Icons
) ? settings
->iconSize() : settings
->previewSize();
219 return size
< KIcon::SizeEnormous
;
222 bool DolphinIconsView::isZoomOutPossible() const
224 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
225 return settings
->iconSize() > KIcon::SizeSmall
;
228 void DolphinIconsView::arrangeItemsInGrid( bool updated
)
231 KFileIconView::arrangeItemsInGrid(updated
);
233 if (m_layoutMode
== Previews
) {
234 // The class KFileIconView has a bug when the size of the previews differs from the size
235 // of the icons: For specific MIME types the y-position and the height is calculated in
236 // a wrong manner. The following code bypasses this issue. No bugreport has been submitted
237 // as this functionality is not used by any KDE3 application and the core developers are
238 // busy enough for KDE4 now :-)
240 KFileIconViewItem
* item
= static_cast<KFileIconViewItem
*>(Q3IconView::firstItem());
243 mimetype
= item
->fileInfo()->mimetype();
244 const bool fixSize
= mimetype
.contains("text") ||
245 mimetype
.contains("application/x-");
247 item
->setPixmapSize(QSize(m_previewIconSize
, m_previewIconSize
));
249 item
= static_cast<KFileIconViewItem
*>(item
->nextItem());
254 void DolphinIconsView::setContextPixmap(void* context
,
255 const QPixmap
& pixmap
)
257 reinterpret_cast<KFileIconViewItem
*>(context
)->setPixmap(pixmap
);
260 const QPixmap
* DolphinIconsView::contextPixmap(void* context
)
262 return reinterpret_cast<KFileIconViewItem
*>(context
)->pixmap();
265 void* DolphinIconsView::firstContext()
267 return reinterpret_cast<void*>(firstItem());
270 void* DolphinIconsView::nextContext(void* context
)
272 KFileIconViewItem
* iconViewItem
= reinterpret_cast<KFileIconViewItem
*>(context
);
273 return reinterpret_cast<void*>(iconViewItem
->nextItem());
276 KFileItem
* DolphinIconsView::contextFileInfo(void* context
)
278 return reinterpret_cast<KFileIconViewItem
*>(context
)->fileInfo();
281 void DolphinIconsView::contentsMousePressEvent(QMouseEvent
* event
)
283 KFileIconView::contentsMousePressEvent(event
);
284 resetActivatedItem();
285 emit
signalRequestActivation();
286 m_dolphinView
->statusBar()->clear();
289 void DolphinIconsView::contentsMouseReleaseEvent(QMouseEvent
* event
)
291 KFileIconView::contentsMouseReleaseEvent(event
);
293 // The KFileIconView does not send any selectionChanged signal if
294 // a selection is done by using the "select-during-button-pressed" feature.
295 // Hence inform Dolphin about the selection change manually:
296 Dolphin::mainWin().slotSelectionChanged();
299 void DolphinIconsView::drawBackground(QPainter
* painter
, const QRect
& rect
)
301 if (m_dolphinView
->isActive()) {
302 KFileIconView::drawBackground(painter
, rect
);
305 const QBrush
brush(colorGroup().background());
306 painter
->fillRect(0, 0, width(), height(), brush
);
310 Q3DragObject
* DolphinIconsView::dragObject()
313 KFileItemListIterator
it(*KFileView::selectedItems());
314 while (it
.current() != 0) {
315 urls
.append((*it
)->url());
320 if(urls
.count() > 1) {
321 pixmap
= DesktopIcon("kmultiple", iconSize());
324 KFileIconViewItem
* item
= static_cast<KFileIconViewItem
*>(currentItem());
325 if ((item
!= 0) && (item
->pixmap() != 0)) {
326 pixmap
= *(item
->pixmap());
330 if (pixmap
.isNull()) {
331 pixmap
= currentFileItem()->pixmap(iconSize());
334 Q3DragObject
* dragObj
= new KURLDrag(urls
, widget());
335 dragObj
->setPixmap(pixmap
);
339 void DolphinIconsView::contentsDragEnterEvent(QDragEnterEvent
* event
)
341 // TODO: The method KFileIconView::contentsDragEnterEvent() does
342 // not allow drag and drop inside itself, which prevents the possability
343 // to move a file into a directory. As the method KFileIconView::acceptDrag()
344 // is not virtual, we must overwrite the method
345 // KFileIconView::contentsDragEnterEvent() and do some cut/copy/paste for this
346 // usecase. Corresponding to the documentation the method KFileIconView::acceptDrag()
347 // will get virtual in KDE 4, which will simplify the code.
349 if (event
->source() != this) {
350 KFileIconView::contentsDragEnterEvent(event
);
354 const bool accept
= KURLDrag::canDecode(event
) &&
355 (event
->action() == QDropEvent::Copy
||
356 event
->action() == QDropEvent::Move
||
357 event
->action() == QDropEvent::Link
);
359 event
->acceptAction();
366 void DolphinIconsView::contentsDragMoveEvent(QDragMoveEvent
* event
)
368 KFileIconView::contentsDragMoveEvent(event
);
370 // If a dragging is done above a directory, show the icon as 'active' for
372 KFileIconViewItem
* item
= static_cast<KFileIconViewItem
*>(findItem(contentsToViewport(event
->pos())));
374 bool showActive
= false;
376 const KFileItem
* fileInfo
= item
->fileInfo();
377 showActive
= (fileInfo
!= 0) && fileInfo
->isDir();
388 void DolphinIconsView::contentsDropEvent(QDropEvent
* event
)
390 // TODO: Most of the following code is a copy of
391 // KFileIconView::contentsDropEvent. See comment in
392 // DolphinIconsView::contentsDragEnterEvent for details.
394 if (event
->source() != this) {
395 KFileIconView::contentsDropEvent(event
);
399 KFileIconViewItem
* item
= static_cast<KFileIconViewItem
*>(findItem(contentsToViewport(event
->pos())));
400 const bool accept
= KURLDrag::canDecode(event
) &&
401 (event
->action() == QDropEvent::Copy
||
402 event
->action() == QDropEvent::Move
||
403 event
->action() == QDropEvent::Link
) &&
409 KFileItem
* fileItem
= item
->fileInfo();
410 if (!fileItem
->isDir()) {
411 // the file is not a directory, hence don't accept any drop
414 emit
dropped(event
, fileItem
);
416 if (KURLDrag::decode(event
, urls
) && !urls
.isEmpty()) {
417 emit
dropped(event
, urls
, fileItem
!= 0 ? fileItem
->url() : KURL());
418 sig
->dropURLs(fileItem
, event
, urls
);
422 void DolphinIconsView::slotOnItem(Q3IconViewItem
* item
)
425 activateItem(reinterpret_cast<void*>(item
));
427 KFileItem
* fileItem
= static_cast<KFileIconViewItem
*>(item
)->fileInfo();
428 m_dolphinView
->requestItemInfo(fileItem
->url());
431 void DolphinIconsView::slotOnViewport()
433 resetActivatedItem();
434 m_dolphinView
->requestItemInfo(KURL());
437 void DolphinIconsView::slotContextMenuRequested(Q3IconViewItem
* item
,
440 KFileItem
* fileInfo
= 0;
442 fileInfo
= static_cast<KFileIconViewItem
*>(item
)->fileInfo();
444 m_dolphinView
->openContextMenu(fileInfo
, pos
);
447 void DolphinIconsView::slotItemRenamed(Q3IconViewItem
* item
,
450 KFileItem
* fileInfo
= static_cast<KFileIconViewItem
*>(item
)->fileInfo();
451 m_dolphinView
->rename(KURL(fileInfo
->url()), name
);
454 void DolphinIconsView::slotActivationUpdate()
458 // TODO: there must be a simpler way to say
459 // "update all children"
460 const QObjectList
* list
= children();
465 QObjectListIterator
it(*list
);
467 while ((object
= it
.current()) != 0) {
468 if (object
->inherits("QWidget")) {
469 QWidget
* widget
= static_cast<QWidget
*>(object
);
476 void DolphinIconsView::slotUpdateDisabledItems()
478 updateDisabledItems();
481 int DolphinIconsView::increasedIconSize(int size
) const
485 case KIcon::SizeSmall
: incSize
= KIcon::SizeSmallMedium
; break;
486 case KIcon::SizeSmallMedium
: incSize
= KIcon::SizeMedium
; break;
487 case KIcon::SizeMedium
: incSize
= KIcon::SizeLarge
; break;
488 case KIcon::SizeLarge
: incSize
= KIcon::SizeHuge
; break;
489 case KIcon::SizeHuge
: incSize
= KIcon::SizeEnormous
; break;
490 default: assert(false); break;
495 int DolphinIconsView::decreasedIconSize(int size
) const
499 case KIcon::SizeSmallMedium
: decSize
= KIcon::SizeSmall
; break;
500 case KIcon::SizeMedium
: decSize
= KIcon::SizeSmallMedium
; break;
501 case KIcon::SizeLarge
: decSize
= KIcon::SizeMedium
; break;
502 case KIcon::SizeHuge
: decSize
= KIcon::SizeLarge
; break;
503 case KIcon::SizeEnormous
: decSize
= KIcon::SizeHuge
; break;
504 default: assert(false); break;
509 #include "dolphiniconsview.moc"