Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: new/select/run2 console support #911

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/plugins/console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ bool Console::start()
using namespace std::placeholders;
terminalService->sendCommand = std::bind(&ConsoleManager::sendCommand, consoleManager, _1);
terminalService->executeCommand = std::bind(&ConsoleManager::executeCommand, consoleManager, _1, _2, _3, _4, _5);
terminalService->createConsole = std::bind(&ConsoleManager::newConsole, consoleManager, _1, _2);
terminalService->selectConsole = std::bind(&ConsoleManager::selectConsole, consoleManager, _1);
terminalService->run2Console = std::bind(&ConsoleManager::run2Console, consoleManager, _1, _2);
}
return true;
}
Expand Down
45 changes: 43 additions & 2 deletions src/plugins/console/consolemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <DFrame>

#include <QUuid>
#include <QProcess>
#include <QHBoxLayout>
#include <QListView>
#include <QSplitter>
Expand All @@ -32,6 +33,7 @@ class ConsoleManagerPrivate : public QObject
void appendConsole();
void removeConsole();
void switchConsole(const QModelIndex &index, const QModelIndex &previous);
void switchConsole(const QUuid &uuid);
void updateButtonState();

public:
Expand Down Expand Up @@ -99,7 +101,8 @@ void ConsoleManagerPrivate::initUI()

void ConsoleManagerPrivate::initConnection()
{
connect(consoleListView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ConsoleManagerPrivate::switchConsole);
connect(consoleListView->selectionModel(), &QItemSelectionModel::currentChanged,
this, QOverload<const QModelIndex &, const QModelIndex &>::of(&ConsoleManagerPrivate::switchConsole));
connect(addConsoleBtn, &DToolButton::clicked, this, &ConsoleManagerPrivate::appendConsole);
connect(removeConsoleBtn, &DToolButton::clicked, this, &ConsoleManagerPrivate::removeConsole);
connect(model, &QStandardItemModel::rowsRemoved, this, &ConsoleManagerPrivate::updateButtonState);
Expand Down Expand Up @@ -141,6 +144,13 @@ void ConsoleManagerPrivate::switchConsole(const QModelIndex &index, const QModel
}
}

void ConsoleManagerPrivate::switchConsole(const QUuid &uuid) {
if (auto console = q->findConsole(uuid.toString())) {
console->setFocus();
consoleStackedWidget->setCurrentWidget(console);
}
}

void ConsoleManagerPrivate::updateButtonState()
{
removeConsoleBtn->setEnabled(model->rowCount() > 1);
Expand Down Expand Up @@ -175,7 +185,7 @@ QTermWidget *ConsoleManager::findConsole(const QString &id)
return d->consoleMap.value(id, nullptr);
}

QTermWidget *ConsoleManager::createConsole(const QString &name, bool startNow)
QTermWidget *ConsoleManager::createConsole(const QString &name, bool startNow, bool rename)
{
auto id = QUuid::createUuid().toString();
ConsoleWidget *console = new ConsoleWidget(this, startNow);
Expand All @@ -184,6 +194,7 @@ QTermWidget *ConsoleManager::createConsole(const QString &name, bool startNow)

QStandardItem *item = new QStandardItem(name);
item->setData(id, IdRole);
item->setData(rename, Qt::EditRole);
d->model->appendRow(item);
d->consoleListView->setCurrentIndex(d->model->index(d->model->rowCount() - 1, 0));

Expand Down Expand Up @@ -218,6 +229,36 @@ void ConsoleManager::executeCommand(const QString &name, const QString &program,
console->sendText(cmd);
}

QUuid ConsoleManager::newConsole(const QString &name, bool rename)
{
auto ptr_term = dynamic_cast<ConsoleWidget*>(this->createConsole(name, true, rename));
return QUuid::fromString(d->consoleMap.key(ptr_term, ""));
}

void ConsoleManager::selectConsole(const QUuid &uuid)
{
d->switchConsole(uuid);
}

void ConsoleManager::run2Console(const QUuid &uuid, const QProcess &process)
{
auto ptr_term = findConsole(uuid.toString());
if (!ptr_term)
return;

if (!process.workingDirectory().isEmpty())
ptr_term->changeDir(process.workingDirectory());
if (!process.environment().isEmpty())
ptr_term->setEnvironment(process.environment());
if (!process.program().isEmpty()) {
QString cmd = process.program();
if (!process.arguments().isEmpty())
cmd += ' ' + process.arguments().join(' ');
cmd += '\n';
ptr_term->sendText(cmd);
}
}

void ConsoleManager::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/console/consolemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "consolewidget.h"

class QProcess;
class ConsoleManagerPrivate;
class ConsoleManager : public QWidget
{
Expand All @@ -17,9 +18,12 @@ class ConsoleManager : public QWidget

QTermWidget *currentConsole();
QTermWidget *findConsole(const QString &id);
QTermWidget *createConsole(const QString &name, bool startNow = true);
QTermWidget *createConsole(const QString &name, bool startNow = true, bool rename = true);
void sendCommand(const QString &text);
void executeCommand(const QString &name, const QString &program, const QStringList &args, const QString &workingDir, const QStringList &env);
QUuid newConsole(const QString &name, bool rename = true);
void selectConsole(const QUuid &uuid);
void run2Console(const QUuid &uuid, const QProcess &process);

private:
void showEvent(QShowEvent *event) override;
Expand Down
3 changes: 3 additions & 0 deletions src/services/terminal/terminalservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class SERVICE_EXPORT TerminalService final : public dpf::PluginService, dpf::Aut
*/
DPF_INTERFACE(void, sendCommand, const QString &command);
DPF_INTERFACE(void, executeCommand, const QString &name, const QString &program, const QStringList &args, const QString &workingDir, const QStringList &env);
DPF_INTERFACE(QUuid, createConsole, const QString &name, bool rename);
DPF_INTERFACE(void, selectConsole, const QUuid &id);
DPF_INTERFACE(void, run2Console, const QUuid &id, const QProcess& process);
};

} // namespace dpfservice
Expand Down
Loading