libplasma
customdragtreeview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kcategorizeditemsview_p.h"
00021
00022 #define PIX_SIZE 64
00023 #define MAX_OFFSET 16
00024 #define MAX_COUNT 5
00025
00026 CustomDragTreeView::CustomDragTreeView(QWidget * parent)
00027 : QTreeView(parent) {}
00028
00029 void CustomDragTreeView::startDrag ( Qt::DropActions supportedActions )
00030 {
00031 Q_UNUSED(supportedActions);
00032
00033
00034
00035
00036 if (!m_view) {
00037 return;
00038 }
00039
00040 QModelIndexList indexes = selectedIndexes();
00041 if (indexes.count() > 0) {
00042 QMimeData *data = model()->mimeData(indexes);
00043 if (!data) {
00044 return;
00045 }
00046
00047 int size = PIX_SIZE + (qMin(MAX_COUNT, indexes.count()) * MAX_OFFSET);
00048 int off = MAX_OFFSET;
00049 if (indexes.count() > MAX_COUNT) {
00050 off = (MAX_OFFSET * MAX_COUNT) / indexes.count();
00051 }
00052
00053
00054
00055 QPixmap pixmap(size, size);
00056 pixmap.fill(QColor(255, 255, 255, 0));
00057 QPainter painter(&pixmap);
00058 QRect rect(0, 0, PIX_SIZE, PIX_SIZE);
00059
00060 foreach (const QModelIndex &index, indexes) {
00061 if (index.column() != 0) {
00062 continue;
00063 }
00064
00065 KCategorizedItemsViewModels::AbstractItem * item =
00066 m_view->getItemByProxyIndex(index);
00067
00068 if (item) {
00069 rect.setSize(item->icon().actualSize(QSize(PIX_SIZE, PIX_SIZE)));
00070
00071 item->icon().paint(&painter, rect);
00072 rect.moveTopLeft(rect.topLeft() + QPoint(off, off));
00073 }
00074 }
00075 painter.end();
00076
00077
00078 QDrag *drag = new QDrag(this);
00079 drag->setPixmap(pixmap);
00080 drag->setMimeData(data);
00081 drag->start(supportedActions);
00082
00083
00084
00085 }
00086 }
00087