1 | /* Copyright (C) 2008 e_k (e_k@users.sourceforge.net) |
---|
2 | |
---|
3 | This library is free software; you can redistribute it and/or |
---|
4 | modify it under the terms of the GNU Library General Public |
---|
5 | License as published by the Free Software Foundation; either |
---|
6 | version 2 of the License, or (at your option) any later version. |
---|
7 | |
---|
8 | This library is distributed in the hope that it will be useful, |
---|
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | Library General Public License for more details. |
---|
12 | |
---|
13 | You should have received a copy of the GNU Library General Public License |
---|
14 | along with this library; see the file COPYING.LIB. If not, write to |
---|
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
16 | Boston, MA 02110-1301, USA. |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | #include "qtermwidget.h" |
---|
21 | |
---|
22 | #include "Session.h" |
---|
23 | #include "TerminalDisplay.h" |
---|
24 | |
---|
25 | |
---|
26 | using namespace Konsole; |
---|
27 | |
---|
28 | void *createTermWidget(int startnow, void *parent) |
---|
29 | { |
---|
30 | return (void*) new QTermWidget(startnow, (QWidget*)parent); |
---|
31 | } |
---|
32 | |
---|
33 | struct TermWidgetImpl |
---|
34 | { |
---|
35 | TermWidgetImpl(QWidget* parent = 0); |
---|
36 | |
---|
37 | TerminalDisplay *m_terminalDisplay; |
---|
38 | Session *m_session; |
---|
39 | |
---|
40 | Session* createSession(); |
---|
41 | TerminalDisplay* createTerminalDisplay(Session *session, QWidget* parent); |
---|
42 | }; |
---|
43 | |
---|
44 | TermWidgetImpl::TermWidgetImpl(QWidget* parent) |
---|
45 | { |
---|
46 | this->m_session = createSession(); |
---|
47 | this->m_terminalDisplay = createTerminalDisplay(this->m_session, parent); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | Session *TermWidgetImpl::createSession() |
---|
52 | { |
---|
53 | Session *session = new Session(); |
---|
54 | |
---|
55 | session->setTitle(Session::NameRole, "QTermWidget"); |
---|
56 | session->setProgram("/bin/bash"); |
---|
57 | QStringList args(""); |
---|
58 | session->setArguments(args); |
---|
59 | session->setAutoClose(true); |
---|
60 | |
---|
61 | session->setCodec(QTextCodec::codecForName("UTF-8")); |
---|
62 | |
---|
63 | session->setFlowControlEnabled(true); |
---|
64 | session->setHistoryType(HistoryTypeBuffer(1000)); |
---|
65 | |
---|
66 | session->setDarkBackground(true); |
---|
67 | |
---|
68 | session->setKeyBindings(""); |
---|
69 | return session; |
---|
70 | } |
---|
71 | |
---|
72 | TerminalDisplay *TermWidgetImpl::createTerminalDisplay(Session *session, QWidget* parent) |
---|
73 | { |
---|
74 | // TerminalDisplay* display = new TerminalDisplay(this); |
---|
75 | TerminalDisplay* display = new TerminalDisplay(parent); |
---|
76 | |
---|
77 | display->setBellMode(TerminalDisplay::NotifyBell); |
---|
78 | display->setTerminalSizeHint(true); |
---|
79 | display->setTripleClickMode(TerminalDisplay::SelectWholeLine); |
---|
80 | display->setTerminalSizeStartup(true); |
---|
81 | |
---|
82 | display->setRandomSeed(session->sessionId() * 31); |
---|
83 | |
---|
84 | return display; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | QTermWidget::QTermWidget(int startnow, QWidget *parent) |
---|
89 | :QWidget(parent) |
---|
90 | { |
---|
91 | m_impl = new TermWidgetImpl(this); |
---|
92 | |
---|
93 | init(); |
---|
94 | |
---|
95 | if (startnow && m_impl->m_session) { |
---|
96 | m_impl->m_session->run(); |
---|
97 | } |
---|
98 | |
---|
99 | this->setFocus( Qt::OtherFocusReason ); |
---|
100 | m_impl->m_terminalDisplay->resize(this->size()); |
---|
101 | |
---|
102 | this->setFocusProxy(m_impl->m_terminalDisplay); |
---|
103 | } |
---|
104 | |
---|
105 | void QTermWidget::startShellProgram() |
---|
106 | { |
---|
107 | if ( m_impl->m_session->isRunning() ) |
---|
108 | return; |
---|
109 | |
---|
110 | m_impl->m_session->run(); |
---|
111 | } |
---|
112 | |
---|
113 | void QTermWidget::init() |
---|
114 | { |
---|
115 | m_impl->m_terminalDisplay->setSize(80, 40); |
---|
116 | |
---|
117 | QFont font = QApplication::font(); |
---|
118 | font.setFamily("Monospace"); |
---|
119 | font.setPointSize(10); |
---|
120 | font.setStyleHint(QFont::TypeWriter); |
---|
121 | setTerminalFont(font); |
---|
122 | setScrollBarPosition(NoScrollBar); |
---|
123 | |
---|
124 | m_impl->m_session->addView(m_impl->m_terminalDisplay); |
---|
125 | |
---|
126 | connect(m_impl->m_session, SIGNAL(finished()), this, SLOT(sessionFinished())); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | QTermWidget::~QTermWidget() |
---|
131 | { |
---|
132 | emit destroyed(); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void QTermWidget::setTerminalFont(QFont &font) |
---|
137 | { |
---|
138 | if (!m_impl->m_terminalDisplay) |
---|
139 | return; |
---|
140 | m_impl->m_terminalDisplay->setVTFont(font); |
---|
141 | } |
---|
142 | |
---|
143 | void QTermWidget::setShellProgram(QString &progname) |
---|
144 | { |
---|
145 | if (!m_impl->m_session) |
---|
146 | return; |
---|
147 | m_impl->m_session->setProgram(progname); |
---|
148 | } |
---|
149 | |
---|
150 | void QTermWidget::setArgs(QStringList &args) |
---|
151 | { |
---|
152 | if (!m_impl->m_session) |
---|
153 | return; |
---|
154 | m_impl->m_session->setArguments(args); |
---|
155 | } |
---|
156 | |
---|
157 | void QTermWidget::setTextCodec(QTextCodec *codec) |
---|
158 | { |
---|
159 | if (!m_impl->m_session) |
---|
160 | return; |
---|
161 | m_impl->m_session->setCodec(codec); |
---|
162 | } |
---|
163 | |
---|
164 | void QTermWidget::setColorScheme(int scheme) |
---|
165 | { |
---|
166 | switch(scheme) { |
---|
167 | case COLOR_SCHEME_WHITE_ON_BLACK: |
---|
168 | m_impl->m_terminalDisplay->setColorTable(whiteonblack_color_table); |
---|
169 | break; |
---|
170 | case COLOR_SCHEME_GREEN_ON_BLACK: |
---|
171 | m_impl->m_terminalDisplay->setColorTable(greenonblack_color_table); |
---|
172 | break; |
---|
173 | case COLOR_SCHEME_BLACK_ON_LIGHT_YELLOW: |
---|
174 | m_impl->m_terminalDisplay->setColorTable(blackonlightyellow_color_table); |
---|
175 | break; |
---|
176 | default: //do nothing |
---|
177 | break; |
---|
178 | }; |
---|
179 | } |
---|
180 | |
---|
181 | void QTermWidget::setSize(int h, int v) |
---|
182 | { |
---|
183 | if (!m_impl->m_terminalDisplay) |
---|
184 | return; |
---|
185 | m_impl->m_terminalDisplay->setSize(h, v); |
---|
186 | } |
---|
187 | |
---|
188 | void QTermWidget::setHistorySize(int lines) |
---|
189 | { |
---|
190 | if (lines < 0) |
---|
191 | m_impl->m_session->setHistoryType(HistoryTypeFile()); |
---|
192 | else |
---|
193 | m_impl->m_session->setHistoryType(HistoryTypeBuffer(lines)); |
---|
194 | } |
---|
195 | |
---|
196 | void QTermWidget::setScrollBarPosition(ScrollBarPosition pos) |
---|
197 | { |
---|
198 | if (!m_impl->m_terminalDisplay) |
---|
199 | return; |
---|
200 | m_impl->m_terminalDisplay->setScrollBarPosition((TerminalDisplay::ScrollBarPosition)pos); |
---|
201 | } |
---|
202 | |
---|
203 | void QTermWidget::sendText(QString &text) |
---|
204 | { |
---|
205 | m_impl->m_session->sendText(text); |
---|
206 | } |
---|
207 | |
---|
208 | void QTermWidget::resizeEvent(QResizeEvent*) |
---|
209 | { |
---|
210 | //qDebug("global window resizing...with %d %d", this->size().width(), this->size().height()); |
---|
211 | m_impl->m_terminalDisplay->resize(this->size()); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | void QTermWidget::sessionFinished() |
---|
217 | { |
---|
218 | emit finished(); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | //#include "moc_consoleq.cpp" |
---|
223 | |
---|