File indexing completed on 2024-05-12 04:58:30
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2016-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 0019 #include "webscrollbar.h" 0020 #include "webview.h" 0021 #include "webpage.h" 0022 0023 #include <QPaintEvent> 0024 #include <QPainter> 0025 0026 WebScrollBar::WebScrollBar(Qt::Orientation orientation, WebView *view) 0027 : QScrollBar(orientation) 0028 , m_view(view) 0029 { 0030 setFocusProxy(m_view); 0031 resize(sizeHint()); 0032 0033 connect(this, &QScrollBar::valueChanged, this, &WebScrollBar::performScroll); 0034 connect(view, &WebView::focusChanged, this, [this]() { update(); }); 0035 } 0036 0037 int WebScrollBar::thickness() const 0038 { 0039 return orientation() == Qt::Vertical ? width() : height(); 0040 } 0041 0042 void WebScrollBar::updateValues(const QSize &viewport) 0043 { 0044 setMinimum(0); 0045 setParent(m_view->overlayWidget()); 0046 0047 int newValue; 0048 0049 m_blockScrolling = true; 0050 0051 if (orientation() == Qt::Vertical) { 0052 setFixedHeight(m_view->height() - (m_view->height() - viewport.height()) * devicePixelRatioF()); 0053 move(m_view->width() - width(), 0); 0054 setPageStep(viewport.height()); 0055 setMaximum(qMax(0, m_view->page()->contentsSize().toSize().height() - viewport.height())); 0056 newValue = m_view->page()->scrollPosition().toPoint().y(); 0057 } else { 0058 setFixedWidth(m_view->width() - (m_view->width() - viewport.width()) * devicePixelRatioF()); 0059 move(0, m_view->height() - height()); 0060 setPageStep(viewport.width()); 0061 setMaximum(qMax(0, m_view->page()->contentsSize().toSize().width() - viewport.width())); 0062 newValue = m_view->page()->scrollPosition().toPoint().x(); 0063 } 0064 0065 if (!isSliderDown()) { 0066 setValue(newValue); 0067 } 0068 0069 m_blockScrolling = false; 0070 } 0071 0072 void WebScrollBar::performScroll() 0073 { 0074 if (m_blockScrolling) { 0075 return; 0076 } 0077 0078 QPointF pos = m_view->page()->scrollPosition(); 0079 0080 if (orientation() == Qt::Vertical) { 0081 pos.setY(value()); 0082 } else { 0083 pos.setX(value()); 0084 } 0085 0086 m_view->page()->setScrollPosition(pos); 0087 } 0088 0089 void WebScrollBar::paintEvent(QPaintEvent *ev) 0090 { 0091 QPainter painter(this); 0092 painter.fillRect(ev->rect(), palette().window()); 0093 QScrollBar::paintEvent(ev); 0094 }