1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 3 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15
16 #include "qgotolinepanel.h"
17
18 /*!
19 \file qgotolinepanel.cpp
20 \brief Implementation of the QGotoLinePanel class.
21
22 \see QGotoLinePanel
23 */
24
25 #include "qeditor.h"
26
27 #include "qdocument.h"
28 #include "qdocumentline.h"
29 #include "qdocumentcursor.h"
30
31 #include <QKeyEvent>
32
33 /*!
34 \ingroup widgets
35 @{
36 */
37
38 /*!
39 \class QGotoLinePanel
40 \brief A panel that provide inline goto line functionalities
41 */
42
QCE_AUTO_REGISTER(QGotoLinePanel)43 QCE_AUTO_REGISTER(QGotoLinePanel)
44
45 /*!
46 \brief Constructor
47 */
48 QGotoLinePanel::QGotoLinePanel(QWidget *p)
49 : QPanel(p)
50 {
51 setupUi(this);
52 setDefaultVisibility(false);
53
54 bClose->setIcon(QPixmap(":/closeall.png"));
55 }
56
57 /*!
58 \brief Empty destructor
59 */
~QGotoLinePanel()60 QGotoLinePanel::~QGotoLinePanel()
61 {
62
63 }
64
65 /*!
66
67 */
type() const68 QString QGotoLinePanel::type() const
69 {
70 return "Goto";
71 }
72
73 /*!
74 \brief
75 */
editorChange(QEditor * e)76 void QGotoLinePanel::editorChange(QEditor *e)
77 {
78 if ( editor() )
79 {
80 disconnect( editor(), SIGNAL( cursorPositionChanged() ),
81 this , SLOT ( cursorPositionChanged() ) );
82
83 disconnect( editor()->document(), SIGNAL( lineCountChanged(int) ),
84 this , SLOT ( lineCountChanged(int) ) );
85 }
86
87 if ( e )
88 {
89 connect(e , SIGNAL( cursorPositionChanged() ),
90 this, SLOT ( cursorPositionChanged() ) );
91
92 connect(e->document() , SIGNAL( lineCountChanged(int) ),
93 this , SLOT ( lineCountChanged(int) ) );
94
95 lineCountChanged(e->document()->lineCount());
96 spLine->setValue(e->cursor().lineNumber() + 1);
97 slLine->setValue(e->cursor().lineNumber() + 1);
98 }
99 }
100
forward(QMouseEvent * e)101 bool QGotoLinePanel::forward(QMouseEvent *e)
102 {
103 Q_UNUSED(e)
104
105 /*
106 This panel does not need mouse events to be forwarded to the editor.
107 Even more, it requires them not to be forwarded...
108 */
109 return false;
110 }
111
showEvent(QShowEvent * e)112 void QGotoLinePanel::showEvent(QShowEvent *e)
113 {
114 Q_UNUSED(e)
115
116 spLine->setFocus();
117 spLine->selectAll();
118 }
119
keyPressEvent(QKeyEvent * e)120 void QGotoLinePanel::keyPressEvent(QKeyEvent *e)
121 {
122 if ( e->key() == Qt::Key_Escape )
123 {
124 on_bClose_clicked();
125 } else if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) {
126 on_bGo_clicked();
127 } else {
128 QPanel::keyPressEvent(e);
129 }
130 }
131
on_bClose_clicked()132 void QGotoLinePanel::on_bClose_clicked()
133 {
134 hide();
135 editor()->setFocus();
136 }
137
on_bGo_clicked()138 void QGotoLinePanel::on_bGo_clicked()
139 {
140 editor()->setCursor(QDocumentCursor(editor()->document(), spLine->value() - 1));
141 }
142
on_spLine_valueChanged(int v)143 void QGotoLinePanel::on_spLine_valueChanged(int v)
144 {
145 if ( slLine->value() != v )
146 slLine->setValue(v);
147 }
148
on_slLine_valueChanged(int v)149 void QGotoLinePanel::on_slLine_valueChanged(int v)
150 {
151 if ( spLine->value() != v )
152 spLine->setValue(v);
153 }
154
lineCountChanged(int n)155 void QGotoLinePanel::lineCountChanged(int n)
156 {
157 spLine->setMaximum(n);
158 slLine->setMaximum(n);
159 }
160
cursorPositionChanged()161 void QGotoLinePanel::cursorPositionChanged()
162 {
163 spLine->setValue(editor()->cursor().lineNumber() + 1);
164 slLine->setValue(editor()->cursor().lineNumber() + 1);
165 }
166
167 /*! @} */
168