Plasma
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 "customdragtreeview_p.h"
00021 #include "kcategorizeditemsview_p.h"
00022
00023 #define PIX_SIZE 64
00024 #define MAX_OFFSET 16
00025 #define MAX_COUNT 5
00026
00027 CustomDragTreeView::CustomDragTreeView(QWidget * parent)
00028 : QTreeView(parent) {}
00029
00030 void CustomDragTreeView::startDrag(Qt::DropActions supportedActions)
00031 {
00032 Q_UNUSED(supportedActions);
00033
00034
00035
00036
00037 if (!m_view) {
00038 return;
00039 }
00040
00041 QModelIndexList indexes = selectedIndexes();
00042 if (indexes.count() > 0) {
00043 QMimeData *data = model()->mimeData(indexes);
00044 if (!data) {
00045 return;
00046 }
00047
00048 int size = PIX_SIZE + (qMin(MAX_COUNT, indexes.count()) * MAX_OFFSET);
00049 int off = MAX_OFFSET;
00050 if (indexes.count() > MAX_COUNT) {
00051 off = (MAX_OFFSET * MAX_COUNT) / indexes.count();
00052 }
00053
00054
00055
00056 QPixmap pixmap(size, size);
00057 pixmap.fill(QColor(255, 255, 255, 0));
00058 QPainter painter(&pixmap);
00059 QRect rect(0, 0, PIX_SIZE, PIX_SIZE);
00060
00061 foreach (const QModelIndex &index, indexes) {
00062 if (index.column() != 0) {
00063 continue;
00064 }
00065
00066 KCategorizedItemsViewModels::AbstractItem * item =
00067 m_view->getItemByProxyIndex(index);
00068
00069 if (item) {
00070 rect.setSize(item->icon().actualSize(QSize(PIX_SIZE, PIX_SIZE)));
00071
00072 item->icon().paint(&painter, rect);
00073 rect.moveTopLeft(rect.topLeft() + QPoint(off, off));
00074 }
00075 }
00076 painter.end();
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