some tricks for flame graph:
authorAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Mon, 2 Apr 2018 19:37:49 +0000 (22:37 +0300)
committerAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Mon, 2 Apr 2018 19:37:49 +0000 (22:37 +0300)
- implementing back/forward actions in NO_K_LIB mode;
- trying to fix a floating bug with wrong initial graph display position

src/analyze/gui/chartwidget.cpp
src/analyze/gui/contextmenuqwt.cpp
src/analyze/gui/contextmenuqwt.h
src/analyze/gui/flamegraph.cpp
src/analyze/gui/flamegraph.h
src/analyze/gui/histogramwidget.cpp
src/analyze/gui/mainwindow.cpp
src/analyze/gui/mainwindow.h

index 0d2e5bd6dd061e23931d1c3e5303767beacb1779..614d79f4d53295b7d3a5a905335998f789de03ea 100644 (file)
@@ -356,7 +356,10 @@ void ChartWidget::contextMenuEvent(QContextMenuEvent *event)
 
 void ChartWidget::keyPressEvent(QKeyEvent *event)
 {
-    m_contextMenuQwt->handleKeyPress(event);
+    if (!m_contextMenuQwt->handleKeyPress(event))
+    {
+        QWidget::keyPressEvent(event);
+    }
 }
 
 void ChartWidget::resetZoom()
index db1e1c7a600cbf9b6e06e655cf5d37c2ab6b3ada..da3182b39632ea61c572dc5027cf58816eb93aeb 100644 (file)
@@ -133,7 +133,7 @@ void ContextMenuQwt::initializeMenu(QMenu& menu, ChartOptions::Options options)
     }
 }
 
-void ContextMenuQwt::handleKeyPress(QKeyEvent *event)
+bool ContextMenuQwt::handleKeyPress(QKeyEvent *event)
 {
     if (event->modifiers() & Qt::AltModifier)
     {
@@ -188,13 +188,10 @@ void ContextMenuQwt::handleKeyPress(QKeyEvent *event)
             }
             break;
         default:
-            event->ignore();
-            return;
+            return false;
         }
         event->accept();
+        return true;
     }
-    else
-    {
-        event->ignore();
-    }
+    return false;
 }
index b2327da2ea03c8e511217b80d4257f6057ae526a..f6a54dbb1c75ece5c46d8814ac0020926fe151f2 100644 (file)
@@ -24,7 +24,7 @@ public:
 
     void initializeMenu(QMenu& menu, ChartOptions::Options options) const;
 
-    void handleKeyPress(QKeyEvent *event);
+    bool handleKeyPress(QKeyEvent *event);
 
 private:
     QAction* m_resetZoomAction;
index b9ebfc6e2a2cadb87d85f33c69eb5b01fc66567c..136f45c3d016681beab7cbf422bb1d950a2d3832 100644 (file)
@@ -21,6 +21,7 @@
 #include <cmath>
 
 #include <QAction>
+#include <QApplication>
 #include <QCheckBox>
 #include <QComboBox>
 #include <QCursor>
@@ -651,9 +652,7 @@ FlameGraph::FlameGraph(QWidget* parent, Qt::WindowFlags flags)
     layout()->addWidget(m_displayLabel);
     layout()->addWidget(m_searchResultsLabel);
 
-#ifdef NO_K_LIB
-    // TODO!! implement back and forward
-#else
+#ifndef NO_K_LIB
     m_backAction = KStandardAction::back(this, SLOT(navigateBack()), this);
     addAction(m_backAction);
     m_forwardAction = KStandardAction::forward(this, SLOT(navigateForward()), this);
@@ -718,6 +717,38 @@ bool FlameGraph::eventFilter(QObject* object, QEvent* event)
     return ret;
 }
 
+#if NO_K_LIB
+bool FlameGraph::handleKeyPress(QKeyEvent *event)
+{
+    if (m_view->hasFocus() || (qApp->focusWidget() == nullptr))
+    {
+        if (event->modifiers() & Qt::AltModifier)
+        {
+            switch (event->key())
+            {
+            case Qt::Key_Backspace:
+            case Qt::Key_Right:
+                navigateForward();
+                return true;
+            case Qt::Key_Left:
+                navigateBack();
+                return true;
+            }
+        }
+        else
+        {
+            switch (event->key())
+            {
+            case Qt::Key_Backspace:
+                navigateBack();
+                return true;
+            }
+        }
+    }
+    return false;
+}
+#endif
+
 void FlameGraph::setTopDownData(const TreeData& topDownData)
 {
     m_topDownData = topDownData;
@@ -809,6 +840,15 @@ void FlameGraph::setData(FrameGraphicsItem* rootItem)
 
     if (isVisible()) {
         selectItem(m_rootItem);
+        // trying to fix a bug (?): sometimes the flamegraph is displayed at wrong position initially
+        // (observed after switching to the flamegraph tab soon after the application starts with
+        // a data file specified in the command line)
+        static bool firstTime = true;
+        if (firstTime)
+        {
+            m_view->updateGeometry();
+            firstTime = false;
+        }
     }
 }
 
index 973f32f1af9356e7a4961d223358832a62538bd2..65ef505cac7de50a97238c2f440ff12608c6d9d8 100644 (file)
@@ -43,10 +43,14 @@ public:
     void setBottomUpData(const TreeData& bottomUpData);
 
     void clearData();
-
+#if NO_K_LIB
+    // handling back and forward shortcuts:
+    // keyPressEvent doesn't receive arrow keys so the main window calls
+    // this function from its event filter
+    bool handleKeyPress(QKeyEvent* event);
+#endif
 protected:
     bool eventFilter(QObject* object, QEvent* event) override;
-
 private slots:
     void setData(FrameGraphicsItem* rootItem);
     void setSearchValue(const QString& value);
index dedc79ed646473b31771656390e0b76996c8c006..846b0542902ee840815bac84b1279bb0a9ad2609 100644 (file)
@@ -247,7 +247,10 @@ void HistogramWidget::contextMenuEvent(QContextMenuEvent *event)
 
 void HistogramWidget::keyPressEvent(QKeyEvent *event)
 {
-    m_contextMenuQwt->handleKeyPress(event);
+    if (!m_contextMenuQwt->handleKeyPress(event))
+    {
+        QWidget::keyPressEvent(event);
+    }
 }
 
 void HistogramWidget::toggleShowTotal()
index 5affcde8e51c75b7def3d0398533781f6eed867f..4f8fcde830b296c27b47abaa141b766b2c372569 100644 (file)
@@ -621,6 +621,8 @@ MainWindow::MainWindow(QWidget* parent)
     connect(m_closeAction, &QAction::triggered, this, &MainWindow::close);
     m_quitAction = new QAction(i18n("&Quit"), this);
     connect(m_quitAction, &QAction::triggered, qApp, &QApplication::quit);
+
+    qApp->installEventFilter(this);
 #else
     m_openAction = KStandardAction::open(this, SLOT(closeFile()), this);
     m_openAction->setEnabled(false);
@@ -636,7 +638,9 @@ MainWindow::MainWindow(QWidget* parent)
 
 MainWindow::~MainWindow()
 {
-#ifndef NO_K_LIB
+#ifdef NO_K_LIB
+    qApp->removeEventFilter(this);
+#else
     auto state = saveState(MAINWINDOW_VERSION);
     auto group = m_config->group(Config::Groups::MainWindow);
     group.writeEntry(Config::Entries::State, state);
@@ -768,6 +772,25 @@ void MainWindow::closeEvent(QCloseEvent *event)
 #endif // QWT_FOUND
 }
 
+bool MainWindow::eventFilter(QObject* object, QEvent* event)
+{
+    // could process arrow keys (left/right) for flamegraph (to implement back/forward) only from here
+    if ((event->type() == QEvent::KeyPress) &&
+        (m_ui->tabWidget->currentWidget() == m_ui->flameGraphTab))
+    {
+        // Qt5: sometimes (e.g. if Alt is pressed) 'object' is QWidgetWindow which is not a part
+        // of Qt public API so trying to detect it indirectly (see 2nd condition below)
+        if ((object == this) || (object->parent() == nullptr))
+        {
+            if (m_ui->flameGraphTab->handleKeyPress(static_cast<QKeyEvent*>(event)))
+            {
+                return true;
+            }
+        }
+    }
+    return QMainWindow::eventFilter(object, event);
+}
+
 static void selectFile(QWidget *parent, QLineEdit *fileNameEdit)
 {
     QString fileName = QFileDialog::getOpenFileName(parent, "Select Data File",
index 63d87e734f182427ac705ed552628d56345d721d..5cd2254ba2772e7071ec107b9f8f7172a9e0f7a9 100644 (file)
@@ -54,6 +54,7 @@ signals:
 #ifdef NO_K_LIB
 protected:
     virtual void closeEvent(QCloseEvent *event) override;
+    virtual bool eventFilter(QObject* object, QEvent* event) override;
 public slots:
     void selectOpenFile();
     void selectCompareToFile();