scide: refactoring - move LineIndicator to separate source file
[supercollider.git] / editors / sc-ide / widgets / goto_line_tool.hpp
blob9f5d5f22c35d2e35900b579804def4898e01828a
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_WIDGETS_GOTO_LINE_TOOL_HPP_INCLUDED
22 #define SCIDE_WIDGETS_GOTO_LINE_TOOL_HPP_INCLUDED
24 #include "code_editor/editor.hpp"
25 #include "../core/doc_manager.hpp"
27 #include <QWidget>
28 #include <QSpinBox>
29 #include <QToolButton>
30 #include <QHBoxLayout>
31 #include <QKeyEvent>
32 #include <QPointer>
33 #include <QLabel>
35 namespace ScIDE {
37 class GoToLineTool : public QWidget
39 Q_OBJECT
41 public:
42 GoToLineTool( QWidget * parent = 0 ):
43 QWidget(parent),
44 mEditor(0)
46 mSpinBox = new QSpinBox;
47 mSpinBox->setMinimum(0);
48 mSpinBox->setMaximum(0);
50 QToolButton *goBtn = new QToolButton;
51 goBtn->setText(tr("Go"));
52 goBtn->setAutoRaise(true);
54 QLabel *label = new QLabel("Line:");
56 QHBoxLayout *layout = new QHBoxLayout;
57 layout->setContentsMargins(0,0,0,0);
58 layout->addWidget(label);
59 layout->addWidget(mSpinBox);
60 layout->addWidget(goBtn);
61 layout->addStretch();
63 setLayout(layout);
65 setFocusProxy(mSpinBox);
67 connect(goBtn, SIGNAL(clicked()), this, SLOT(go()));
68 connect(mSpinBox, SIGNAL(editingFinished()), this, SLOT(onEditingFinished()));
71 void setEditor( CodeEditor *editor )
73 if (mEditor)
74 mEditor->disconnect( this, SLOT(setMaximum(int)) );
76 mEditor = editor;
78 if (mEditor) {
79 connect(mEditor, SIGNAL(blockCountChanged(int)), this, SLOT(setMaximum(int)));
80 setMaximum(mEditor->blockCount());
82 else
83 setMaximum(0);
86 void setValue( int value ) { mSpinBox->setValue(value); }
88 public slots:
90 void setMaximum( int max ) {
91 if (max > 0)
92 mSpinBox->setRange(1, max);
93 else
94 mSpinBox->setRange(0,0);
97 void setFocus()
99 mSpinBox->setFocus(Qt::OtherFocusReason);
100 mSpinBox->selectAll();
103 signals:
105 void activated( int lineNumber );
107 private slots:
109 void go()
111 int lineNumber = mSpinBox->value();
113 if (mEditor) {
114 QTextDocument *doc = mEditor->textDocument();
115 QTextBlock block( doc->findBlockByNumber(lineNumber - 1) );
116 if (!block.isValid())
117 return;
119 QTextCursor cursor( doc );
120 cursor.setPosition( block.position() );
121 mEditor->setTextCursor(cursor);
124 emit activated( lineNumber );
127 void onEditingFinished()
129 if (mSpinBox->hasFocus())
130 go();
133 private:
135 QSpinBox *mSpinBox;
136 QPointer<CodeEditor> mEditor;
139 } // namespace ScIDE
141 #endif