Skip to content

Commit

Permalink
clean code 2
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <2014500726@smail.xtu.edu.cn>
  • Loading branch information
QQxiaoming committed Aug 27, 2024
1 parent 723430f commit 8a6dd29
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 362 deletions.
2 changes: 1 addition & 1 deletion lib/qtermwidget/Emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public slots:
* Emitted after receiving the escape sequence which asks to change
* the terminal emulator's size
*/
void imageResizeRequest(const QSize& sizz);
void imageResizeRequest(const QSize& size);

/**
* Emitted when the terminal program requests to change various properties
Expand Down
201 changes: 30 additions & 171 deletions lib/qtermwidget/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,27 @@ Session::Session(QObject* parent) :
//create emulation backend
_emulation = new Vt102Emulation();

connect( _emulation, SIGNAL( titleChanged( int, const QString & ) ),
this, SLOT( setUserTitle( int, const QString & ) ) );
connect( _emulation, SIGNAL( stateSet(int) ),
this, SLOT( activityStateSet(int) ) );
connect( _emulation, SIGNAL( changeTabTextColorRequest( int ) ),
this, SIGNAL( changeTabTextColorRequest( int ) ) );
connect( _emulation, SIGNAL(profileChangeCommandReceived(const QString &)),
this, SIGNAL( profileChangeCommandReceived(const QString &)) );

connect(_emulation, &Emulation::primaryScreenInUse,
this, &Session::onPrimaryScreenInUse);
connect(_emulation, SIGNAL(imageResizeRequest(QSize)),
this, SLOT(onEmulationSizeChange(QSize)));
connect(_emulation, SIGNAL(imageSizeChanged(int, int)),
this, SLOT(onViewSizeChange(int, int)));
connect(_emulation, &Vt102Emulation::cursorChanged,
this, &Session::cursorChanged);

connect( _emulation, &Emulation::titleChanged, this, &Session::setUserTitle);
connect( _emulation, &Emulation::stateSet, this, &Session::activityStateSet);
connect( _emulation, &Emulation::changeTabTextColorRequest, this, &Session::changeTabTextColorRequest);
connect( _emulation, &Emulation::profileChangeCommandReceived, this, &Session::profileChangeCommandReceived);

connect(_emulation, &Emulation::primaryScreenInUse, this, [this](bool use){
_isPrimaryScreen = use;
emit primaryScreenInUse(use);
});
connect(_emulation, &Emulation::imageResizeRequest, this, [this](const QSize& size){
setSize(size);
});
connect(_emulation, &Emulation::imageSizeChanged, this, [this](int /*height*/, int /*width*/){
updateTerminalSize();
});
connect(_emulation, &Vt102Emulation::cursorChanged, this, &Session::cursorChanged);

//setup timer for monitoring session activity
_monitorTimer = new QTimer(this);
_monitorTimer->setSingleShot(true);
connect(_monitorTimer, SIGNAL(timeout()), this, SLOT(monitorTimerDone()));
connect(_monitorTimer, &QTimer::timeout, this, &Session::monitorTimerDone);
}

WId Session::windowId() const
Expand Down Expand Up @@ -120,36 +118,29 @@ void Session::addView(TerminalDisplay * widget)

if ( _emulation != nullptr ) {
// connect emulation - view signals and slots
connect( widget , &TerminalDisplay::keyPressedSignal, _emulation ,
&Emulation::sendKeyEvent);
connect( widget , SIGNAL(mouseSignal(int,int,int,int)) , _emulation ,
SLOT(sendMouseEvent(int,int,int,int)) );
connect( widget , SIGNAL(sendStringToEmu(const char *)) , _emulation ,
SLOT(sendString(const char *)) );
connect(widget, &TerminalDisplay::keyPressedSignal, _emulation, &Emulation::sendKeyEvent);
connect(widget, &TerminalDisplay::mouseSignal, _emulation, &Emulation::sendMouseEvent);
connect(widget, &TerminalDisplay::sendStringToEmu, this, [this](const char* s){
_emulation->sendString(s);
});

// allow emulation to notify view when the foreground process
// indicates whether or not it is interested in mouse signals
connect( _emulation , SIGNAL(programUsesMouseChanged(bool)) , widget ,
SLOT(setUsesMouse(bool)) );

widget->setUsesMouse( _emulation->programUsesMouse() );

connect( _emulation , SIGNAL(programBracketedPasteModeChanged(bool)) ,
widget , SLOT(setBracketedPasteMode(bool)) );

connect(_emulation, &Emulation::programUsesMouseChanged, widget, &TerminalDisplay::setUsesMouse);
widget->setUsesMouse(_emulation->programUsesMouse() );
connect(_emulation, &Emulation::programBracketedPasteModeChanged, widget, &TerminalDisplay::setBracketedPasteMode);
widget->setBracketedPasteMode(_emulation->programBracketedPasteMode());

widget->setScreenWindow(_emulation->createWindow());
}

//connect view signals and slots
QObject::connect( widget ,SIGNAL(changedContentSizeSignal(int,int)),this,
SLOT(onViewSizeChange(int,int)));
QObject::connect(widget, &TerminalDisplay::changedContentSizeSignal, this, [this](int /*height*/, int /*width*/){
updateTerminalSize();
});

QObject::connect( widget ,SIGNAL(destroyed(QObject *)) , this ,
SLOT(viewDestroyed(QObject *)) );
QObject::connect(widget, &TerminalDisplay::destroyed, this, &Session::viewDestroyed);
//slot for close
QObject::connect(this, SIGNAL(finished()), widget, SLOT(close()));
QObject::connect(this, &Session::finished, widget, &TerminalDisplay::close);
//slot for primaryScreen
QObject::connect(this, &Session::primaryScreenInUse, widget, &TerminalDisplay::usingPrimaryScreen);
}
Expand Down Expand Up @@ -343,20 +334,6 @@ void Session::activityStateSet(int state)
emit stateChanged(state);
}

void Session::onViewSizeChange(int /*height*/, int /*width*/)
{
updateTerminalSize();
}
void Session::onEmulationSizeChange(QSize size)
{
setSize(size);
}

void Session::onPrimaryScreenInUse(bool use)
{
_isPrimaryScreen = use;
emit primaryScreenInUse(use);
}

void Session::updateTerminalSize()
{
Expand Down Expand Up @@ -606,121 +583,3 @@ int Session::recvData(const char *buff, int len)
return len;
}

SessionGroup::SessionGroup()
: _masterMode(0)
{
}
SessionGroup::~SessionGroup()
{
// disconnect all
connectAll(false);
}
int SessionGroup::masterMode() const
{
return _masterMode;
}
QList<Session *> SessionGroup::sessions() const
{
return _sessions.keys();
}
bool SessionGroup::masterStatus(Session * session) const
{
return _sessions[session];
}

void SessionGroup::addSession(Session * session)
{
_sessions.insert(session,false);

QListIterator<Session *> masterIter(masters());

while ( masterIter.hasNext() ) {
connectPair(masterIter.next(),session);
}
}
void SessionGroup::removeSession(Session * session)
{
setMasterStatus(session,false);

QListIterator<Session *> masterIter(masters());

while ( masterIter.hasNext() ) {
disconnectPair(masterIter.next(),session);
}

_sessions.remove(session);
}
void SessionGroup::setMasterMode(int mode)
{
_masterMode = mode;

connectAll(false);
connectAll(true);
}
QList<Session *> SessionGroup::masters() const
{
return _sessions.keys(true);
}
void SessionGroup::connectAll(bool connect)
{
QListIterator<Session *> masterIter(masters());

while ( masterIter.hasNext() ) {
Session * master = masterIter.next();

QListIterator<Session *> otherIter(_sessions.keys());
while ( otherIter.hasNext() ) {
Session * other = otherIter.next();

if ( other != master ) {
if ( connect ) {
connectPair(master,other);
} else {
disconnectPair(master,other);
}
}
}
}
}
void SessionGroup::setMasterStatus(Session * session, bool master)
{
bool wasMaster = _sessions[session];
_sessions[session] = master;

if (wasMaster == master) {
return;
}

QListIterator<Session *> iter(_sessions.keys());
while (iter.hasNext()) {
Session * other = iter.next();

if (other != session) {
if (master) {
connectPair(session, other);
} else {
disconnectPair(session, other);
}
}
}
}

void SessionGroup::connectPair(Session * master , Session * other) const
{
if ( _masterMode & CopyInputToAll ) {
qDebug() << "Connection session " << master->nameTitle() << "to" << other->nameTitle();

connect( master->emulation() , SIGNAL(sendData(const char *,int)) , other->emulation() ,
SLOT(sendString(const char *,int)) );
}
}
void SessionGroup::disconnectPair(Session * master , Session * other) const
{
if ( _masterMode & CopyInputToAll ) {
qDebug() << "Disconnecting session " << master->nameTitle() << "from" << other->nameTitle();

disconnect( master->emulation() , SIGNAL(sendData(const char *,int)) , other->emulation() ,
SLOT(sendString(const char *,int)) );
}
}

97 changes: 0 additions & 97 deletions lib/qtermwidget/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public slots:
void setUserTitle( int, const QString & caption );

signals:

/** Emitted when the terminal process starts. */
void started();

Expand Down Expand Up @@ -408,23 +407,10 @@ public slots:
private slots:
void onReceiveBlock( const char * buffer, int len );
void monitorTimerDone();

void onViewSizeChange(int height, int width);
void onEmulationSizeChange(QSize);

void activityStateSet(int);

//automatically detach views from sessions when view is destroyed
void viewDestroyed(QObject * view);

// void zmodemReadStatus();
// void zmodemReadAndSendBlock();
// void zmodemRcvBlock(const char *data, int len);
// void zmodemFinished();

// Relays the signal from Emulation and sets _isPrimaryScreen
void onPrimaryScreenInUse(bool use);

private:
void updateTerminalSize();
WId windowId() const;
Expand Down Expand Up @@ -456,97 +442,14 @@ private slots:
bool _isTitleChanged; ///< flag if the title/icon was changed by user
bool _flowControl;
bool _fullScripting;

int _sessionId;

// ZModem
// bool _zmodemBusy;
// KProcess* _zmodemProc;
// ZModemDialog* _zmodemProgress;

// Color/Font Changes by ESC Sequences

QColor _modifiedBackground; // as set by: echo -en '\033]11;Color\007

QString _profileKey;

bool _hasDarkBackground;

static int lastSessionId;

bool _isPrimaryScreen;
};

/**
* Provides a group of sessions which is divided into master and slave sessions.
* Activity in master sessions can be propagated to all sessions within the group.
* The type of activity which is propagated and method of propagation is controlled
* by the masterMode() flags.
*/
class SessionGroup : public QObject {
Q_OBJECT

public:
/** Constructs an empty session group. */
SessionGroup();
/** Destroys the session group and removes all connections between master and slave sessions. */
~SessionGroup() override;

/** Adds a session to the group. */
void addSession( Session * session );
/** Removes a session from the group. */
void removeSession( Session * session );

/** Returns the list of sessions currently in the group. */
QList<Session *> sessions() const;

/**
* Sets whether a particular session is a master within the group.
* Changes or activity in the group's master sessions may be propagated
* to all the sessions in the group, depending on the current masterMode()
*
* @param session The session whose master status should be changed.
* @param master True to make this session a master or false otherwise
*/
void setMasterStatus( Session * session , bool master );
/** Returns the master status of a session. See setMasterStatus() */
bool masterStatus( Session * session ) const;

/**
* This enum describes the options for propagating certain activity or
* changes in the group's master sessions to all sessions in the group.
*/
enum MasterMode {
/**
* Any input key presses in the master sessions are sent to all
* sessions in the group.
*/
CopyInputToAll = 1
};

/**
* Specifies which activity in the group's master sessions is propagated
* to all sessions in the group.
*
* @param mode A bitwise OR of MasterMode flags.
*/
void setMasterMode( int mode );
/**
* Returns a bitwise OR of the active MasterMode flags for this group.
* See setMasterMode()
*/
int masterMode() const;

private:
void connectPair(Session * master , Session * other) const;
void disconnectPair(Session * master , Session * other) const;
void connectAll(bool connect);
QList<Session *> masters() const;

// maps sessions to their master status
QHash<Session *,bool> _sessions;

int _masterMode;
};

#endif
Loading

0 comments on commit 8a6dd29

Please sign in to comment.