|
| 1 | +#include <QtGui> |
| 2 | + |
| 3 | +#include "listwidget.h" |
| 4 | + |
| 5 | +cListWidget::cListWidget(QWidget *parent) : QTableWidget(parent) |
| 6 | +{ |
| 7 | +#ifdef Q_OS_WIN32 |
| 8 | + rxPath = new QRegExp(tr("^/(.*/)([^/]+)$")); |
| 9 | +#else |
| 10 | + rxPath = new QRegExp(tr("^(.*/)([^/]+)$")); |
| 11 | +#endif |
| 12 | + |
| 13 | + QStringList labels; |
| 14 | + labels << tr("Filename") << tr("Path"); |
| 15 | + |
| 16 | + setAcceptDrops(true); |
| 17 | + setAlternatingRowColors(true); |
| 18 | + setColumnCount(2); |
| 19 | + setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 20 | + setHorizontalHeaderLabels(labels); |
| 21 | + setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); |
| 22 | + setSelectionBehavior(QAbstractItemView::SelectRows); |
| 23 | + setShowGrid(false); |
| 24 | + setTabKeyNavigation(false); |
| 25 | + setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); |
| 26 | + |
| 27 | + horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); |
| 28 | + horizontalHeader()->setHighlightSections(false); |
| 29 | + horizontalHeader()->setStretchLastSection(true); |
| 30 | + resizeColumnsToContents(); |
| 31 | + verticalHeader()->hide(); |
| 32 | +} |
| 33 | + |
| 34 | +void cListWidget::removeAll() |
| 35 | +{ |
| 36 | + setRowCount(0); |
| 37 | + resizeColumnsToContents(); |
| 38 | + resizeRowsToContents(); |
| 39 | + horizontalHeader()->setStretchLastSection(true); |
| 40 | +} |
| 41 | + |
| 42 | +void cListWidget::removeSelected() |
| 43 | +{ |
| 44 | + int i, size = rowCount(); |
| 45 | + for (i = 0; i < size; ++i) { |
| 46 | + if (item(i, 0)->isSelected()) { |
| 47 | + removeRow(i); |
| 48 | + --i; |
| 49 | + --size; |
| 50 | + } // if |
| 51 | + } // for |
| 52 | +} |
| 53 | + |
| 54 | +void cListWidget::dragEnterEvent(QDragEnterEvent *event) |
| 55 | +{ |
| 56 | + event->acceptProposedAction(); |
| 57 | +} |
| 58 | + |
| 59 | +void cListWidget::dragMoveEvent(QDragMoveEvent *event) |
| 60 | +{ |
| 61 | + event->acceptProposedAction(); |
| 62 | +} |
| 63 | + |
| 64 | +void cListWidget::dragLeaveEvent(QDragLeaveEvent *event) |
| 65 | +{ |
| 66 | + event->accept(); |
| 67 | +} |
| 68 | + |
| 69 | +void cListWidget::dropEvent(QDropEvent *event) |
| 70 | +{ |
| 71 | + const QMimeData *mimeData = event->mimeData(); |
| 72 | + |
| 73 | + if (mimeData->hasUrls()) { |
| 74 | + QList<QUrl> urlList = mimeData->urls(); |
| 75 | + int row; |
| 76 | + for (int i = 0; i < urlList.size(); ++i) { |
| 77 | + if (rxPath->indexIn(urlList.at(i).path()) < 0) |
| 78 | + continue; |
| 79 | + row = rowCount(); |
| 80 | + insertRow(row); |
| 81 | + setItem(row, 0, new QTableWidgetItem(rxPath->cap(2))); |
| 82 | + setItem(row, 1, new QTableWidgetItem(rxPath->cap(1))); |
| 83 | + } // for |
| 84 | + } // if |
| 85 | + resizeColumnsToContents(); |
| 86 | + resizeRowsToContents(); |
| 87 | + horizontalHeader()->setStretchLastSection(true); |
| 88 | + |
| 89 | + event->acceptProposedAction(); |
| 90 | +} |
0 commit comments