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
00021 #include "CustomWidgets.h"
00022 #include <QMimeData>
00023 #include <QResizeEvent>
00024 #include <QPainter>
00025
00026 namespace Aseba
00027 {
00030
00031
00032 QStringList DraggableListWidget::mimeTypes () const
00033 {
00034 QStringList types;
00035 types << "text/plain";
00036 return types;
00037 }
00038
00039 QMimeData * DraggableListWidget::mimeData ( const QList<QListWidgetItem *> items ) const
00040 {
00041 QString texts;
00042 foreach (QListWidgetItem *item, items)
00043 {
00044 texts += item->text();
00045 }
00046
00047 QMimeData *mimeData = new QMimeData();
00048 mimeData->setText(texts);
00049 return mimeData;
00050 }
00051
00053
00054 FixedWidthTableView::FixedWidthTableView()
00055 {
00056 col1Width = 50;
00057 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00058 }
00059
00060 void FixedWidthTableView::setSecondColumnLongestContent(const QString& content)
00061 {
00062 Q_ASSERT((model ()->columnCount() == 2) || (model ()->columnCount() == 3));
00063 QFontMetrics fm(font());
00064 col1Width = fm.width(content);
00065 }
00066
00067 void FixedWidthTableView::resizeEvent ( QResizeEvent * event )
00068 {
00069 Q_ASSERT((model ()->columnCount() == 2) || (model ()->columnCount() == 3));
00070 if (model()->columnCount() == 2)
00071 {
00072 int col0Width = event->size().width() - col1Width;
00073 setColumnWidth(0, col0Width);
00074 setColumnWidth(1, col1Width);
00075 }
00076 else if (model()->columnCount() == 3)
00077 {
00078 int col0Width = event->size().width() - col1Width - 22;
00079 setColumnWidth(0, col0Width);
00080 setColumnWidth(1, col1Width);
00081 setColumnWidth(2, 22);
00082 }
00083 }
00084
00085 void FixedWidthTableView::startDrag(Qt::DropActions supportedActions)
00086 {
00087 QModelIndex item = currentIndex();
00088 QModelIndexList list;
00089 list.append(item);
00090
00091 QMimeData* mimeData = model()->mimeData(list);
00092
00093 QDrag *drag = new QDrag(this);
00094 drag->setMimeData(mimeData);
00095 QPixmap pixmap = getDragPixmap(mimeData->text());
00096 drag->setPixmap(pixmap);
00097 QPoint hotpoint(pixmap.width()+10, pixmap.height()/2);
00098 drag->setHotSpot(hotpoint);
00099
00100 if (drag->exec(Qt::CopyAction|Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
00101 {
00102
00103 }
00104 else
00105 {
00106
00107 }
00108 }
00109
00110 void FixedWidthTableView::dragEnterEvent(QDragEnterEvent *event)
00111 {
00112 if (modelMatchMimeFormat(event->mimeData()->formats()))
00113 {
00114 event->setDropAction(Qt::MoveAction);
00115 event->accept();
00116 }
00117 else
00118 event->ignore();
00119 }
00120
00121 void FixedWidthTableView::dragMoveEvent(QDragMoveEvent *event)
00122 {
00123 event->accept();
00124 }
00125
00126 void FixedWidthTableView::dropEvent(QDropEvent *event)
00127 {
00128 int row = rowAt(event->pos().y());
00129 int col = columnAt(event->pos().x());
00130
00131 if (model()->dropMimeData(event->mimeData(), event->dropAction(), row, col, QModelIndex()))
00132 {
00133 event->accept();
00134 }
00135 else
00136 event->ignore();
00137 }
00138
00139 QPixmap FixedWidthTableView::getDragPixmap(QString text)
00140 {
00141
00142 QFontMetrics metric(font());
00143 QSize size = metric.size(Qt::TextSingleLine, text);
00144
00145 QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied);
00146 image.fill(qRgba(0, 0, 0, 0));
00147
00148 QFont font;
00149 font.setStyleStrategy(QFont::ForceOutline);
00150
00151 QPainter painter;
00152 painter.begin(&image);
00153 painter.setRenderHint(QPainter::Antialiasing);
00154
00155 painter.setFont(font);
00156 painter.setBrush(Qt::black);
00157 painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text);
00158 painter.end();
00159
00160 return QPixmap::fromImage(image);
00161 }
00162
00163 bool FixedWidthTableView::modelMatchMimeFormat(QStringList candidates)
00164 {
00165 QStringList acceptedMime = model()->mimeTypes();
00166 for (QStringList::ConstIterator it = acceptedMime.constBegin(); it != acceptedMime.constEnd(); it++)
00167 {
00168 if (candidates.contains(*it))
00169 return true;
00170 }
00171 return false;
00172 }
00173
00175 };