Change from QListWidget to QTreeWidget
authorChristoph Egger <Christoph.Egger@fau.de>
Wed, 4 Mar 2015 14:12:46 +0000 (15:12 +0100)
committerChristoph Egger <Christoph.Egger@fau.de>
Wed, 4 Mar 2015 14:12:46 +0000 (15:12 +0100)
This way we can group functions on the left plane. Allows to group and
hide some groups of items. As a first take, group the external
functions. This also places them at the top where they stay together.

src/gui/Mainwindow.cxx
src/gui/Mainwindow.hxx
src/gui/qt.hxx

index 0c08892642dd760b4f5e611254e9c8c67de2467e..fe48f8fd38cb4d0af385844466fc7e1921d5c22a 100644 (file)
@@ -44,7 +44,8 @@ Mainwindow::Mainwindow(InformationManager* mgr)
        scripting->setAllowedAreas(Qt::BottomDockWidgetArea);
        addDockWidget(Qt::BottomDockWidgetArea, scripting);
 
-       listWidget = new QListWidget();
+       listWidget = new QTreeWidget();
+       listWidget->setColumnCount(1);
        listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(listWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
                this, SLOT(showListContextMenu(const QPoint&)));
@@ -57,15 +58,20 @@ Mainwindow::Mainwindow(InformationManager* mgr)
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
        setCentralWidget(stackedWidget);
 
-       connect(listWidget, SIGNAL(currentRowChanged(int)),
-               this, SLOT(switchMainPlane(int)));
+       connect(listWidget, &QTreeWidget::currentItemChanged,
+               [=] (QTreeWidgetItem* current, QTreeWidgetItem*) {
+                       switchMainPlane(current);
+               });
 
        setWindowTitle(tr("FRIDA"));
 
+       QTreeWidgetItem * external = new QTreeWidgetItem(listWidget, QStringList("External Functions"));
+       external->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
+       external->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
        mgr->connect_new_function_signal([&] (Function* fun) {addFunction(fun);});
-       mgr->connect_new_dyn_symbol_signal([&] (const std::string& name) {
-                       auto item = new QListWidgetItem(name.c_str(), listWidget);
-                       item->setBackground(QBrush(QColor(0xff, 0xdd, 0xdd)));
+       mgr->connect_new_dyn_symbol_signal([=] (const std::string& name) {
+                       auto item = new QTreeWidgetItem(external, QStringList(name.c_str()));
+                       item->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
                });
        setGlobalHotkeys();
 }
@@ -76,7 +82,7 @@ void Mainwindow::setGlobalHotkeys() {
 
        shortcut = new QShortcut(QKeySequence("r"), listWidget);
        connect(shortcut, &QShortcut::activated, [=]() {
-                       QListWidgetItem * item = listWidget->currentItem();
+                       QTreeWidgetItem * item = listWidget->currentItem();
                        if (item) renameFunction(objects_list[item]->getFunction());
                });
 }
@@ -107,7 +113,7 @@ void Mainwindow::save() {
 void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
        if (objects_list_by_address.find(address) != objects_list_by_address.end()) {
                LOG4CXX_DEBUG(logger, "Switching to function " << std::hex << address);
-               QListWidgetItem * item = objects_list_by_address[address];
+               QTreeWidgetItem * item = objects_list_by_address[address];
                listWidget->setCurrentItem(item);
                stackedWidget->setCurrentWidget(objects_list[item]);
        } else {
@@ -116,12 +122,13 @@ void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
        }
 }
 
-void Mainwindow::switchMainPlane(int) {
-       stackedWidget->setCurrentWidget(objects_list[listWidget->currentItem()]);
+void Mainwindow::switchMainPlane(QTreeWidgetItem* to) {
+       if (objects_list.end() != objects_list.find(to))
+               stackedWidget->setCurrentWidget(objects_list[to]);
 }
 
 void Mainwindow::showListContextMenu(const QPoint& point) {
-       QListWidgetItem * item = listWidget->itemAt(point);
+       QTreeWidgetItem * item = listWidget->itemAt(point);
        QMenu menu(this);
        if (item) {
                QAction * act = menu.addAction("Rename Function");
@@ -156,7 +163,7 @@ void Mainwindow::renameFunction(Function* function) {
                LOG4CXX_DEBUG(logger, "renaming Function " << function->getName()
                              << " to " << dialog.result().toStdString());
                function->setName(dialog.result().toStdString());
-               objects_list_by_address[function->getStartAddress()]->setText(dialog.result());
+               objects_list_by_address[function->getStartAddress()]->setText(0, dialog.result());
        } else {
                LOG4CXX_DEBUG(logger, "renameFunction aborted");
        }
@@ -194,7 +201,7 @@ void Mainwindow::addFunction(Function* fun) {
 
        w->addTab(t, "Listing");
 
-       QListWidgetItem * item = new QListWidgetItem(fun->getName().c_str(), listWidget);
+       QTreeWidgetItem * item = new QTreeWidgetItem(listWidget, QStringList(fun->getName().c_str()));
        stackedWidget->addWidget(w);
        objects_list.insert(std::make_pair(item, w));
        LOG4CXX_DEBUG(logger, "Adding function widget at " << std::hex
index ba0d8b4944529138ff78fbf2e9b995133c677dab..a3bd4bdfa88ff354c0ee09a557ceb4d6d8c668c3 100644 (file)
@@ -7,7 +7,7 @@
 #include <QTextEdit>
 #include <QPushButton>
 #include <QMainWindow>
-#include <QListWidget>
+#include <QTreeWidget>
 #include <QStackedWidget>
 
 #include <log4cxx/logger.h>
@@ -37,7 +37,7 @@ private:
        QMenu *fileMenu;
 
        QTabWidget * tabwidget;
-       QListWidget * listWidget;
+       QTreeWidget * listWidget;
        QStackedWidget * stackedWidget;
        QDockWidget * dockWidget;
        ScriptingDock * scripting;
@@ -48,8 +48,8 @@ private:
 
        std::map<uint64_t, BasicBlockWidget*> blocks;
        std::map<uint64_t, Function*> functions;
-       std::map<QListWidgetItem*, FunctionWidget*> objects_list;
-       std::map<uint64_t, QListWidgetItem*> objects_list_by_address;
+       std::map<QTreeWidgetItem*, FunctionWidget*> objects_list;
+       std::map<uint64_t, QTreeWidgetItem*> objects_list_by_address;
 
        InformationManager* manager;
        log4cxx::LoggerPtr logger;
@@ -58,7 +58,7 @@ private Q_SLOTS:
        void quit();
        void open();
        void save();
-       void switchMainPlane(int);
+       void switchMainPlane(QTreeWidgetItem* item);
        void showListContextMenu(const QPoint&);
        void requestNewFunction();
        void renameFunction(Function* function);
index ae4ef2c51fd46876c95533160c9e65e97b87e215..ae370d561c3f8658f1401bce16ed5a94aa71157c 100644 (file)
@@ -10,7 +10,7 @@
 #include <QGraphicsView>
 #include <QGroupBox>
 #include <QHeaderView>
-#include <QListWidget>
+#include <QTreeWidget>
 #include <QMenu>
 #include <QMenuBar>
 #include <QMessageBox>