Skip to content

Commit

Permalink
Added QTreeWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Dec 11, 2017
1 parent 86725cb commit 639380e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 37 deletions.
51 changes: 36 additions & 15 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <QSettings>
#include <QFileInfo>
#include <QProcess>
#include <QObject>
#include <QDialog>
#include <QDebug>
#include <QMenu>
#include <QFile>
#include <QDir>

Expand All @@ -28,6 +30,8 @@ MainWindow::MainWindow(QWidget *parent) :
settings.endGroup();
#else
ui->linePlink->setVisible(false);
ui->buttonPlink->setVisible(false);
ui->labelPlink->setVisible(false);
ui->linePlink->setText("ssh");
#endif

Expand Down Expand Up @@ -96,18 +100,15 @@ void MainWindow::newEntryAdded(QString name, QString host, QString sshPort, QStr
}
}

QListWidgetItem *newItem = new QListWidgetItem();
QTreeWidgetItem *newItem = new QTreeWidgetItem();
itemData[newItem] << name << host << sshPort << user << locPort << extIP << extPort;

QString eStr;
for (QString str : itemData[newItem])
{
eStr += str % " ";
}
eStr = eStr.trimmed();
newItem->setText(eStr);
newItem->setText(0, name);
newItem->setText(1, host);
newItem->setText(2, locPort);
newItem->setText(3, extPort);

ui->listWidget->addItem(newItem);
ui->treeWidget->addTopLevelItem(newItem);

QProcess *process = new QProcess;
process->start(plink, plink_args);
Expand Down Expand Up @@ -205,17 +206,22 @@ QStringList MainWindow::buildPlinkOptions(QString host, QString sshPort, QString
}
#endif

void MainWindow::on_pushButton_2_clicked()
void MainWindow::on_buttonDelete_clicked()
{
for (QListWidgetItem *item : ui->listWidget->selectedItems())
for (QTreeWidgetItem *item : ui->treeWidget->selectedItems())
{
processMap[item]->kill();
processMap.remove(item);
itemData.remove(item);
delete item;
deleteTreeWidgetItem(item);
}
}

void MainWindow::deleteTreeWidgetItem(QTreeWidgetItem *item)
{
processMap[item]->kill();
processMap.remove(item);
itemData.remove(item);
delete item;
}

void MainWindow::toggleWindowState()
{
if (this->isVisible()) {
Expand All @@ -224,3 +230,18 @@ void MainWindow::toggleWindowState()
this->setVisible(true);
}
}

void MainWindow::on_treeWidget_customContextMenuRequested(const QPoint &pos)
{
QMenu *contextMenu = new QMenu();
QAction *deleteAction = contextMenu->addAction(tr("Delete"), this, SLOT(deleteFromContextMenu()));
contextMenu->exec(ui->treeWidget->mapToGlobal(pos));
delete deleteAction;
delete contextMenu;
}

void MainWindow::deleteFromContextMenu()
{
on_buttonDelete_clicked();
}

15 changes: 10 additions & 5 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <QMap>
#include <QMainWindow>
#include <QListWidgetItem>
#include <QTreeWidgetItem>
#include <QSystemTrayIcon>
#include <QProcess>
#include <QStringList>
Expand All @@ -22,11 +22,14 @@ class MainWindow : public QMainWindow
~MainWindow();

private:
QStringList buildPlinkOptions(QString host, QString sshPort, QString user, QString locPort, QString extIP, QString extPort);
void deleteTreeWidgetItem(QTreeWidgetItem *item);

//
Ui::MainWindow *ui;
NewEntry *entry;
QMap<QListWidgetItem*, QProcess*> processMap;
QMap<QListWidgetItem*, QStringList> itemData;
QStringList buildPlinkOptions(QString host, QString sshPort, QString user, QString locPort, QString extIP, QString extPort);
QMap<QTreeWidgetItem*, QProcess*> processMap;
QMap<QTreeWidgetItem*, QStringList> itemData;
QSystemTrayIcon *trayIcon;

private slots:
Expand All @@ -35,8 +38,10 @@ private slots:
void on_actionBeenden_triggered();
void newEntryAdded(QString name, QString host, QString sshPort, QString user, QString locPort, QString extIP, QString extPort, bool startup = true);
void on_buttonPlink_triggered();
void on_pushButton_2_clicked();
void on_buttonDelete_clicked();
void toggleWindowState();
void on_treeWidget_customContextMenuRequested(const QPoint &pos);
void deleteFromContextMenu();
};

#endif // MAINWINDOW_H
48 changes: 37 additions & 11 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>500</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -18,7 +18,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="labelPlink">
<property name="text">
<string>plink:</string>
</property>
Expand All @@ -43,14 +43,14 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="buttonNewentry">
<property name="text">
<string>New entry</string>
<string>New Entry</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="buttonDelete">
<property name="text">
<string>Delete</string>
</property>
Expand All @@ -72,7 +72,34 @@
</layout>
</item>
<item>
<widget class="QListWidget" name="listWidget"/>
<widget class="QTreeWidget" name="treeWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
</property>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Host</string>
</property>
</column>
<column>
<property name="text">
<string>Local Port</string>
</property>
</column>
<column>
<property name="text">
<string>Remote Port</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
Expand All @@ -81,8 +108,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
<width>500</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuStart">
Expand All @@ -93,7 +120,6 @@
</widget>
<addaction name="menuStart"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionBeenden">
<property name="text">
<string>Quit</string>
Expand All @@ -104,7 +130,7 @@
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<sender>buttonNewentry</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>addNewEntry()</slot>
Expand Down
12 changes: 6 additions & 6 deletions newentry.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>416</height>
<height>513</height>
</rect>
</property>
<property name="windowTitle">
<string>Neuen Eintrag erstellen</string>
<string>Add New Entry</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
Expand Down Expand Up @@ -44,7 +44,7 @@
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>SSH-Port:</string>
<string>SSH Port:</string>
</property>
</widget>
</item>
Expand All @@ -64,7 +64,7 @@
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Lokaler-Port:</string>
<string>Local Port:</string>
</property>
</widget>
</item>
Expand All @@ -74,7 +74,7 @@
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Remote-IP:</string>
<string>Remote IP:</string>
</property>
</widget>
</item>
Expand All @@ -84,7 +84,7 @@
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Remote-Port:</string>
<string>Remote Port:</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 639380e

Please sign in to comment.