Changeset View
Changeset View
Standalone View
Standalone View
src/filewidgets/kcolumnviewgrip.cpp
- This file was added.
| 1 | /**************************************************************************** | ||||
|---|---|---|---|---|---|
| 2 | ** | ||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | ||||
| 4 | ** Contact: https://www.qt.io/licensing/ | ||||
| 5 | ** | ||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | ||||
| 7 | ** | ||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | ||||
| 9 | ** Commercial License Usage | ||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | ||||
| 11 | ** accordance with the commercial license agreement provided with the | ||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | ||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | ||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | ||||
| 16 | ** | ||||
| 17 | ** GNU Lesser General Public License Usage | ||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||||
| 19 | ** General Public License version 3 as published by the Free Software | ||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | ||||
| 21 | ** packaging of this file. Please review the following information to | ||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | ||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | ||||
| 24 | ** | ||||
| 25 | ** GNU General Public License Usage | ||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | ||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | ||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | ||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | ||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | ||||
| 31 | ** included in the packaging of this file. Please review the following | ||||
| 32 | ** information to ensure the GNU General Public License requirements will | ||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | ||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | ||||
| 35 | ** | ||||
| 36 | ** $QT_END_LICENSE$ | ||||
| 37 | ** | ||||
| 38 | ****************************************************************************/ | ||||
| 39 | | ||||
| 40 | #include "kcolumnviewgrip_p.h" | ||||
| 41 | #include <qstyleoption.h> | ||||
| 42 | #include <qpainter.h> | ||||
| 43 | #include <qbrush.h> | ||||
| 44 | #include <qevent.h> | ||||
| 45 | #include <qdebug.h> | ||||
| 46 | | ||||
| 47 | class KColumnViewGrip::Private | ||||
| 48 | { | ||||
| 49 | public: | ||||
| 50 | int originalXLocation = -1; | ||||
| 51 | }; | ||||
| 52 | | ||||
| 53 | /* | ||||
| 54 | \internal | ||||
| 55 | class KColumnViewGrip | ||||
| 56 | | ||||
| 57 | KColumnViewGrip is created to go inside QAbstractScrollArea's corner. | ||||
| 58 | When the mouse it moved it will resize the scroll area and emit's a signal. | ||||
| 59 | */ | ||||
| 60 | | ||||
| 61 | /* | ||||
| 62 | \internal | ||||
| 63 | \fn void KColumnViewGrip::gripMoved() | ||||
| 64 | Signal that is emitted when the grip moves the parent widget. | ||||
| 65 | */ | ||||
| 66 | | ||||
| 67 | /*! | ||||
| 68 | Creates a new KColumnViewGrip with the given \a parent to view a model. | ||||
| 69 | Use setModel() to set the model. | ||||
| 70 | */ | ||||
| 71 | KColumnViewGrip::KColumnViewGrip(QWidget *parent) | ||||
| 72 | : QWidget(parent), d(new Private) | ||||
| 73 | { | ||||
| 74 | #ifndef QT_NO_CURSOR | ||||
| 75 | setCursor(Qt::SplitHCursor); | ||||
| 76 | #endif | ||||
| 77 | } | ||||
| 78 | | ||||
| 79 | /*! | ||||
| 80 | Destroys the view. | ||||
| 81 | */ | ||||
| 82 | KColumnViewGrip::~KColumnViewGrip() | ||||
| 83 | { | ||||
| 84 | } | ||||
| 85 | | ||||
| 86 | /*! | ||||
| 87 | Attempt to resize the parent object by \a offset | ||||
| 88 | returns the amount of offset that it was actually able to resized | ||||
| 89 | */ | ||||
| 90 | int KColumnViewGrip::moveGrip(int offset) | ||||
| 91 | { | ||||
| 92 | QWidget *parentWidget = (QWidget*)parent(); | ||||
| 93 | | ||||
| 94 | // first resize the parent | ||||
| 95 | int oldWidth = parentWidget->width(); | ||||
| 96 | int newWidth = oldWidth; | ||||
| 97 | if (isRightToLeft()) | ||||
| 98 | newWidth -= offset; | ||||
| 99 | else | ||||
| 100 | newWidth += offset; | ||||
| 101 | newWidth = qMax(parentWidget->minimumWidth(), newWidth); | ||||
| 102 | parentWidget->resize(newWidth, parentWidget->height()); | ||||
| 103 | | ||||
| 104 | // Then have the view move the widget | ||||
| 105 | int realOffset = parentWidget->width() - oldWidth; | ||||
| 106 | int oldX = parentWidget->x(); | ||||
| 107 | if (realOffset != 0) | ||||
| 108 | emit gripMoved(realOffset); | ||||
| 109 | if (isRightToLeft()) | ||||
| 110 | realOffset = -1 * (oldX - parentWidget->x()); | ||||
| 111 | return realOffset; | ||||
| 112 | } | ||||
| 113 | | ||||
| 114 | /*! | ||||
| 115 | \reimp | ||||
| 116 | */ | ||||
| 117 | void KColumnViewGrip::paintEvent(QPaintEvent *event) | ||||
| 118 | { | ||||
| 119 | QPainter painter(this); | ||||
| 120 | QStyleOption opt; | ||||
| 121 | opt.initFrom(this); | ||||
| 122 | style()->drawControl(QStyle::CE_ColumnViewGrip, &opt, &painter, this); | ||||
| 123 | event->accept(); | ||||
| 124 | } | ||||
| 125 | | ||||
| 126 | /*! | ||||
| 127 | \reimp | ||||
| 128 | Resize the parent window to the sizeHint | ||||
| 129 | */ | ||||
| 130 | void KColumnViewGrip::mouseDoubleClickEvent(QMouseEvent *event) | ||||
| 131 | { | ||||
| 132 | Q_UNUSED(event); | ||||
| 133 | QWidget *parentWidget = (QWidget*)parent(); | ||||
| 134 | int offset = parentWidget->sizeHint().width() - parentWidget->width(); | ||||
| 135 | if (isRightToLeft()) | ||||
| 136 | offset *= -1; | ||||
| 137 | moveGrip(offset); | ||||
| 138 | event->accept(); | ||||
| 139 | } | ||||
| 140 | | ||||
| 141 | /*! | ||||
| 142 | \reimp | ||||
| 143 | Begin watching for mouse movements | ||||
| 144 | */ | ||||
| 145 | void KColumnViewGrip::mousePressEvent(QMouseEvent *event) | ||||
| 146 | { | ||||
| 147 | d->originalXLocation = event->globalX(); | ||||
| 148 | event->accept(); | ||||
| 149 | } | ||||
| 150 | | ||||
| 151 | /*! | ||||
| 152 | \reimp | ||||
| 153 | Calculate the movement of the grip and moveGrip() and emit gripMoved | ||||
| 154 | */ | ||||
| 155 | void KColumnViewGrip::mouseMoveEvent(QMouseEvent *event) | ||||
| 156 | { | ||||
| 157 | int offset = event->globalX() - d->originalXLocation; | ||||
| 158 | d->originalXLocation = moveGrip(offset) + d->originalXLocation; | ||||
| 159 | event->accept(); | ||||
| 160 | } | ||||
| 161 | | ||||
| 162 | /*! | ||||
| 163 | \reimp | ||||
| 164 | Stop watching for mouse movements | ||||
| 165 | */ | ||||
| 166 | void KColumnViewGrip::mouseReleaseEvent(QMouseEvent *event) | ||||
| 167 | { | ||||
| 168 | d->originalXLocation = -1; | ||||
| 169 | event->accept(); | ||||
| 170 | } | ||||
| 171 | | ||||
| 172 | #include "moc_kcolumnviewgrip_p.cpp" | ||||