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 | #include "qtermwidget.moc" |
---|
22 | |
---|
23 | #include "Session.h" |
---|
24 | #include "TerminalDisplay.h" |
---|
25 | |
---|
26 | |
---|
27 | using namespace Konsole; |
---|
28 | |
---|
29 | void *createTermWidget(int startnow, void *parent) |
---|
30 | { |
---|
31 | return (void*) new QTermWidget(startnow, (QWidget*)parent); |
---|
32 | } |
---|
33 | |
---|
34 | struct TermWidgetImpl |
---|
35 | { |
---|
36 | TermWidgetImpl(QWidget* parent = 0); |
---|
37 | |
---|
38 | TerminalDisplay *m_terminalDisplay; |
---|
39 | Session *m_session; |
---|
40 | |
---|
41 | Session* createSession(); |
---|
42 | TerminalDisplay* createTerminalDisplay(Session *session, QWidget* parent); |
---|
43 | }; |
---|
44 | |
---|
45 | TermWidgetImpl::TermWidgetImpl(QWidget* parent) |
---|
46 | { |
---|
47 | this->m_session = createSession(); |
---|
48 | this->m_terminalDisplay = createTerminalDisplay(this->m_session, parent); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | Session *TermWidgetImpl::createSession() |
---|
53 | { |
---|
54 | Session *session = new Session(); |
---|
55 | |
---|
56 | session->setTitle(Session::NameRole, "QTermWidget"); |
---|
57 | session->setProgram("/bin/bash"); |
---|
58 | QStringList args(""); |
---|
59 | session->setArguments(args); |
---|
60 | session->setAutoClose(true); |
---|
61 | |
---|
62 | session->setCodec(QTextCodec::codecForName("UTF-8")); |
---|
63 | |
---|
64 | session->setFlowControlEnabled(true); |
---|
65 | session->setHistoryType(HistoryTypeBuffer(1000)); |
---|
66 | |
---|
67 | session->setDarkBackground(true); |
---|
68 | |
---|
69 | session->setKeyBindings(""); |
---|
70 | return session; |
---|
71 | } |
---|
72 | |
---|
73 | TerminalDisplay *TermWidgetImpl::createTerminalDisplay(Session *session, QWidget* parent) |
---|
74 | { |
---|
75 | // TerminalDisplay* display = new TerminalDisplay(this); |
---|
76 | TerminalDisplay* display = new TerminalDisplay(parent); |
---|
77 | |
---|
78 | display->setBellMode(TerminalDisplay::NotifyBell); |
---|
79 | display->setTerminalSizeHint(true); |
---|
80 | display->setTripleClickMode(TerminalDisplay::SelectWholeLine); |
---|
81 | display->setTerminalSizeStartup(true); |
---|
82 | |
---|
83 | display->setRandomSeed(session->sessionId() * 31); |
---|
84 | |
---|
85 | return display; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | QTermWidget::QTermWidget(int startnow, QWidget *parent) |
---|
90 | :QWidget(parent) |
---|
91 | { |
---|
92 | m_impl = new TermWidgetImpl(this); |
---|
93 | |
---|
94 | init(); |
---|
95 | |
---|
96 | if (startnow && m_impl->m_session) { |
---|
97 | m_impl->m_session->run(); |
---|
98 | } |
---|
99 | |
---|
100 | this->setFocus( Qt::OtherFocusReason ); |
---|
101 | m_impl->m_terminalDisplay->resize(this->size()); |
---|
102 | |
---|
103 | this->setFocusProxy(m_impl->m_terminalDisplay); |
---|
104 | } |
---|
105 | |
---|
106 | void QTermWidget::startShellProgram() |
---|
107 | { |
---|
108 | if ( m_impl->m_session->isRunning() ) |
---|
109 | return; |
---|
110 | |
---|
111 | m_impl->m_session->run(); |
---|
112 | } |
---|
113 | |
---|
114 | void QTermWidget::init() |
---|
115 | { |
---|
116 | m_impl->m_terminalDisplay->setSize(80, 40); |
---|
117 | |
---|
118 | QFont font = QApplication::font(); |
---|
119 | font.setFamily("Monospace"); |
---|
120 | font.setPointSize(10); |
---|
121 | font.setStyleHint(QFont::TypeWriter); |
---|
122 | setTerminalFont(font); |
---|
123 | setScrollBarPosition(NoScrollBar); |
---|
124 | |
---|
125 | m_impl->m_session->addView(m_impl->m_terminalDisplay); |
---|
126 | |
---|
127 | connect(m_impl->m_session, SIGNAL(finished()), this, SLOT(sessionFinished())); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | QTermWidget::~QTermWidget() |
---|
132 | { |
---|
133 | emit destroyed(); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | void QTermWidget::setTerminalFont(QFont &font) |
---|
138 | { |
---|
139 | if (!m_impl->m_terminalDisplay) |
---|
140 | return; |
---|
141 | m_impl->m_terminalDisplay->setVTFont(font); |
---|
142 | } |
---|
143 | |
---|
144 | void QTermWidget::setShellProgram(QString &progname) |
---|
145 | { |
---|
146 | if (!m_impl->m_session) |
---|
147 | return; |
---|
148 | m_impl->m_session->setProgram(progname); |
---|
149 | } |
---|
150 | |
---|
151 | void QTermWidget::setArgs(QStringList &args) |
---|
152 | { |
---|
153 | if (!m_impl->m_session) |
---|
154 | return; |
---|
155 | m_impl->m_session->setArguments(args); |
---|
156 | } |
---|
157 | |
---|
158 | void QTermWidget::setTextCodec(QTextCodec *codec) |
---|
159 | { |
---|
160 | if (!m_impl->m_session) |
---|
161 | return; |
---|
162 | m_impl->m_session->setCodec(codec); |
---|
163 | } |
---|
164 | |
---|
165 | void QTermWidget::setColorScheme(int scheme) |
---|
166 | { |
---|
167 | switch(scheme) { |
---|
168 | case COLOR_SCHEME_WHITE_ON_BLACK: |
---|
169 | m_impl->m_terminalDisplay->setColorTable(whiteonblack_color_table); |
---|
170 | break; |
---|
171 | case COLOR_SCHEME_GREEN_ON_BLACK: |
---|
172 | m_impl->m_terminalDisplay->setColorTable(greenonblack_color_table); |
---|
173 | break; |
---|
174 | case COLOR_SCHEME_BLACK_ON_LIGHT_YELLOW: |
---|
175 | m_impl->m_terminalDisplay->setColorTable(blackonlightyellow_color_table); |
---|
176 | break; |
---|
177 | default: //do nothing |
---|
178 | break; |
---|
179 | }; |
---|
180 | } |
---|
181 | |
---|
182 | void QTermWidget::setSize(int h, int v) |
---|
183 | { |
---|
184 | if (!m_impl->m_terminalDisplay) |
---|
185 | return; |
---|
186 | m_impl->m_terminalDisplay->setSize(h, v); |
---|
187 | } |
---|
188 | |
---|
189 | void QTermWidget::setHistorySize(int lines) |
---|
190 | { |
---|
191 | if (lines < 0) |
---|
192 | m_impl->m_session->setHistoryType(HistoryTypeFile()); |
---|
193 | else |
---|
194 | m_impl->m_session->setHistoryType(HistoryTypeBuffer(lines)); |
---|
195 | } |
---|
196 | |
---|
197 | void QTermWidget::setScrollBarPosition(ScrollBarPosition pos) |
---|
198 | { |
---|
199 | if (!m_impl->m_terminalDisplay) |
---|
200 | return; |
---|
201 | m_impl->m_terminalDisplay->setScrollBarPosition((TerminalDisplay::ScrollBarPosition)pos); |
---|
202 | } |
---|
203 | |
---|
204 | void QTermWidget::sendText(QString &text) |
---|
205 | { |
---|
206 | m_impl->m_session->sendText(text); |
---|
207 | } |
---|
208 | |
---|
209 | void QTermWidget::resizeEvent(QResizeEvent*) |
---|
210 | { |
---|
211 | //qDebug("global window resizing...with %d %d", this->size().width(), this->size().height()); |
---|
212 | m_impl->m_terminalDisplay->resize(this->size()); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | void QTermWidget::sessionFinished() |
---|
218 | { |
---|
219 | emit finished(); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | //#include "moc_consoleq.cpp" |
---|
224 | |
---|