X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/794911106c265c8691689f601e4aed0862aff0b8..f9f4dbf005f63d9e1d14acbe21beab6e0fc26b36:/src/dolphindetailsview.cpp diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index df07fb9a5..ba82fc901 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -28,13 +28,20 @@ #include "dolphin_detailsmodesettings.h" #include -#include -#include +#include +#include +#include +#include +#include DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) : QTreeView(parent), - m_controller(controller) + m_controller(controller), + m_dragging(false), + m_showElasticBand(false), + m_elasticBandOrigin(), + m_elasticBandDestination() { Q_ASSERT(controller != 0); @@ -71,7 +78,7 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr connect(this, SIGNAL(activated(const QModelIndex&)), controller, SLOT(triggerItem(const QModelIndex&))); connect(this, SIGNAL(entered(const QModelIndex&)), - controller, SLOT(emitItemEntered(const QModelIndex&))); + this, SLOT(slotEntered(const QModelIndex&))); connect(this, SIGNAL(viewportEntered()), controller, SLOT(emitViewportEntered())); connect(controller, SIGNAL(zoomIn()), @@ -94,7 +101,8 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr } DolphinDetailsView::~DolphinDetailsView() -{} +{ +} bool DolphinDetailsView::event(QEvent* event) { @@ -145,10 +153,46 @@ void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event) m_controller->triggerContextMenuRequest(event->pos()); } +void DolphinDetailsView::mousePressEvent(QMouseEvent* event) +{ + m_controller->triggerActivation(); + + QTreeView::mousePressEvent(event); + + const QModelIndex index = indexAt(event->pos()); + if (!index.isValid() || (index.column() != KDirModel::Name)) { + const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers(); + if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) { + clearSelection(); + } + } + + if (event->button() == Qt::LeftButton) { + m_showElasticBand = true; + + const QPoint pos(contentsPos()); + m_elasticBandOrigin = event->pos(); + m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x()); + m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y()); + m_elasticBandDestination = event->pos(); + } +} + +void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event) +{ + QTreeView::mouseMoveEvent(event); + if (m_showElasticBand) { + updateElasticBand(); + } +} + void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event) { QTreeView::mouseReleaseEvent(event); - m_controller->triggerActivation(); + if (m_showElasticBand) { + updateElasticBand(); + m_showElasticBand = false; + } } void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event) @@ -156,6 +200,37 @@ void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event) if (event->mimeData()->hasUrls()) { event->acceptProposedAction(); } + + if (m_showElasticBand) { + updateElasticBand(); + m_showElasticBand = false; + } + m_dragging = true; +} + +void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event) +{ + QTreeView::dragLeaveEvent(event); + + // TODO: remove this code when the issue #160611 is solved in Qt 4.4 + m_dragging = false; + setDirtyRegion(m_dropRect); +} + +void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event) +{ + QTreeView::dragMoveEvent(event); + + // TODO: remove this code when the issue #160611 is solved in Qt 4.4 + setDirtyRegion(m_dropRect); + const QModelIndex index = indexAt(event->pos()); + if (!index.isValid() || (index.column() != KDirModel::Name)) { + m_dragging = false; + } else { + m_dragging = true; + m_dropRect = visualRect(index); + setDirtyRegion(m_dropRect); + } } void DolphinDetailsView::dropEvent(QDropEvent* event) @@ -168,6 +243,33 @@ void DolphinDetailsView::dropEvent(QDropEvent* event) event->source()); } QTreeView::dropEvent(event); + m_dragging = false; +} + +void DolphinDetailsView::paintEvent(QPaintEvent* event) +{ + QTreeView::paintEvent(event); + if (m_showElasticBand) { + // The following code has been taken from QListView + // and adapted to DolphinDetailsView. + // (C) 1992-2007 Trolltech ASA + QStyleOptionRubberBand opt; + opt.initFrom(this); + opt.shape = QRubberBand::Rectangle; + opt.opaque = false; + opt.rect = elasticBandRect(); + + QPainter painter(viewport()); + painter.save(); + style()->drawControl(QStyle::CE_RubberBand, &opt, &painter); + painter.restore(); + } + + // TODO: remove this code when the issue #160611 is solved in Qt 4.4 + if (m_dragging) { + const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight); + DolphinController::drawHoverIndication(viewport(), m_dropRect, brush); + } } void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting) @@ -192,6 +294,27 @@ void DolphinDetailsView::synchronizeSortingState(int column) m_controller->indicateSortOrderChange(sortOrder); } +void DolphinDetailsView::slotEntered(const QModelIndex& index) +{ + const QPoint pos = viewport()->mapFromGlobal(QCursor::pos()); + const int nameColumnWidth = header()->sectionSize(KDirModel::Name); + if (pos.x() < nameColumnWidth) { + m_controller->emitItemEntered(index); + } + else { + m_controller->emitViewportEntered(); + } +} + +void DolphinDetailsView::updateElasticBand() +{ + Q_ASSERT(m_showElasticBand); + QRect dirtyRegion(elasticBandRect()); + m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos()); + dirtyRegion = dirtyRegion.united(elasticBandRect()); + setDirtyRegion(dirtyRegion); +} + void DolphinDetailsView::zoomIn() { if (isZoomInPossible()) { @@ -244,4 +367,29 @@ void DolphinDetailsView::updateDecorationSize() doItemsLayout(); } +QPoint DolphinDetailsView::contentsPos() const +{ + // implementation note: the horizonal position is ignored currently, as no + // horizontal scrolling is done anyway during a selection + const QScrollBar* scrollbar = verticalScrollBar(); + Q_ASSERT(scrollbar != 0); + + const int maxHeight = maximumViewportSize().height(); + const int height = scrollbar->maximum() - scrollbar->minimum() + 1; + const int visibleHeight = model()->rowCount() + 1 - height; + if (visibleHeight <= 0) { + return QPoint(0, 0); + } + + const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight; + return QPoint(0, y); +} + +QRect DolphinDetailsView::elasticBandRect() const +{ + const QPoint pos(contentsPos()); + const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y()); + return QRect(topLeft, m_elasticBandDestination).normalized(); +} + #include "dolphindetailsview.moc"