CustomWidgets.cpp
Go to the documentation of this file.
00001 /*
00002         Aseba - an event-based framework for distributed robot control
00003         Copyright (C) 2007--2012:
00004                 Stephane Magnenat <stephane at magnenat dot net>
00005                 (http://stephane.magnenat.net)
00006                 and other contributors, see authors.txt for details
00007         
00008         This program is free software: you can redistribute it and/or modify
00009         it under the terms of the GNU Lesser General Public License as published
00010         by the Free Software Foundation, version 3 of the License.
00011         
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU Lesser General Public License for more details.
00016         
00017         You should have received a copy of the GNU Lesser General Public License
00018         along with this program. If not, see <http://www.gnu.org/licenses/>.
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                         // item moved
00103                 }
00104                 else
00105                 {
00106                         // item copied
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                 // From the "Fridge Magnets" example
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 }; // Aseba


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:38