1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "selectrangeview.hpp"
10 
11 // tool
12 #include "selectrangetool.hpp"
13 // Okteta Kasten gui
14 #include <Kasten/Okteta/AddressComboBox>
15 // KF
16 #include <KGuiItem>
17 #include <KLocalizedString>
18 // Qt
19 #include <QPushButton>
20 #include <QCheckBox>
21 #include <QLabel>
22 #include <QLayout>
23 
24 namespace Kasten {
25 
SelectRangeView(SelectRangeTool * tool,QWidget * parent)26 SelectRangeView::SelectRangeView(SelectRangeTool* tool, QWidget* parent)
27     : AbstractToolWidget(parent)
28     , mTool(tool)
29 {
30     auto* baseLayout = new QHBoxLayout(this);
31     baseLayout->setContentsMargins(0, 0, 0, 0);
32 
33     // offsets
34     auto* offsetLayout = new QVBoxLayout();
35     offsetLayout->setContentsMargins(0, 0, 0, 0);
36 
37     // start offset
38     auto* startOffsetLayout = new QHBoxLayout();
39     startOffsetLayout->setContentsMargins(0, 0, 0, 0);
40 
41     QLabel* label = new QLabel(i18nc("@label:listbox", "Start offset:"), this);
42     mStartEdit = new Okteta::AddressComboBox(this);
43     connect(mStartEdit, &Okteta::AddressComboBox::addressChanged,
44             mTool, &SelectRangeTool::setTargetStart);
45     label->setBuddy(mStartEdit);
46     const QString startInputWhatsThis =
47         i18nc("@info:whatsthis", "Enter an offset to go to, or select a previous offset from the list.");
48     label->setWhatsThis(startInputWhatsThis);
49     mStartEdit->setWhatsThis(startInputWhatsThis);
50 
51     startOffsetLayout->addWidget(label);
52     startOffsetLayout->addWidget(mStartEdit);
53     setFocusProxy(mStartEdit);   // TODO: see how KDialog does it, e.g. see if there is already a focuswidget as child
54 
55     offsetLayout->addLayout(startOffsetLayout);
56 
57     // end offset
58     auto* endOffsetLayout = new QHBoxLayout();
59     endOffsetLayout->setContentsMargins(0, 0, 0, 0);
60 
61     label = new QLabel(i18nc("@label:listbox", "End offset:"), this);
62     mEndEdit = new Okteta::AddressComboBox(this);
63     connect(mEndEdit, &Okteta::AddressComboBox::addressChanged,
64             mTool, &SelectRangeTool::setTargetEnd);
65     label->setBuddy(mEndEdit);
66     const QString endInputWhatsThis =
67         i18nc("@info:whatsthis", "Enter an offset to go to, or select a previous offset from the list.");
68     label->setWhatsThis(endInputWhatsThis);
69     mEndEdit->setWhatsThis(endInputWhatsThis);
70 
71     endOffsetLayout->addWidget(label);
72     endOffsetLayout->addWidget(mEndEdit);
73 
74     offsetLayout->addLayout(endOffsetLayout);
75     baseLayout->addLayout(offsetLayout);
76 
77     // options
78     auto* optionsLayout = new QVBoxLayout();
79     optionsLayout->setContentsMargins(0, 0, 0, 0);
80 
81     mRelativeCheckBox = new QCheckBox(i18nc("@option:check", "End relative"), this);
82     mRelativeCheckBox->setWhatsThis(
83         i18nc("@info:whatsthis", "Extend the selection by the cursor move."));
84     connect(mRelativeCheckBox, &QCheckBox::toggled,
85             mTool, &SelectRangeTool::setIsEndRelative);
86     mRelativeCheckBox->setChecked(mTool->isEndRelative());
87     mBackwardsCheckBox = new QCheckBox(i18nc("@option:check", "&Backwards"), this);
88     mBackwardsCheckBox->setWhatsThis(
89         i18nc("@info:whatsthis", "Go backwards from the end or the current cursor location."));
90     connect(mBackwardsCheckBox, &QCheckBox::toggled,
91             mTool, &SelectRangeTool::setIsEndBackwards);
92     mBackwardsCheckBox->setChecked(mTool->isEndBackwards());
93 
94     connect(mRelativeCheckBox, &QCheckBox::toggled, mBackwardsCheckBox, &QCheckBox::setEnabled);
95     mBackwardsCheckBox->setEnabled(mRelativeCheckBox->isChecked());
96 
97     optionsLayout->addWidget(mRelativeCheckBox);
98     optionsLayout->addWidget(mBackwardsCheckBox);
99 
100     baseLayout->addLayout(optionsLayout);
101 
102     // Select button
103     const KGuiItem selectGuiItem =
104         KGuiItem(i18nc("@action:button",
105                        "&Select"),
106                  QString(),
107                  i18nc("@info:tooltip",
108                        "Select the range."),
109                  xi18nc("@info:whatsthis",
110                         "If you press the <interface>Select</interface> "
111                         "button, the cursor will be moved in the document to or, "
112                         "on your option, by the offset you entered above."));
113     mSelectButton = new QPushButton(this);
114     KGuiItem::assign(mSelectButton, selectGuiItem);
115     connect(mSelectButton, &QPushButton::clicked, this, &SelectRangeView::onSelectButtonClicked);
116     addButton(mSelectButton, AbstractToolWidget::Default);
117     baseLayout->addWidget(mSelectButton);
118     baseLayout->setAlignment(mSelectButton, Qt::AlignTop);
119 
120     baseLayout->addStretch();
121 
122     setTabOrder(mStartEdit, mEndEdit);
123     setTabOrder(mEndEdit, mRelativeCheckBox);
124     setTabOrder(mRelativeCheckBox, mBackwardsCheckBox);
125     setTabOrder(mBackwardsCheckBox, mSelectButton);
126 
127     connect(mTool, &SelectRangeTool::isApplyableChanged, this, &SelectRangeView::onApplyableChanged);
128 
129     onApplyableChanged(mTool->isApplyable());
130 }
131 
132 SelectRangeView::~SelectRangeView() = default;
133 
onApplyableChanged(bool isApplyable)134 void SelectRangeView::onApplyableChanged(bool isApplyable)
135 {
136     // TODO: set error tooltip, like offset out of range or no document
137     // TODO: set color flag to offset input
138     mSelectButton->setEnabled(isApplyable);
139 }
140 
onSelectButtonClicked()141 void SelectRangeView::onSelectButtonClicked()
142 {
143     // TODO: collect recently used offsets in tool instead?
144     mStartEdit->rememberCurrentAddress();
145     mEndEdit->rememberCurrentAddress();
146 
147     mTool->select();
148 //     emit toolUsed();
149 }
150 
151 }
152