File: extendedtabwidget.cpp

package info (click to toggle)
mcrl2 201409.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd
  • size: 46,348 kB
  • ctags: 29,960
  • sloc: cpp: 213,160; ansic: 16,219; python: 13,238; yacc: 309; lex: 214; xml: 197; makefile: 83; sh: 82; pascal: 17
file content (106 lines) | stat: -rwxr-xr-x 2,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Author(s): Rimco Boudewijns
// Copyright: see the accompanying file COPYING or copy at
// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#include "mcrl2/utilities/extendedtabwidget.h"
#include <QTabBar>
#include <QEvent>
#include <QMouseEvent>
#include <QMenu>
#include <QAction>

using namespace mcrl2::utilities::qt;

ExtendedTabWidget::ExtendedTabWidget(QWidget *parent) :
  QTabWidget(parent)
{
  setFocusPolicy(Qt::StrongFocus);
  tabBar()->installEventFilter(this);
}

bool ExtendedTabWidget::eventFilter(QObject *target, QEvent *event) {
  if(target == tabBar()) {
    if(event->type() == QEvent::MouseButtonPress) {
      QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
      if (mouseEvent->button() == Qt::RightButton)
      {
        QPoint position = mouseEvent->pos();
        int clickedItem = -1;
        for (int i=0; i < tabBar()->count(); i++)
        {
          if ( tabBar()->tabRect(i).contains( position ) )
          {
            clickedItem = i;
            break;
          }
        }
        if (clickedItem != -1)
        {
          QMenu menu(this);
          menu.addAction("Close tab");
          menu.addSeparator();
          menu.addAction("Close all tabs");
          menu.addAction("Close other tabs");
          menu.addAction("Close tabs on the left");
          menu.addAction("Close tabs on the right");
          QAction* act = menu.exec(mapToGlobal(position));
          if (act != 0)
          {
            if (act->text() == "Close tab")
            {
              emit(tabCloseRequested(clickedItem));
            }
            if (act->text() == "Close all tabs")
            {
              for (int i = count()-1; i >= 0; i--)
              {
                  emit(tabCloseRequested(i));
              }
            }
            if (act->text() == "Close other tabs")
            {
              for (int i = count()-1; i >= 0; i--)
              {
                if (i != clickedItem)
                  emit(tabCloseRequested(i));
              }
            }
            if (act->text() == "Close tabs on the left")
            {
              for (int i = clickedItem-1; i >= 0; i--)
              {
                emit(tabCloseRequested(i));
              }
            }
            if (act->text() == "Close tabs on the right")
            {
              for (int i = count()-1; i > clickedItem; i--)
              {
                emit(tabCloseRequested(i));
              }
            }
          }
          return true;
        }
      }
    }
  }
  return false;
}

void ExtendedTabWidget::keyPressEvent(QKeyEvent *event)
{
  if (event->matches(QKeySequence::Close) && currentIndex() != -1)
  {
    emit(tabCloseRequested(currentIndex()));
  }
  else
  {
    QTabWidget::keyPressEvent(event);
  }
}