Changeset View
Changeset View
Standalone View
Standalone View
src/filewidgets/kcolumnview.cpp
- This file was added.
| 1 | /** | ||||
|---|---|---|---|---|---|
| 2 | * This file is imported from qtbase (c7ec07d40115bef849574c81d619b629af9434a9) | ||||
| 3 | * and adjusted to build as part of KIO without needing any private | ||||
| 4 | * headers. This was necessary as QColumnView had too many drawbacks | ||||
| 5 | * and issues that could not be fixed by inheritance. | ||||
| 6 | * The changes are: | ||||
| 7 | * - Relay setIconSize to the child columns | ||||
| 8 | * - Relay setMouseTracking to the child columns | ||||
| 9 | * - Add rowsAboutToBeRemoved overload that closes to-be removed columns | ||||
| 10 | * - Change scrollTo(index) to scroll to the column with index as root or | ||||
| 11 | * the current preview of index if possible | ||||
| 12 | * - Change scrollTo(index) to always scroll the column into the target | ||||
| 13 | * position, even if an animation is pending or the column is visible, | ||||
| 14 | * but in the wrong place | ||||
| 15 | * - Change closeColumns to always relayout | ||||
| 16 | * - Support having no previewWidget (d->previewqColumn == nullptr) | ||||
| 17 | * - Install an event filter on the columns' viewports to react on a left | ||||
| 18 | * click into an empty spot to clear the selection | ||||
| 19 | * - Call _q_changeCurrentColumn directly instead of after the scroll | ||||
| 20 | * animation finished | ||||
| 21 | * - Remove show parameter of the private's createColumn method | ||||
| 22 | * | ||||
| 23 | * Unfortunately custom row delegates are not implemented as they are not | ||||
| 24 | * easily accessible by QAbstractItemView's public API. | ||||
| 25 | */ | ||||
| 26 | | ||||
| 27 | /**************************************************************************** | ||||
| 28 | ** | ||||
| 29 | ** Copyright (C) 2016 The Qt Company Ltd. | ||||
| 30 | ** Contact: https://www.qt.io/licensing/ | ||||
| 31 | ** | ||||
| 32 | ** This file is part of the QtWidgets module of the Qt Toolkit. | ||||
| 33 | ** | ||||
| 34 | ** $QT_BEGIN_LICENSE:LGPL$ | ||||
| 35 | ** Commercial License Usage | ||||
| 36 | ** Licensees holding valid commercial Qt licenses may use this file in | ||||
| 37 | ** accordance with the commercial license agreement provided with the | ||||
| 38 | ** Software or, alternatively, in accordance with the terms contained in | ||||
| 39 | ** a written agreement between you and The Qt Company. For licensing terms | ||||
| 40 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||||
| 41 | ** information use the contact form at https://www.qt.io/contact-us. | ||||
| 42 | ** | ||||
| 43 | ** GNU Lesser General Public License Usage | ||||
| 44 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||||
| 45 | ** General Public License version 3 as published by the Free Software | ||||
| 46 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | ||||
| 47 | ** packaging of this file. Please review the following information to | ||||
| 48 | ** ensure the GNU Lesser General Public License version 3 requirements | ||||
| 49 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | ||||
| 50 | ** | ||||
| 51 | ** GNU General Public License Usage | ||||
| 52 | ** Alternatively, this file may be used under the terms of the GNU | ||||
| 53 | ** General Public License version 2.0 or (at your option) the GNU General | ||||
| 54 | ** Public license version 3 or any later version approved by the KDE Free | ||||
| 55 | ** Qt Foundation. The licenses are as published by the Free Software | ||||
| 56 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | ||||
| 57 | ** included in the packaging of this file. Please review the following | ||||
| 58 | ** information to ensure the GNU General Public License requirements will | ||||
| 59 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | ||||
| 60 | ** https://www.gnu.org/licenses/gpl-3.0.html. | ||||
| 61 | ** | ||||
| 62 | ** $QT_END_LICENSE$ | ||||
| 63 | ** | ||||
| 64 | ****************************************************************************/ | ||||
| 65 | | ||||
| 66 | #include <qglobal.h> | ||||
| 67 | #include "kcolumnview.h" | ||||
| 68 | | ||||
| 69 | #include "kcolumnview_p.h" | ||||
| 70 | #include "kcolumnviewgrip_p.h" | ||||
| 71 | | ||||
| 72 | #include <QApplication> | ||||
| 73 | #include <qlistview.h> | ||||
| 74 | #include <qabstractitemdelegate.h> | ||||
| 75 | #include <qscrollbar.h> | ||||
| 76 | #include <qpainter.h> | ||||
| 77 | #include <qdebug.h> | ||||
| 78 | | ||||
| 79 | #define ANIMATION_DURATION_MSEC 150 | ||||
| 80 | | ||||
| 81 | /*! | ||||
| 82 | \since 4.3 | ||||
| 83 | \class KColumnView | ||||
| 84 | \brief The KColumnView class provides a model/view implementation of a column view. | ||||
| 85 | \ingroup model-view | ||||
| 86 | \ingroup advanced | ||||
| 87 | \inmodule QtWidgets | ||||
| 88 | | ||||
| 89 | KColumnView displays a model in a number of QListViews, one for each | ||||
| 90 | hierarchy in the tree. This is sometimes referred to as a cascading list. | ||||
| 91 | | ||||
| 92 | The KColumnView class is one of the \l{Model/View Classes} | ||||
| 93 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | ||||
| 94 | | ||||
| 95 | KColumnView implements the interfaces defined by the | ||||
| 96 | QAbstractItemView class to allow it to display data provided by | ||||
| 97 | models derived from the QAbstractItemModel class. | ||||
| 98 | | ||||
| 99 | \image kcolumnview.png | ||||
| 100 | | ||||
| 101 | \sa {Model/View Programming} | ||||
| 102 | */ | ||||
| 103 | | ||||
| 104 | /*! | ||||
| 105 | Constructs a column view with a \a parent to represent a model's | ||||
| 106 | data. Use setModel() to set the model. | ||||
| 107 | | ||||
| 108 | \sa QAbstractItemModel | ||||
| 109 | */ | ||||
| 110 | KColumnView::KColumnView(QWidget * parent) | ||||
| 111 | : QAbstractItemView(parent), d(new Private(this)) | ||||
| 112 | { | ||||
| 113 | d->initialize(); | ||||
| 114 | | ||||
| 115 | connect(this, SIGNAL(iconSizeChanged(QSize)), this, SLOT(_q_iconSizeChanged(QSize))); | ||||
| 116 | } | ||||
| 117 | | ||||
| 118 | void KColumnView::Private::initialize() | ||||
| 119 | { | ||||
| 120 | q->setTextElideMode(Qt::ElideMiddle); | ||||
| 121 | #ifndef QT_NO_ANIMATION | ||||
| 122 | currentAnimation.setDuration(ANIMATION_DURATION_MSEC); | ||||
| 123 | currentAnimation.setTargetObject(q->horizontalScrollBar()); | ||||
| 124 | currentAnimation.setPropertyName("value"); | ||||
| 125 | currentAnimation.setEasingCurve(QEasingCurve::InOutQuad); | ||||
| 126 | #endif //QT_NO_ANIMATION | ||||
| 127 | delete q->itemDelegate(); | ||||
| 128 | q->setItemDelegate(new KColumnViewDelegate(q)); | ||||
| 129 | } | ||||
| 130 | | ||||
| 131 | /*! | ||||
| 132 | Destroys the column view. | ||||
| 133 | */ | ||||
| 134 | KColumnView::~KColumnView() | ||||
| 135 | { | ||||
| 136 | } | ||||
| 137 | | ||||
| 138 | /*! | ||||
| 139 | \property KColumnView::resizeGripsVisible | ||||
| 140 | \brief the way to specify if the list views gets resize grips or not | ||||
| 141 | | ||||
| 142 | By default, \c visible is set to true | ||||
| 143 | | ||||
| 144 | \sa setRootIndex() | ||||
| 145 | */ | ||||
| 146 | void KColumnView::setResizeGripsVisible(bool visible) | ||||
| 147 | { | ||||
| 148 | if (d->showResizeGrips == visible) | ||||
| 149 | return; | ||||
| 150 | d->showResizeGrips = visible; | ||||
| 151 | for (int i = 0; i < d->columns.count(); ++i) { | ||||
| 152 | QAbstractItemView *view = d->columns[i]; | ||||
| 153 | if (visible) { | ||||
| 154 | KColumnViewGrip *grip = new KColumnViewGrip(view); | ||||
| 155 | view->setCornerWidget(grip); | ||||
| 156 | connect(grip, SIGNAL(gripMoved(int)), this, SLOT(_q_gripMoved(int))); | ||||
| 157 | } else { | ||||
| 158 | QWidget *widget = view->cornerWidget(); | ||||
| 159 | view->setCornerWidget(0); | ||||
| 160 | widget->deleteLater(); | ||||
| 161 | } | ||||
| 162 | } | ||||
| 163 | } | ||||
| 164 | | ||||
| 165 | bool KColumnView::resizeGripsVisible() const | ||||
| 166 | { | ||||
| 167 | return d->showResizeGrips; | ||||
| 168 | } | ||||
| 169 | | ||||
| 170 | /*! | ||||
| 171 | \reimp | ||||
| 172 | */ | ||||
| 173 | void KColumnView::setModel(QAbstractItemModel *model) | ||||
| 174 | { | ||||
| 175 | if (model == this->model()) | ||||
| 176 | return; | ||||
| 177 | | ||||
| 178 | if (this->model()) { | ||||
| 179 | disconnect(this->model(), 0, this, 0); | ||||
| 180 | d->closeColumns(); | ||||
| 181 | } | ||||
| 182 | | ||||
| 183 | QAbstractItemView::setModel(model); | ||||
| 184 | | ||||
| 185 | connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(_q_columnsInserted(QModelIndex,int,int))); | ||||
| 186 | } | ||||
| 187 | | ||||
| 188 | /*! | ||||
| 189 | \reimp | ||||
| 190 | */ | ||||
| 191 | void KColumnView::setRootIndex(const QModelIndex &index) | ||||
| 192 | { | ||||
| 193 | if (!model()) | ||||
| 194 | return; | ||||
| 195 | | ||||
| 196 | d->closeColumns(); | ||||
| 197 | Q_ASSERT(d->columns.count() == 0); | ||||
| 198 | | ||||
| 199 | QAbstractItemView *view = d->createColumn(index); | ||||
| 200 | if (view) { | ||||
| 201 | if (view->selectionModel()) { | ||||
| 202 | view->selectionModel()->deleteLater(); | ||||
| 203 | } | ||||
| 204 | | ||||
| 205 | if (view->model()) { | ||||
| 206 | view->setSelectionModel(selectionModel()); | ||||
| 207 | } | ||||
| 208 | } | ||||
| 209 | | ||||
| 210 | QAbstractItemView::setRootIndex(index); | ||||
| 211 | d->updateScrollbars(); | ||||
| 212 | } | ||||
| 213 | | ||||
| 214 | /*! | ||||
| 215 | \reimp | ||||
| 216 | */ | ||||
| 217 | bool KColumnView::isIndexHidden(const QModelIndex &index) const | ||||
| 218 | { | ||||
| 219 | Q_UNUSED(index); | ||||
| 220 | return false; | ||||
| 221 | } | ||||
| 222 | | ||||
| 223 | /*! | ||||
| 224 | \reimp | ||||
| 225 | */ | ||||
| 226 | QModelIndex KColumnView::indexAt(const QPoint &point) const | ||||
| 227 | { | ||||
| 228 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 229 | QPoint topLeft = d->columns.at(i)->frameGeometry().topLeft(); | ||||
| 230 | QPoint adjustedPoint(point.x() - topLeft.x(), point.y() - topLeft.y()); | ||||
| 231 | QModelIndex index = d->columns.at(i)->indexAt(adjustedPoint); | ||||
| 232 | if (index.isValid()) | ||||
| 233 | return index; | ||||
| 234 | } | ||||
| 235 | return QModelIndex(); | ||||
| 236 | } | ||||
| 237 | | ||||
| 238 | /*! | ||||
| 239 | \reimp | ||||
| 240 | */ | ||||
| 241 | QRect KColumnView::visualRect(const QModelIndex &index) const | ||||
| 242 | { | ||||
| 243 | if (!index.isValid()) | ||||
| 244 | return QRect(); | ||||
| 245 | | ||||
| 246 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 247 | QRect rect = d->columns.at(i)->visualRect(index); | ||||
| 248 | if (!rect.isNull()) { | ||||
| 249 | rect.translate(d->columns.at(i)->frameGeometry().topLeft()); | ||||
| 250 | return rect; | ||||
| 251 | } | ||||
| 252 | } | ||||
| 253 | return QRect(); | ||||
| 254 | } | ||||
| 255 | | ||||
| 256 | /*! | ||||
| 257 | \reimp | ||||
| 258 | */ | ||||
| 259 | void KColumnView::scrollContentsBy(int dx, int dy) | ||||
| 260 | { | ||||
| 261 | if (d->columns.isEmpty() || dx == 0) | ||||
| 262 | return; | ||||
| 263 | | ||||
| 264 | dx = isRightToLeft() ? -dx : dx; | ||||
| 265 | for (int i = 0; i < d->columns.count(); ++i) | ||||
| 266 | d->columns.at(i)->move(d->columns.at(i)->x() + dx, 0); | ||||
| 267 | d->offset += dx; | ||||
| 268 | QAbstractItemView::scrollContentsBy(dx, dy); | ||||
| 269 | } | ||||
| 270 | | ||||
| 271 | /*! | ||||
| 272 | \reimp | ||||
| 273 | */ | ||||
| 274 | void KColumnView::scrollTo(const QModelIndex &index, ScrollHint hint) | ||||
| 275 | { | ||||
| 276 | #ifndef QT_NO_ANIMATION | ||||
| 277 | d->currentAnimation.stop(); | ||||
| 278 | #endif //QT_NO_ANIMATION | ||||
| 279 | | ||||
| 280 | // Fill up what is needed to get to index | ||||
| 281 | if (index.isValid()) { | ||||
| 282 | d->closeColumns(index, true); | ||||
| 283 | } | ||||
| 284 | | ||||
| 285 | // Nothing to do | ||||
| 286 | if (d->columns.isEmpty()) { | ||||
| 287 | return; | ||||
| 288 | } | ||||
| 289 | | ||||
| 290 | int targetColumn = -1; | ||||
| 291 | | ||||
| 292 | // Find the column we want to focus on | ||||
| 293 | for (int i = d->columns.count() - 1; i >= 0; --i) { | ||||
| 294 | auto column = d->columns[i]; | ||||
| 295 | | ||||
| 296 | // If the index is shown in the preview, it's the target | ||||
| 297 | if (column == d->previewColumn) { | ||||
| 298 | if (index == currentIndex()) { | ||||
| 299 | targetColumn = i; | ||||
| 300 | break; | ||||
| 301 | } | ||||
| 302 | | ||||
| 303 | continue; | ||||
| 304 | } | ||||
| 305 | | ||||
| 306 | // Otherwise it's the column with rootIndex() == index | ||||
| 307 | // or the column that contains index | ||||
| 308 | if (d->columns.at(i)->rootIndex() == index | ||||
| 309 | || d->columns.at(i)->rootIndex() == index.parent()) { | ||||
| 310 | targetColumn = i; | ||||
| 311 | break; | ||||
| 312 | } | ||||
| 313 | } | ||||
| 314 | | ||||
| 315 | // Not found? | ||||
| 316 | if (targetColumn == -1) { | ||||
| 317 | return; | ||||
| 318 | } | ||||
| 319 | | ||||
| 320 | { | ||||
| 321 | // Scroll all the columns to make the path visible | ||||
| 322 | int containerColumn = targetColumn; | ||||
| 323 | QModelIndex scrollIndex = index; | ||||
| 324 | if (d->columns[containerColumn]->rootIndex() == index) { | ||||
| 325 | // Scroll the column with index as root to the top | ||||
| 326 | d->columns.at(containerColumn)->scrollToTop(); | ||||
| 327 | containerColumn -= 1; | ||||
| 328 | } | ||||
| 329 | | ||||
| 330 | while (containerColumn >= 0) { | ||||
| 331 | // scrollTo in the container of index itself, setCurrentIndex for the path items | ||||
| 332 | if (containerColumn == targetColumn) { | ||||
| 333 | d->columns.at(containerColumn)->scrollTo(index, hint); | ||||
| 334 | } else { | ||||
| 335 | if(d->columns.at(containerColumn)->currentIndex() != scrollIndex) { | ||||
| 336 | d->columns.at(containerColumn)->setCurrentIndex(scrollIndex); | ||||
| 337 | } | ||||
| 338 | } | ||||
| 339 | scrollIndex = scrollIndex.parent(); | ||||
| 340 | containerColumn -= 1; | ||||
| 341 | } | ||||
| 342 | } | ||||
| 343 | | ||||
| 344 | int newScrollbarValue = 0; | ||||
| 345 | | ||||
| 346 | if (isLeftToRight()) { | ||||
| 347 | newScrollbarValue = d->columns.at(targetColumn)->x() + d->columns.at(targetColumn)->width() - viewport()->width(); | ||||
| 348 | } else { | ||||
| 349 | newScrollbarValue = d->columns.at(targetColumn)->x() + viewport()->width(); | ||||
| 350 | } | ||||
| 351 | | ||||
| 352 | newScrollbarValue += horizontalScrollBar()->value(); | ||||
| 353 | newScrollbarValue = qMax(0, newScrollbarValue); | ||||
| 354 | | ||||
| 355 | #ifndef QT_NO_ANIMATION | ||||
| 356 | if (style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) { | ||||
| 357 | d->currentAnimation.setEndValue(newScrollbarValue); | ||||
| 358 | d->currentAnimation.start(); | ||||
| 359 | } else | ||||
| 360 | #endif //QT_NO_ANIMATION | ||||
| 361 | { | ||||
| 362 | horizontalScrollBar()->setValue(newScrollbarValue); | ||||
| 363 | } | ||||
| 364 | } | ||||
| 365 | | ||||
| 366 | /*! | ||||
| 367 | \reimp | ||||
| 368 | Move left should go to the parent index | ||||
| 369 | Move right should go to the child index or down if there is no child | ||||
| 370 | */ | ||||
| 371 | QModelIndex KColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) | ||||
| 372 | { | ||||
| 373 | // the child views which have focus get to deal with this first and if | ||||
| 374 | // they don't accept it then it comes up this view and we only grip left/right | ||||
| 375 | Q_UNUSED(modifiers); | ||||
| 376 | if (!model()) | ||||
| 377 | return QModelIndex(); | ||||
| 378 | | ||||
| 379 | QModelIndex current = currentIndex(); | ||||
| 380 | if (isRightToLeft()) { | ||||
| 381 | if (cursorAction == MoveLeft) | ||||
| 382 | cursorAction = MoveRight; | ||||
| 383 | else if (cursorAction == MoveRight) | ||||
| 384 | cursorAction = MoveLeft; | ||||
| 385 | } | ||||
| 386 | switch (cursorAction) { | ||||
| 387 | case MoveLeft: | ||||
| 388 | if (current.parent().isValid() && current.parent() != rootIndex()) | ||||
| 389 | return (current.parent()); | ||||
| 390 | else | ||||
| 391 | return current; | ||||
| 392 | | ||||
| 393 | case MoveRight: | ||||
| 394 | if (model()->hasChildren(current)) | ||||
| 395 | return model()->index(0, 0, current); | ||||
| 396 | else | ||||
| 397 | return current.sibling(current.row() + 1, current.column()); | ||||
| 398 | | ||||
| 399 | default: | ||||
| 400 | break; | ||||
| 401 | } | ||||
| 402 | | ||||
| 403 | return QModelIndex(); | ||||
| 404 | } | ||||
| 405 | | ||||
| 406 | /*! | ||||
| 407 | \reimp | ||||
| 408 | */ | ||||
| 409 | void KColumnView::resizeEvent(QResizeEvent *event) | ||||
| 410 | { | ||||
| 411 | d->doLayout(); | ||||
| 412 | d->updateScrollbars(); | ||||
| 413 | if (!isRightToLeft()) { | ||||
| 414 | int diff = event->oldSize().width() - event->size().width(); | ||||
| 415 | if (diff < 0 && horizontalScrollBar()->isVisible() | ||||
| 416 | && horizontalScrollBar()->value() == horizontalScrollBar()->maximum()) { | ||||
| 417 | horizontalScrollBar()->setMaximum(horizontalScrollBar()->maximum() + diff); | ||||
| 418 | } | ||||
| 419 | } | ||||
| 420 | QAbstractItemView::resizeEvent(event); | ||||
| 421 | } | ||||
| 422 | | ||||
| 423 | /*! | ||||
| 424 | \internal | ||||
| 425 | */ | ||||
| 426 | void KColumnView::Private::updateScrollbars() | ||||
| 427 | { | ||||
| 428 | #ifndef QT_NO_ANIMATION | ||||
| 429 | if (currentAnimation.state() == QPropertyAnimation::Running) | ||||
| 430 | return; | ||||
| 431 | #endif //QT_NO_ANIMATION | ||||
| 432 | | ||||
| 433 | // find the total horizontal length of the laid out columns | ||||
| 434 | int horizontalLength = 0; | ||||
| 435 | if (!columns.isEmpty()) { | ||||
| 436 | horizontalLength = (columns.constLast()->x() + columns.constLast()->width()) - columns.constFirst()->x(); | ||||
| 437 | if (horizontalLength <= 0) // reverse mode | ||||
| 438 | horizontalLength = (columns.constFirst()->x() + columns.constFirst()->width()) - columns.constLast()->x(); | ||||
| 439 | } | ||||
| 440 | | ||||
| 441 | QSize viewportSize = q->viewport()->size(); | ||||
| 442 | auto hbar = q->horizontalScrollBar(); | ||||
| 443 | if (horizontalLength < viewportSize.width() && hbar->value() == 0) { | ||||
| 444 | hbar->setRange(0, 0); | ||||
| 445 | } else { | ||||
| 446 | int visibleLength = qMin(horizontalLength + q->horizontalOffset(), viewportSize.width()); | ||||
| 447 | int hiddenLength = horizontalLength - visibleLength; | ||||
| 448 | if (hiddenLength != hbar->maximum()) | ||||
| 449 | hbar->setRange(0, hiddenLength); | ||||
| 450 | } | ||||
| 451 | if (!columns.isEmpty()) { | ||||
| 452 | int pageStepSize = columns.at(0)->width(); | ||||
| 453 | if (pageStepSize != hbar->pageStep()) | ||||
| 454 | hbar->setPageStep(pageStepSize); | ||||
| 455 | } | ||||
| 456 | bool visible = (hbar->maximum() > 0); | ||||
| 457 | if (visible != hbar->isVisible()) | ||||
| 458 | hbar->setVisible(visible); | ||||
| 459 | } | ||||
| 460 | | ||||
| 461 | /*! | ||||
| 462 | \reimp | ||||
| 463 | */ | ||||
| 464 | int KColumnView::horizontalOffset() const | ||||
| 465 | { | ||||
| 466 | return d->offset; | ||||
| 467 | } | ||||
| 468 | | ||||
| 469 | /*! | ||||
| 470 | \reimp | ||||
| 471 | */ | ||||
| 472 | int KColumnView::verticalOffset() const | ||||
| 473 | { | ||||
| 474 | return 0; | ||||
| 475 | } | ||||
| 476 | | ||||
| 477 | /*! | ||||
| 478 | \reimp | ||||
| 479 | */ | ||||
| 480 | QRegion KColumnView::visualRegionForSelection(const QItemSelection &selection) const | ||||
| 481 | { | ||||
| 482 | int ranges = selection.count(); | ||||
| 483 | | ||||
| 484 | if (ranges == 0) | ||||
| 485 | return QRect(); | ||||
| 486 | | ||||
| 487 | // Note that we use the top and bottom functions of the selection range | ||||
| 488 | // since the data is stored in rows. | ||||
| 489 | int firstRow = selection.at(0).top(); | ||||
| 490 | int lastRow = selection.at(0).top(); | ||||
| 491 | for (int i = 0; i < ranges; ++i) { | ||||
| 492 | firstRow = qMin(firstRow, selection.at(i).top()); | ||||
| 493 | lastRow = qMax(lastRow, selection.at(i).bottom()); | ||||
| 494 | } | ||||
| 495 | | ||||
| 496 | QModelIndex firstIdx = model()->index(qMin(firstRow, lastRow), 0, rootIndex()); | ||||
| 497 | QModelIndex lastIdx = model()->index(qMax(firstRow, lastRow), 0, rootIndex()); | ||||
| 498 | | ||||
| 499 | if (firstIdx == lastIdx) | ||||
| 500 | return visualRect(firstIdx); | ||||
| 501 | | ||||
| 502 | QRegion firstRegion = visualRect(firstIdx); | ||||
| 503 | QRegion lastRegion = visualRect(lastIdx); | ||||
| 504 | return firstRegion.united(lastRegion); | ||||
| 505 | } | ||||
| 506 | | ||||
| 507 | /*! | ||||
| 508 | \reimp | ||||
| 509 | */ | ||||
| 510 | void KColumnView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) | ||||
| 511 | { | ||||
| 512 | Q_UNUSED(rect); | ||||
| 513 | Q_UNUSED(command); | ||||
| 514 | } | ||||
| 515 | | ||||
| 516 | /*! | ||||
| 517 | \reimp | ||||
| 518 | */ | ||||
| 519 | void KColumnView::setSelectionModel(QItemSelectionModel *newSelectionModel) | ||||
| 520 | { | ||||
| 521 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 522 | if (d->columns.at(i)->selectionModel() == selectionModel()) { | ||||
| 523 | d->columns.at(i)->setSelectionModel(newSelectionModel); | ||||
| 524 | break; | ||||
| 525 | } | ||||
| 526 | } | ||||
| 527 | QAbstractItemView::setSelectionModel(newSelectionModel); | ||||
| 528 | } | ||||
| 529 | | ||||
| 530 | /*! | ||||
| 531 | \reimp | ||||
| 532 | */ | ||||
| 533 | QSize KColumnView::sizeHint() const | ||||
| 534 | { | ||||
| 535 | QSize sizeHint; | ||||
| 536 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 537 | sizeHint += d->columns.at(i)->sizeHint(); | ||||
| 538 | } | ||||
| 539 | return sizeHint.expandedTo(QAbstractItemView::sizeHint()); | ||||
| 540 | } | ||||
| 541 | | ||||
| 542 | /*! | ||||
| 543 | \internal | ||||
| 544 | Move all widgets from the corner grip and to the right | ||||
| 545 | */ | ||||
| 546 | void KColumnView::Private::_q_gripMoved(int offset) | ||||
| 547 | { | ||||
| 548 | QObject *grip = q->sender(); | ||||
| 549 | Q_ASSERT(grip); | ||||
| 550 | | ||||
| 551 | if (q->isRightToLeft()) | ||||
| 552 | offset = -1 * offset; | ||||
| 553 | | ||||
| 554 | bool found = false; | ||||
| 555 | for (int i = 0; i < columns.size(); ++i) { | ||||
| 556 | if (!found && columns.at(i)->cornerWidget() == grip) { | ||||
| 557 | found = true; | ||||
| 558 | columnSizes[i] = columns.at(i)->width(); | ||||
| 559 | if (q->isRightToLeft()) | ||||
| 560 | columns.at(i)->move(columns.at(i)->x() + offset, 0); | ||||
| 561 | continue; | ||||
| 562 | } | ||||
| 563 | if (!found) | ||||
| 564 | continue; | ||||
| 565 | | ||||
| 566 | int currentX = columns.at(i)->x(); | ||||
| 567 | columns.at(i)->move(currentX + offset, 0); | ||||
| 568 | } | ||||
| 569 | | ||||
| 570 | updateScrollbars(); | ||||
| 571 | } | ||||
| 572 | | ||||
| 573 | /*! | ||||
| 574 | \internal | ||||
| 575 | | ||||
| 576 | Find where the current columns intersect parent's columns | ||||
| 577 | | ||||
| 578 | Delete any extra columns and insert any needed columns. | ||||
| 579 | */ | ||||
| 580 | void KColumnView::Private::closeColumns(const QModelIndex &parent, bool build) | ||||
| 581 | { | ||||
| 582 | if (columns.isEmpty()) | ||||
| 583 | return; | ||||
| 584 | | ||||
| 585 | bool clearAll = !parent.isValid(); | ||||
| 586 | bool passThroughRoot = false; | ||||
| 587 | | ||||
| 588 | QVector<QModelIndex> dirsToAppend; | ||||
| 589 | | ||||
| 590 | // Find the last column that matches the parent's tree | ||||
| 591 | int currentColumn = -1; | ||||
| 592 | QModelIndex parentIndex = parent; | ||||
| 593 | while (currentColumn == -1 && parentIndex.isValid()) { | ||||
| 594 | if (columns.isEmpty()) | ||||
| 595 | break; | ||||
| 596 | parentIndex = parentIndex.parent(); | ||||
| 597 | if (q->rootIndex() == parentIndex) | ||||
| 598 | passThroughRoot = true; | ||||
| 599 | if (!parentIndex.isValid()) | ||||
| 600 | break; | ||||
| 601 | for (int i = columns.size() - 1; i >= 0; --i) { | ||||
| 602 | if (columns.at(i)->rootIndex() == parentIndex) { | ||||
| 603 | currentColumn = i; | ||||
| 604 | break; | ||||
| 605 | } | ||||
| 606 | } | ||||
| 607 | if (currentColumn == -1) | ||||
| 608 | dirsToAppend.append(parentIndex); | ||||
| 609 | } | ||||
| 610 | | ||||
| 611 | // Someone wants to go to an index that can be reached without changing | ||||
| 612 | // the root index, don't allow them | ||||
| 613 | if (!clearAll && !passThroughRoot && currentColumn == -1) | ||||
| 614 | return; | ||||
| 615 | | ||||
| 616 | if (currentColumn == -1 && parent.isValid()) | ||||
| 617 | currentColumn = 0; | ||||
| 618 | | ||||
| 619 | // Optimization so we don't go deleting and then creating the same thing | ||||
| 620 | bool alreadyExists = false; | ||||
| 621 | if (build && columns.size() > currentColumn + 1) { | ||||
| 622 | bool viewingParent = (columns.at(currentColumn + 1)->rootIndex() == parent); | ||||
| 623 | bool viewingChild = (!q->model()->hasChildren(parent) | ||||
| 624 | && !columns.at(currentColumn + 1)->rootIndex().isValid()); | ||||
| 625 | if (viewingParent || viewingChild) { | ||||
| 626 | currentColumn++; | ||||
| 627 | alreadyExists = true; | ||||
| 628 | } | ||||
| 629 | } | ||||
| 630 | | ||||
| 631 | // Delete columns that don't match our path | ||||
| 632 | for (int i = columns.size() - 1; i > currentColumn; --i) { | ||||
| 633 | QAbstractItemView* notShownAnymore = columns.at(i); | ||||
| 634 | columns.removeAt(i); | ||||
| 635 | notShownAnymore->setVisible(false); | ||||
| 636 | if (notShownAnymore != previewColumn) | ||||
| 637 | notShownAnymore->deleteLater(); | ||||
| 638 | } | ||||
| 639 | | ||||
| 640 | if (columns.isEmpty()) { | ||||
| 641 | offset = 0; | ||||
| 642 | updateScrollbars(); | ||||
| 643 | } | ||||
| 644 | | ||||
| 645 | // Now fill in missing columns | ||||
| 646 | while (!dirsToAppend.isEmpty()) { | ||||
| 647 | QAbstractItemView *newView = createColumn(dirsToAppend.takeLast()); | ||||
| 648 | if (!dirsToAppend.isEmpty()) | ||||
| 649 | newView->setCurrentIndex(dirsToAppend.constLast()); | ||||
| 650 | } | ||||
| 651 | | ||||
| 652 | if (build && !alreadyExists) { | ||||
| 653 | createColumn(parent); | ||||
| 654 | } | ||||
| 655 | | ||||
| 656 | doLayout(); | ||||
| 657 | updateScrollbars(); | ||||
| 658 | _q_changeCurrentColumn(); | ||||
| 659 | } | ||||
| 660 | | ||||
| 661 | void KColumnView::Private::_q_clicked(const QModelIndex &index) | ||||
| 662 | { | ||||
| 663 | QModelIndex parent = index.parent(); | ||||
| 664 | QAbstractItemView *columnClicked = 0; | ||||
| 665 | for (int column = 0; column < columns.count(); ++column) { | ||||
| 666 | if (columns.at(column)->rootIndex() == parent) { | ||||
| 667 | columnClicked = columns[column]; | ||||
| 668 | break; | ||||
| 669 | } | ||||
| 670 | } | ||||
| 671 | if (q->selectionModel() && columnClicked) { | ||||
| 672 | QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::Current; | ||||
| 673 | if (columnClicked->selectionModel()->isSelected(index)) | ||||
| 674 | flags |= QItemSelectionModel::Select; | ||||
| 675 | q->selectionModel()->setCurrentIndex(index, flags); | ||||
| 676 | } | ||||
| 677 | } | ||||
| 678 | | ||||
| 679 | /*! | ||||
| 680 | \internal | ||||
| 681 | Create a new column for \a index. A grip is attached if requested and it is shown | ||||
| 682 | if requested. | ||||
| 683 | | ||||
| 684 | Return the new view. If index has no children and no previewWidget is set, | ||||
| 685 | it will return nullptr; | ||||
| 686 | | ||||
| 687 | \sa createColumn(), setPreviewWidget() | ||||
| 688 | \sa doLayout() | ||||
| 689 | */ | ||||
| 690 | QAbstractItemView *KColumnView::Private::createColumn(const QModelIndex &index) | ||||
| 691 | { | ||||
| 692 | QAbstractItemView *view = 0; | ||||
| 693 | auto viewport = q->viewport(); | ||||
| 694 | if (q->model()->hasChildren(index)) { | ||||
| 695 | view = q->createColumn(index); | ||||
| 696 | q->connect(view, SIGNAL(clicked(QModelIndex)), | ||||
| 697 | q, SLOT(_q_clicked(QModelIndex))); | ||||
| 698 | } else { | ||||
| 699 | if (!previewColumn) { | ||||
| 700 | return nullptr; | ||||
| 701 | } | ||||
| 702 | view = previewColumn; | ||||
| 703 | view->setMinimumWidth(qMax(view->minimumWidth(), previewWidget->minimumWidth())); | ||||
| 704 | } | ||||
| 705 | | ||||
| 706 | q->connect(view, SIGNAL(activated(QModelIndex)), | ||||
| 707 | q, SIGNAL(activated(QModelIndex))); | ||||
| 708 | q->connect(view, SIGNAL(clicked(QModelIndex)), | ||||
| 709 | q, SIGNAL(clicked(QModelIndex))); | ||||
| 710 | q->connect(view, SIGNAL(doubleClicked(QModelIndex)), | ||||
| 711 | q, SIGNAL(doubleClicked(QModelIndex))); | ||||
| 712 | q->connect(view, SIGNAL(entered(QModelIndex)), | ||||
| 713 | q, SIGNAL(entered(QModelIndex))); | ||||
| 714 | q->connect(view, SIGNAL(pressed(QModelIndex)), | ||||
| 715 | q, SIGNAL(pressed(QModelIndex))); | ||||
| 716 | | ||||
| 717 | view->setFocusPolicy(Qt::NoFocus); | ||||
| 718 | view->setParent(viewport); | ||||
| 719 | Q_ASSERT(view); | ||||
| 720 | | ||||
| 721 | // Setup corner grip | ||||
| 722 | if (showResizeGrips) { | ||||
| 723 | KColumnViewGrip *grip = new KColumnViewGrip(view); | ||||
| 724 | view->setCornerWidget(grip); | ||||
| 725 | q->connect(grip, SIGNAL(gripMoved(int)), q, SLOT(_q_gripMoved(int))); | ||||
| 726 | } | ||||
| 727 | | ||||
| 728 | if (columnSizes.count() > columns.count()) { | ||||
| 729 | view->setGeometry(0, 0, columnSizes.at(columns.count()), viewport->height()); | ||||
| 730 | } else { | ||||
| 731 | int initialWidth = view->sizeHint().width(); | ||||
| 732 | if (q->isRightToLeft()) | ||||
| 733 | view->setGeometry(viewport->width() - initialWidth, 0, initialWidth, viewport->height()); | ||||
| 734 | else | ||||
| 735 | view->setGeometry(0, 0, initialWidth, viewport->height()); | ||||
| 736 | columnSizes.resize(qMax(columnSizes.count(), columns.count() + 1)); | ||||
| 737 | columnSizes[columns.count()] = initialWidth; | ||||
| 738 | } | ||||
| 739 | if (!columns.isEmpty() && columns.constLast()->isHidden()) | ||||
| 740 | columns.constLast()->setVisible(true); | ||||
| 741 | | ||||
| 742 | columns.append(view); | ||||
| 743 | doLayout(); | ||||
| 744 | updateScrollbars(); | ||||
| 745 | view->setVisible(true); | ||||
| 746 | return view; | ||||
| 747 | } | ||||
| 748 | | ||||
| 749 | /*! | ||||
| 750 | \fn void KColumnView::updatePreviewWidget(const QModelIndex &index) | ||||
| 751 | | ||||
| 752 | This signal is emitted when the preview widget should be updated to | ||||
| 753 | provide rich information about \a index | ||||
| 754 | | ||||
| 755 | \sa previewWidget() | ||||
| 756 | */ | ||||
| 757 | | ||||
| 758 | /*! | ||||
| 759 | To use a custom widget for the final column when you select | ||||
| 760 | an item overload this function and return a widget. | ||||
| 761 | \a index is the root index that will be assigned to the view. | ||||
| 762 | | ||||
| 763 | Return the new view. KColumnView will automatically take ownership of the widget. | ||||
| 764 | | ||||
| 765 | \sa setPreviewWidget() | ||||
| 766 | */ | ||||
| 767 | QAbstractItemView *KColumnView::createColumn(const QModelIndex &index) | ||||
| 768 | { | ||||
| 769 | QListView *view = new QListView(viewport()); | ||||
| 770 | | ||||
| 771 | initializeColumn(view); | ||||
| 772 | | ||||
| 773 | view->setRootIndex(index); | ||||
| 774 | if (model()->canFetchMore(index)) | ||||
| 775 | model()->fetchMore(index); | ||||
| 776 | | ||||
| 777 | return view; | ||||
| 778 | } | ||||
| 779 | | ||||
| 780 | /*! | ||||
| 781 | Copies the behavior and options of the column view and applies them to | ||||
| 782 | the \a column such as the iconSize(), textElideMode() and | ||||
| 783 | alternatingRowColors(). This can be useful when reimplementing | ||||
| 784 | createColumn(). | ||||
| 785 | | ||||
| 786 | \since 4.4 | ||||
| 787 | \sa createColumn() | ||||
| 788 | */ | ||||
| 789 | void KColumnView::initializeColumn(QAbstractItemView *column) | ||||
| 790 | { | ||||
| 791 | column->setFrameShape(QFrame::NoFrame); | ||||
| 792 | column->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
| 793 | column->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | ||||
| 794 | column->setMinimumWidth(100); | ||||
| 795 | column->setAttribute(Qt::WA_MacShowFocusRect, false); | ||||
| 796 | column->viewport()->installEventFilter(this); | ||||
| 797 | | ||||
| 798 | #ifndef QT_NO_DRAGANDDROP | ||||
| 799 | column->setDragDropMode(dragDropMode()); | ||||
| 800 | column->setDragDropOverwriteMode(dragDropOverwriteMode()); | ||||
| 801 | column->setDropIndicatorShown(showDropIndicator()); | ||||
| 802 | #endif | ||||
| 803 | column->setAlternatingRowColors(alternatingRowColors()); | ||||
| 804 | column->setAutoScroll(hasAutoScroll()); | ||||
| 805 | column->setEditTriggers(editTriggers()); | ||||
| 806 | column->setHorizontalScrollMode(horizontalScrollMode()); | ||||
| 807 | column->setMouseTracking(hasMouseTracking()); | ||||
| 808 | column->setIconSize(iconSize()); | ||||
| 809 | column->setSelectionBehavior(selectionBehavior()); | ||||
| 810 | column->setSelectionMode(selectionMode()); | ||||
| 811 | column->setTabKeyNavigation(tabKeyNavigation()); | ||||
| 812 | column->setTextElideMode(textElideMode()); | ||||
| 813 | column->setVerticalScrollMode(verticalScrollMode()); | ||||
| 814 | | ||||
| 815 | column->setModel(model()); | ||||
| 816 | | ||||
| 817 | // Copy the custom delegate per row | ||||
| 818 | /*for (auto i = d->rowDelegates.cbegin(), end = d->rowDelegates.cend(); i != end; ++i) | ||||
| 819 | column->setItemDelegateForRow(i.key(), i.value());*/ | ||||
| 820 | | ||||
| 821 | // set the delegate to be the columnview delegate | ||||
| 822 | QAbstractItemDelegate *delegate = column->itemDelegate(); | ||||
| 823 | column->setItemDelegate(itemDelegate()); | ||||
| 824 | delete delegate; | ||||
| 825 | } | ||||
| 826 | | ||||
| 827 | /*! | ||||
| 828 | Returns the preview widget, or 0 if there is none. | ||||
| 829 | | ||||
| 830 | \sa setPreviewWidget(), updatePreviewWidget() | ||||
| 831 | */ | ||||
| 832 | QWidget *KColumnView::previewWidget() const | ||||
| 833 | { | ||||
| 834 | return d->previewWidget; | ||||
| 835 | } | ||||
| 836 | | ||||
| 837 | /*! | ||||
| 838 | Sets the preview \a widget. | ||||
| 839 | | ||||
| 840 | The \a widget becomes a child of the column view, and will be | ||||
| 841 | destroyed when the column area is deleted or when a new widget is | ||||
| 842 | set. | ||||
| 843 | | ||||
| 844 | \sa previewWidget(), updatePreviewWidget() | ||||
| 845 | */ | ||||
| 846 | void KColumnView::setPreviewWidget(QWidget *widget) | ||||
| 847 | { | ||||
| 848 | d->setPreviewWidget(widget); | ||||
| 849 | } | ||||
| 850 | | ||||
| 851 | /*! | ||||
| 852 | \internal | ||||
| 853 | */ | ||||
| 854 | void KColumnView::Private::setPreviewWidget(QWidget *widget) | ||||
| 855 | { | ||||
| 856 | if (previewColumn) { | ||||
| 857 | if (!columns.isEmpty() && columns.constLast() == previewColumn) | ||||
| 858 | columns.removeLast(); | ||||
| 859 | previewColumn->deleteLater(); | ||||
| 860 | previewColumn = nullptr; | ||||
| 861 | } | ||||
| 862 | | ||||
| 863 | if (!widget) { | ||||
| 864 | return; | ||||
| 865 | } | ||||
| 866 | | ||||
| 867 | KColumnViewPreviewColumn *column = new KColumnViewPreviewColumn(q); | ||||
| 868 | column->setPreviewWidget(widget); | ||||
| 869 | previewColumn = column; | ||||
| 870 | previewColumn->hide(); | ||||
| 871 | previewColumn->setFrameShape(QFrame::NoFrame); | ||||
| 872 | previewColumn->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); | ||||
| 873 | previewColumn->setSelectionMode(QAbstractItemView::NoSelection); | ||||
| 874 | previewColumn->setMinimumWidth(qMax(previewColumn->verticalScrollBar()->width(), | ||||
| 875 | previewColumn->minimumWidth())); | ||||
| 876 | previewWidget = widget; | ||||
| 877 | previewWidget->setParent(previewColumn->viewport()); | ||||
| 878 | } | ||||
| 879 | | ||||
| 880 | /*! | ||||
| 881 | Sets the column widths to the values given in the \a list. Extra values in the list are | ||||
| 882 | kept and used when the columns are created. | ||||
| 883 | | ||||
| 884 | If list contains too few values, only width of the rest of the columns will not be modified. | ||||
| 885 | | ||||
| 886 | \sa columnWidths(), createColumn() | ||||
| 887 | */ | ||||
| 888 | void KColumnView::setColumnWidths(const QList<int> &list) | ||||
| 889 | { | ||||
| 890 | int i = 0; | ||||
| 891 | const int listCount = list.count(); | ||||
| 892 | const int count = qMin(listCount, d->columns.count()); | ||||
| 893 | for (; i < count; ++i) { | ||||
| 894 | d->columns.at(i)->resize(list.at(i), d->columns.at(i)->height()); | ||||
| 895 | d->columnSizes[i] = list.at(i); | ||||
| 896 | } | ||||
| 897 | | ||||
| 898 | d->columnSizes.reserve(listCount); | ||||
| 899 | for (; i < listCount; ++i) | ||||
| 900 | d->columnSizes.append(list.at(i)); | ||||
| 901 | } | ||||
| 902 | | ||||
| 903 | /*! | ||||
| 904 | Returns a list of the width of all the columns in this view. | ||||
| 905 | | ||||
| 906 | \sa setColumnWidths() | ||||
| 907 | */ | ||||
| 908 | QList<int> KColumnView::columnWidths() const | ||||
| 909 | { | ||||
| 910 | QList<int> list; | ||||
| 911 | const int columnCount = d->columns.count(); | ||||
| 912 | list.reserve(columnCount); | ||||
| 913 | for (int i = 0; i < columnCount; ++i) | ||||
| 914 | list.append(d->columnSizes.at(i)); | ||||
| 915 | return list; | ||||
| 916 | } | ||||
| 917 | | ||||
| 918 | /*! | ||||
| 919 | \reimp | ||||
| 920 | */ | ||||
| 921 | void KColumnView::rowsInserted(const QModelIndex &parent, int start, int end) | ||||
| 922 | { | ||||
| 923 | QAbstractItemView::rowsInserted(parent, start, end); | ||||
| 924 | d->checkColumnCreation(parent); | ||||
| 925 | } | ||||
| 926 | | ||||
| 927 | /*! | ||||
| 928 | \reimp | ||||
| 929 | */ | ||||
| 930 | void KColumnView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) | ||||
| 931 | { | ||||
| 932 | if (!current.isValid()) { | ||||
| 933 | QAbstractItemView::currentChanged(current, previous); | ||||
| 934 | return; | ||||
| 935 | } | ||||
| 936 | | ||||
| 937 | QModelIndex currentParent = current.parent(); | ||||
| 938 | // optimize for just moving up/down in a list where the child view doesn't change | ||||
| 939 | if (currentParent == previous.parent() | ||||
| 940 | && model()->hasChildren(current) && model()->hasChildren(previous)) { | ||||
| 941 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 942 | if (currentParent == d->columns.at(i)->rootIndex()) { | ||||
| 943 | if (d->columns.size() > i + 1) { | ||||
| 944 | QAbstractItemView::currentChanged(current, previous); | ||||
| 945 | return; | ||||
| 946 | } | ||||
| 947 | break; | ||||
| 948 | } | ||||
| 949 | } | ||||
| 950 | } | ||||
| 951 | | ||||
| 952 | // Scrolling to the right we need to have an empty spot | ||||
| 953 | bool found = false; | ||||
| 954 | if (currentParent == previous) { | ||||
| 955 | for (int i = 0; i < d->columns.size(); ++i) { | ||||
| 956 | if (currentParent == d->columns.at(i)->rootIndex()) { | ||||
| 957 | found = true; | ||||
| 958 | if (d->columns.size() < i + 2) { | ||||
| 959 | d->createColumn(current); | ||||
| 960 | } | ||||
| 961 | break; | ||||
| 962 | } | ||||
| 963 | } | ||||
| 964 | } | ||||
| 965 | if (!found) | ||||
| 966 | d->closeColumns(current, true); | ||||
| 967 | | ||||
| 968 | if (!model()->hasChildren(current)) | ||||
| 969 | emit updatePreviewWidget(current); | ||||
| 970 | | ||||
| 971 | QAbstractItemView::currentChanged(current, previous); | ||||
| 972 | } | ||||
| 973 | | ||||
| 974 | void KColumnView::changeEvent(QEvent *event) | ||||
| 975 | { | ||||
| 976 | if (event->type() != QEvent::MouseTrackingChange) | ||||
| 977 | return; | ||||
| 978 | | ||||
| 979 | for (auto *column : d->columns) { | ||||
| 980 | column->setMouseTracking(hasMouseTracking()); | ||||
| 981 | } | ||||
| 982 | } | ||||
| 983 | | ||||
| 984 | void KColumnView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) | ||||
| 985 | { | ||||
| 986 | // Columns may need to be closed | ||||
| 987 | for (int i = 0; i < d->columns.count(); ++i) { | ||||
| 988 | auto column = d->columns[i]; | ||||
| 989 | if (column->rootIndex().parent() == parent | ||||
| 990 | && column->rootIndex().row() >= start && column->rootIndex().row() <= end) { | ||||
| 991 | d->closeColumns(parent, true); | ||||
| 992 | } | ||||
| 993 | } | ||||
| 994 | | ||||
| 995 | QAbstractItemView::rowsAboutToBeRemoved(parent, start, end); | ||||
| 996 | } | ||||
| 997 | | ||||
| 998 | bool KColumnView::eventFilter(QObject *obj, QEvent *event) | ||||
| 999 | { | ||||
| 1000 | // Clear the selection if clicking on an empty spot without | ||||
| 1001 | // having ctrl or shift pressed | ||||
| 1002 | if (event->type() == QEvent::MouseButtonPress) { | ||||
| 1003 | auto column = qobject_cast<QAbstractItemView*>(obj->parent()); | ||||
| 1004 | if (!column) { | ||||
| 1005 | return false; | ||||
| 1006 | } | ||||
| 1007 | | ||||
| 1008 | const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers(); | ||||
| 1009 | if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier)) { | ||||
| 1010 | const QModelIndex index = column->indexAt(static_cast<QMouseEvent*>(event)->pos()); | ||||
| 1011 | if (!index.isValid()) { | ||||
| 1012 | clearSelection(); | ||||
| 1013 | } | ||||
| 1014 | } | ||||
| 1015 | } | ||||
| 1016 | | ||||
| 1017 | return false; | ||||
| 1018 | } | ||||
| 1019 | | ||||
| 1020 | /* | ||||
| 1021 | We have change the current column and need to update focus and selection models | ||||
| 1022 | on the new current column. | ||||
| 1023 | */ | ||||
| 1024 | void KColumnView::Private::_q_changeCurrentColumn() | ||||
| 1025 | { | ||||
| 1026 | if (columns.isEmpty()) | ||||
| 1027 | return; | ||||
| 1028 | | ||||
| 1029 | QModelIndex current = q->currentIndex(); | ||||
| 1030 | | ||||
| 1031 | // Set up the "current" column with focus | ||||
| 1032 | int currentColumn = qMax(0, columns.size() - 1); | ||||
| 1033 | if (currentColumn != 0 && columns.constLast() == previewColumn) | ||||
| 1034 | --currentColumn; | ||||
| 1035 | | ||||
| 1036 | QAbstractItemView *parentColumn = columns.at(currentColumn); | ||||
| 1037 | if (q->hasFocus()) | ||||
| 1038 | parentColumn->setFocus(Qt::OtherFocusReason); | ||||
| 1039 | q->setFocusProxy(parentColumn); | ||||
| 1040 | | ||||
| 1041 | // find the column that is our current selection model and give it a new one. | ||||
| 1042 | for (int i = 0; i < columns.size(); ++i) { | ||||
| 1043 | if (columns.at(i)->selectionModel() == q->selectionModel()) { | ||||
| 1044 | QItemSelectionModel *replacementSelectionModel = | ||||
| 1045 | new QItemSelectionModel(parentColumn->model()); | ||||
| 1046 | replacementSelectionModel->setCurrentIndex( | ||||
| 1047 | q->selectionModel()->currentIndex(), QItemSelectionModel::Current); | ||||
| 1048 | replacementSelectionModel->select( | ||||
| 1049 | q->selectionModel()->selection(), QItemSelectionModel::Select); | ||||
| 1050 | QAbstractItemView *view = columns.at(i); | ||||
| 1051 | view->setSelectionModel(replacementSelectionModel); | ||||
| 1052 | view->setFocusPolicy(Qt::NoFocus); | ||||
| 1053 | if (columns.size() > i + 1) { | ||||
| 1054 | const QModelIndex newRootIndex = columns.at(i + 1)->rootIndex(); | ||||
| 1055 | if (newRootIndex.isValid()) | ||||
| 1056 | view->setCurrentIndex(newRootIndex); | ||||
| 1057 | } | ||||
| 1058 | break; | ||||
| 1059 | } | ||||
| 1060 | } | ||||
| 1061 | parentColumn->selectionModel()->deleteLater(); | ||||
| 1062 | parentColumn->setFocusPolicy(Qt::StrongFocus); | ||||
| 1063 | parentColumn->setSelectionModel(q->selectionModel()); | ||||
| 1064 | // We want the parent selection to stay highlighted (but dimmed depending upon the color theme) | ||||
| 1065 | if (currentColumn > 0) { | ||||
| 1066 | parentColumn = columns.at(currentColumn - 1); | ||||
| 1067 | if (parentColumn->currentIndex() != current.parent()) | ||||
| 1068 | parentColumn->setCurrentIndex(current.parent()); | ||||
| 1069 | } | ||||
| 1070 | | ||||
| 1071 | if (columns.constLast()->isHidden()) { | ||||
| 1072 | columns.constLast()->setVisible(true); | ||||
| 1073 | } | ||||
| 1074 | if (columns.constLast() == previewColumn && columns.constLast()->selectionModel()) | ||||
| 1075 | columns.constLast()->selectionModel()->clear(); | ||||
| 1076 | updateScrollbars(); | ||||
| 1077 | } | ||||
| 1078 | | ||||
| 1079 | /*! | ||||
| 1080 | \reimp | ||||
| 1081 | */ | ||||
| 1082 | void KColumnView::selectAll() | ||||
| 1083 | { | ||||
| 1084 | if (!model() || !selectionModel()) | ||||
| 1085 | return; | ||||
| 1086 | | ||||
| 1087 | QModelIndexList indexList = selectionModel()->selectedIndexes(); | ||||
| 1088 | QModelIndex parent = rootIndex(); | ||||
| 1089 | QItemSelection selection; | ||||
| 1090 | if (indexList.count() >= 1) | ||||
| 1091 | parent = indexList.at(0).parent(); | ||||
| 1092 | if (indexList.count() == 1) { | ||||
| 1093 | parent = indexList.at(0); | ||||
| 1094 | if (!model()->hasChildren(parent)) | ||||
| 1095 | parent = parent.parent(); | ||||
| 1096 | else | ||||
| 1097 | selection.append(QItemSelectionRange(parent, parent)); | ||||
| 1098 | } | ||||
| 1099 | | ||||
| 1100 | QModelIndex tl = model()->index(0, 0, parent); | ||||
| 1101 | QModelIndex br = model()->index(model()->rowCount(parent) - 1, | ||||
| 1102 | model()->columnCount(parent) - 1, | ||||
| 1103 | parent); | ||||
| 1104 | selection.append(QItemSelectionRange(tl, br)); | ||||
| 1105 | selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect); | ||||
| 1106 | } | ||||
| 1107 | | ||||
| 1108 | void KColumnView::showPathTo(const QModelIndex &index) | ||||
| 1109 | { | ||||
| 1110 | // Most of the work is done by the currentChanged method, | ||||
| 1111 | // but it is not called for the root index (QModelIndex()), | ||||
| 1112 | // so we need to handle that special case here. | ||||
| 1113 | if (!index.isValid()) { | ||||
| 1114 | // Root | ||||
| 1115 | | ||||
| 1116 | while (d->columns.count() > 1) { | ||||
| 1117 | auto c = d->columns.constLast(); | ||||
| 1118 | if (c == d->previewColumn) { | ||||
| 1119 | break; | ||||
| 1120 | } | ||||
| 1121 | | ||||
| 1122 | d->columns.removeLast(); | ||||
| 1123 | c->setVisible(false); | ||||
| 1124 | delete c; | ||||
| 1125 | } | ||||
| 1126 | | ||||
| 1127 | setCurrentIndex(index); | ||||
| 1128 | scrollTo(index); | ||||
| 1129 | } else if (currentIndex() != index) { | ||||
| 1130 | setCurrentIndex(index); | ||||
| 1131 | } | ||||
| 1132 | } | ||||
| 1133 | | ||||
| 1134 | /* | ||||
| 1135 | * private object implementation | ||||
| 1136 | */ | ||||
| 1137 | KColumnView::Private::Private(KColumnView *parent) | ||||
| 1138 | : q(parent) | ||||
| 1139 | ,showResizeGrips(true) | ||||
| 1140 | ,offset(0) | ||||
| 1141 | ,previewWidget(0) | ||||
| 1142 | ,previewColumn(0) | ||||
| 1143 | { | ||||
| 1144 | } | ||||
| 1145 | | ||||
| 1146 | KColumnView::Private::~Private() | ||||
| 1147 | { | ||||
| 1148 | } | ||||
| 1149 | | ||||
| 1150 | /*! | ||||
| 1151 | \internal | ||||
| 1152 | | ||||
| 1153 | */ | ||||
| 1154 | void KColumnView::Private::_q_columnsInserted(const QModelIndex &parent, int start, int end) | ||||
| 1155 | { | ||||
| 1156 | Q_UNUSED(start); | ||||
| 1157 | Q_UNUSED(end); | ||||
| 1158 | | ||||
| 1159 | checkColumnCreation(parent); | ||||
| 1160 | } | ||||
| 1161 | | ||||
| 1162 | void KColumnView::Private::_q_iconSizeChanged(QSize size) | ||||
| 1163 | { | ||||
| 1164 | for (auto column : columns) | ||||
| 1165 | column->setIconSize(size); | ||||
| 1166 | } | ||||
| 1167 | | ||||
| 1168 | /*! | ||||
| 1169 | \internal | ||||
| 1170 | | ||||
| 1171 | Makes sure we create a corresponding column as a result of changing the model. | ||||
| 1172 | | ||||
| 1173 | */ | ||||
| 1174 | void KColumnView::Private::checkColumnCreation(const QModelIndex &parent) | ||||
| 1175 | { | ||||
| 1176 | if (parent == q->currentIndex() && q->model()->hasChildren(parent)) { | ||||
| 1177 | //the parent has children and is the current | ||||
| 1178 | //let's try to find out if there is already a mapping that is good | ||||
| 1179 | for (int i = 0; i < columns.count(); ++i) { | ||||
| 1180 | QAbstractItemView *view = columns.at(i); | ||||
| 1181 | if (view->rootIndex() == parent) { | ||||
| 1182 | if (view == previewColumn) { | ||||
| 1183 | //let's recreate the parent | ||||
| 1184 | closeColumns(parent, false); | ||||
| 1185 | createColumn(parent); | ||||
| 1186 | } | ||||
| 1187 | break; | ||||
| 1188 | } | ||||
| 1189 | } | ||||
| 1190 | } | ||||
| 1191 | } | ||||
| 1192 | | ||||
| 1193 | /*! | ||||
| 1194 | \internal | ||||
| 1195 | Place all of the columns where they belong inside of the viewport, resize as necessary. | ||||
| 1196 | */ | ||||
| 1197 | void KColumnView::Private::doLayout() | ||||
| 1198 | { | ||||
| 1199 | if (!q->model() || columns.isEmpty()) | ||||
| 1200 | return; | ||||
| 1201 | | ||||
| 1202 | auto viewport = q->viewport(); | ||||
| 1203 | int viewportHeight = viewport->height(); | ||||
| 1204 | int x = columns.at(0)->x(); | ||||
| 1205 | | ||||
| 1206 | if (q->isRightToLeft()) { | ||||
| 1207 | x = viewport->width() + q->horizontalOffset(); | ||||
| 1208 | for (int i = 0; i < columns.size(); ++i) { | ||||
| 1209 | QAbstractItemView *view = columns.at(i); | ||||
| 1210 | x -= view->width(); | ||||
| 1211 | if (x != view->x() || viewportHeight != view->height()) | ||||
| 1212 | view->setGeometry(x, 0, view->width(), viewportHeight); | ||||
| 1213 | } | ||||
| 1214 | } else { | ||||
| 1215 | for (int i = 0; i < columns.size(); ++i) { | ||||
| 1216 | QAbstractItemView *view = columns.at(i); | ||||
| 1217 | int currentColumnWidth = view->width(); | ||||
| 1218 | if (x != view->x() || viewportHeight != view->height()) | ||||
| 1219 | view->setGeometry(x, 0, currentColumnWidth, viewportHeight); | ||||
| 1220 | x += currentColumnWidth; | ||||
| 1221 | } | ||||
| 1222 | } | ||||
| 1223 | } | ||||
| 1224 | | ||||
| 1225 | /*! | ||||
| 1226 | \internal | ||||
| 1227 | | ||||
| 1228 | Draws a delegate with a > if an object has children. | ||||
| 1229 | | ||||
| 1230 | \sa {Model/View Programming}, QItemDelegate | ||||
| 1231 | */ | ||||
| 1232 | void KColumnViewDelegate::paint(QPainter *painter, | ||||
| 1233 | const QStyleOptionViewItem &option, | ||||
| 1234 | const QModelIndex &index) const | ||||
| 1235 | { | ||||
| 1236 | drawBackground(painter, option, index ); | ||||
| 1237 | | ||||
| 1238 | bool reverse = (option.direction == Qt::RightToLeft); | ||||
| 1239 | int width = ((option.rect.height() * 2) / 3); | ||||
| 1240 | // Modify the options to give us room to add an arrow | ||||
| 1241 | QStyleOptionViewItem opt = option; | ||||
| 1242 | if (reverse) | ||||
| 1243 | opt.rect.adjust(width,0,0,0); | ||||
| 1244 | else | ||||
| 1245 | opt.rect.adjust(0,0,-width,0); | ||||
| 1246 | | ||||
| 1247 | if (!(index.model()->flags(index) & Qt::ItemIsEnabled)) { | ||||
| 1248 | opt.showDecorationSelected = true; | ||||
| 1249 | opt.state |= QStyle::State_Selected; | ||||
| 1250 | } | ||||
| 1251 | | ||||
| 1252 | QItemDelegate::paint(painter, opt, index); | ||||
| 1253 | | ||||
| 1254 | if (reverse) | ||||
| 1255 | opt.rect = QRect(option.rect.x(), option.rect.y(), width, option.rect.height()); | ||||
| 1256 | else | ||||
| 1257 | opt.rect = QRect(option.rect.x() + option.rect.width() - width, option.rect.y(), | ||||
| 1258 | width, option.rect.height()); | ||||
| 1259 | | ||||
| 1260 | // Draw > | ||||
| 1261 | if (index.model()->hasChildren(index)) { | ||||
| 1262 | const QWidget *view = opt.widget; | ||||
| 1263 | QStyle *style = view ? view->style() : QApplication::style(); | ||||
| 1264 | style->drawPrimitive(QStyle::PE_IndicatorColumnViewArrow, &opt, painter, view); | ||||
| 1265 | } | ||||
| 1266 | } | ||||
| 1267 | | ||||
| 1268 | #include "moc_kcolumnview.cpp" | ||||