Changeset View
Changeset View
Standalone View
Standalone View
src/TerminalScrollBar.cpp
- This file was added.
1 | #include "TerminalScrollBar.h" | ||||
---|---|---|---|---|---|
2 | #include <QDebug> | ||||
3 | #include <QStyle> | ||||
4 | #include <QApplication> | ||||
5 | | ||||
6 | namespace Konsole { | ||||
7 | | ||||
8 | TerminalScrollBar::TerminalScrollBar(QWidget *parent) | ||||
9 | : QScrollBar(parent) | ||||
10 | , _position(Right) | ||||
11 | , _canFloat(false) | ||||
12 | { | ||||
13 | _canFloat = qApp->style()->objectName() == QLatin1Literal("breeze"); | ||||
14 | setCursor(Qt::ArrowCursor); | ||||
15 | } | ||||
16 | | ||||
17 | void TerminalScrollBar::setLines(int lines) | ||||
18 | { | ||||
19 | if (_lines == lines) { | ||||
20 | return; | ||||
21 | } | ||||
22 | _lines = lines; | ||||
23 | emit linesChanged(lines); | ||||
24 | } | ||||
25 | | ||||
26 | void TerminalScrollBar::changeEvent(QEvent *event) | ||||
27 | { | ||||
28 | if (event->type() == QEvent::StyleChange) { | ||||
29 | _canFloat = qApp->style()->objectName() == QLatin1Literal("breeze"); | ||||
30 | parentWidget()->update(); | ||||
31 | } | ||||
32 | } | ||||
33 | | ||||
34 | void TerminalScrollBar::setPosition(Position position) | ||||
35 | { | ||||
36 | if (_position == position) { | ||||
37 | return; | ||||
38 | } | ||||
39 | | ||||
40 | setVisible(position != Position::Hidden || maximum() != 0); | ||||
41 | _position = position; | ||||
42 | emit positionChanged(position); | ||||
43 | } | ||||
44 | | ||||
45 | void TerminalScrollBar::fixGeometry() | ||||
46 | { | ||||
47 | auto parentRect = parentWidget()->contentsRect(); | ||||
48 | resize(sizeHint().width(), parentRect.height()); | ||||
49 | | ||||
50 | switch (_position) { | ||||
51 | case Hidden : break; | ||||
52 | case Left : | ||||
53 | move(parentRect.topLeft()); | ||||
54 | break; | ||||
55 | case Right: | ||||
56 | move(parentRect.topRight() - QPoint(width() - 1, 0)); | ||||
57 | break; | ||||
58 | } | ||||
59 | } | ||||
60 | | ||||
61 | void TerminalScrollBar::setScroll(int cursor, int slines) | ||||
62 | { | ||||
63 | if (maximum() == 0 && value() == cursor) { | ||||
64 | // hide the scrollbar if it's not needed | ||||
65 | hide(); | ||||
66 | return; | ||||
67 | } else if (_position != Position::Hidden) { | ||||
68 | show(); | ||||
69 | } | ||||
70 | | ||||
71 | setRange(0, slines - _lines); | ||||
72 | setSingleStep(1); | ||||
73 | setPageStep(_lines); | ||||
74 | setValue(cursor); | ||||
75 | } | ||||
76 | | ||||
77 | int TerminalScrollBar::themeAwareWidth() const | ||||
78 | { | ||||
79 | return _canFloat ? 0 : width(); | ||||
80 | } | ||||
81 | | ||||
82 | TerminalScrollBar::Position TerminalScrollBar::position() const | ||||
83 | { | ||||
84 | return _position; | ||||
85 | } | ||||
86 | | ||||
87 | int TerminalScrollBar::lines() const | ||||
88 | { | ||||
89 | return _lines; | ||||
90 | } | ||||
91 | | ||||
92 | } |