📄 mainwindow.cpp
字号:
/*************************************************************************** * Copyright (C) 2007 by Motorola Commnunity around the World * * lahu3672@googlemail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "MainWindow.h"#include <ZApplication.h>#include <ZSoftKey.h>#include <ZOptionsMenu.h>#include <ZMessageDlg.h>#define SEQ_UP "\x1b\x5b\x41"#define SEQ_DOWN "\x1b\x5b\x42"#define SEQ_RIGHT "\x1b\x5b\x43"#define SEQ_LEFT "\x1b\x5b\x44"const char *args[] = {"bash", "-i", NULL};MainWindow::MainWindow(QWidget *parent, const char *name): ZKbMainWidget(parent, name){ end = false; createUI(); setupUI(); setupSK(); connect(&inp, SIGNAL(charSuggested(const QChar&)), this, SLOT(slotCharSuggested(const QChar&))); connect(&inp, SIGNAL(charConfirmed(const QChar&)), this, SLOT(slotCharConfirmed(const QChar&))); suggested = false; setContentWidget(textbuf); cop = new QCopChannel(EZXTERM_CHANNEL_NAME, this); connect(cop, SIGNAL(received(const QCString&, const QByteArray&)), this, SLOT(slotQCopReceived(const QCString&, const QByteArray&))); ex = new QAppExec("/bin/bash", (char* const*)args); connect(ex, SIGNAL(readyRead()), this, SLOT(slotReadData())); connect(ex, SIGNAL(stopped()), this, SLOT(slotAppStopped()));}MainWindow::~MainWindow(){ end = true; ex->stop(); delete ex;}void MainWindow::createUI(){ textbuf = new TextArea(this); connect(textbuf, SIGNAL(configured()), this, SLOT(slotResize()));}void MainWindow::setupUI(){ setAppTitle(QString("ezxterm"));// textbuf->resize(100, 100);}void MainWindow::setupSK(){ ZSoftKey *keys; QRect rect; ZOptionsMenu *menu, *send; keys = getSoftKey(); keys->setText(ZSoftKey::LEFT, "Menu"); keys->setText(ZSoftKey::RIGHT, "Home"); menu = new ZOptionsMenu(rect, this); menu->insertItem("Hide", NULL, this, SLOT(slotHide())); menu->insertItem("Send ASCII...", NULL, this, SLOT(slotSendASCII())); send = new ZOptionsMenu(rect, this); send->insertItem("Send Ctrl-C", NULL, this, SLOT(slotSendCC())); send->insertItem("Send Ctrl-Z", NULL, this, SLOT(slotSendCZ())); send->insertItem("Send Ctrl-D", NULL, this, SLOT(slotSendCD())); menu->insertItem("Send", send); menu->insertItem("Exit", NULL, this, SLOT(slotQuitApp())); keys->setOptMenu(ZSoftKey::LEFT, menu); keys->setClickedSlot(ZSoftKey::RIGHT, this, SLOT(slotHome()));}void MainWindow::slotReadData(){ if (end) return; QByteArray data(ex->size()); ex->readBlock(data); textbuf->appendText(data);}void MainWindow::keyPressEvent(QKeyEvent *event){ if (this->isHidden()) return; QByteArray block; switch (event->key()) { case EZX_KEY_UP: ex->writeBlock(block.duplicate(SEQ_UP, strlen(SEQ_UP))); event->accept(); break; case EZX_KEY_DOWN: ex->writeBlock(block.duplicate(SEQ_DOWN, strlen(SEQ_DOWN))); event->accept(); break; case EZX_KEY_RIGHT: ex->writeBlock(block.duplicate(SEQ_RIGHT, strlen(SEQ_RIGHT))); event->accept(); break; case EZX_KEY_LEFT: ex->writeBlock(block.duplicate(SEQ_LEFT, strlen(SEQ_LEFT))); event->accept(); break; case EZX_KEY_SIDE_UP: textbuf->scroll(10); break; case EZX_KEY_SIDE_DOWN: textbuf->scroll(-10); break; default: if (inp.processEvent(event)) event->accept(); else event->ignore(); }}void MainWindow::keyReleaseEvent(QKeyEvent *event){ event->ignore();}void MainWindow::slotCharSuggested(const QChar &ch){ setAppTitle(QString("ezxterm - ")+ch);// if (suggested) textbuf->back(); else suggested = true;// textbuf->appendChar(ch);}void MainWindow::slotCharConfirmed(const QChar &ch){ setAppTitle(QString("ezxterm"));// textbuf->back();// textbuf->appendChar(ch); sendChar(ch); suggested = false;}void MainWindow::slotQCopReceived(const QCString &msg, const QByteArray &data){ if (msg == "show()") this->show();}void MainWindow::slotAppStopped(){ if (end) return; ZMessageDlg dlg("ezxterm", "Child died.", ZMessageDlg::TypeOK, 0, this); dlg.exec();}void MainWindow::slotQuitApp(){ ((QApplication*)qApp)->exit();}void MainWindow::sendChar(char ch){ QByteArray a(1); a[0] = ch; ex->writeBlock(a);}void MainWindow::slotSendCC(){ sendChar('\x03');}void MainWindow::slotSendCZ(){ sendChar('\x1a');}void MainWindow::slotSendCD(){ sendChar('\x04');}void MainWindow::slotHide(){ hide();}void MainWindow::slotSendASCII(){ ZNumPickerDlg *dlg = new ZNumPickerDlg(3, this); ZNumModule *num = dlg->getNumModule(); num->setMinValue(0); num->setMaxValue(0xff); num->setValue(0); num->setLineStep(1); if (dlg->exec() == ZPopup::Accepted) { char code = (char)dlg->getNumValue(); sendChar(code); }}void MainWindow::slotResize(){ ex->start(); struct winsize ws; textbuf->getSize(&ws); ex->setSize(&ws);}void MainWindow::slotHome(){ textbuf->home();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -