]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Use static signal offsets instead of offsets calculated on-the-fly
[pulseview.git] / pv / view / header.cpp
CommitLineData
1d8dca91 1/*
b3f22de0 2 * This file is part of the PulseView project.
1d8dca91
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "header.h"
22#include "view.h"
23
8d634081 24#include "signal.h"
51e77110 25#include "../sigsession.h"
1d8dca91
JH
26
27#include <assert.h>
28
29#include <boost/foreach.hpp>
30
b3b57abc 31#include <QColorDialog>
49f8ff3f
JH
32#include <QInputDialog>
33#include <QMenu>
a29bb7fb 34#include <QMouseEvent>
1d8dca91
JH
35#include <QPainter>
36#include <QRect>
37
38using namespace boost;
39using namespace std;
40
41namespace pv {
42namespace view {
43
44Header::Header(View &parent) :
45 QWidget(&parent),
49f8ff3f 46 _view(parent),
b3b57abc
JH
47 _action_set_name(new QAction(tr("Set &Name..."), this)),
48 _action_set_colour(new QAction(tr("Set &Colour..."), this))
1d8dca91 49{
a29bb7fb 50 setMouseTracking(true);
49f8ff3f
JH
51
52 connect(_action_set_name, SIGNAL(triggered()),
53 this, SLOT(on_action_set_name_triggered()));
b3b57abc
JH
54 connect(_action_set_colour, SIGNAL(triggered()),
55 this, SLOT(on_action_set_colour_triggered()));
1d8dca91
JH
56}
57
58void Header::paintEvent(QPaintEvent *event)
59{
60 const int w = width();
61 const vector< shared_ptr<Signal> > &sigs =
62 _view.session().get_signals();
63
64 QPainter painter(this);
65 painter.setRenderHint(QPainter::Antialiasing);
66
2e575351 67 const int v_offset = _view.v_offset();
1d8dca91
JH
68 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
69 {
70 assert(s);
71
a29bb7fb 72 const QRect signal_heading_rect(
2e575351
JH
73 0, s->get_v_offset() - v_offset,
74 w, View::SignalHeight);
a29bb7fb
JH
75
76 s->paint_label(painter, signal_heading_rect,
ed6f0f4f 77 s->pt_in_label_rect(signal_heading_rect, _mouse_point));
1d8dca91
JH
78 }
79
80 painter.end();
81}
82
a29bb7fb
JH
83void Header::mouseMoveEvent(QMouseEvent *event)
84{
85 assert(event);
86 _mouse_point = event->pos();
87 update();
88}
89
90void Header::leaveEvent(QEvent *event)
91{
92 _mouse_point = QPoint(-1, -1);
93 update();
94}
95
49f8ff3f
JH
96void Header::contextMenuEvent(QContextMenuEvent *event)
97{
98 const int w = width();
99 const vector< shared_ptr<Signal> > &sigs =
100 _view.session().get_signals();
101
2e575351 102 const int v_offset = _view.v_offset();
49f8ff3f
JH
103 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
104 {
105 assert(s);
106
107 const QRect signal_heading_rect(
2e575351
JH
108 0, s->get_v_offset() - v_offset,
109 w, View::SignalHeight);
49f8ff3f
JH
110
111 if(s->pt_in_label_rect(signal_heading_rect, _mouse_point)) {
112 QMenu menu(this);
113 menu.addAction(_action_set_name);
b3b57abc 114 menu.addAction(_action_set_colour);
49f8ff3f
JH
115
116 _context_signal = s;
117 menu.exec(event->globalPos());
118 _context_signal.reset();
119
120 break;
121 }
49f8ff3f
JH
122 }
123}
124
125void Header::on_action_set_name_triggered()
126{
8d634081 127 shared_ptr<view::Signal> context_signal = _context_signal;
49f8ff3f
JH
128 if(!context_signal)
129 return;
130
131 const QString new_label = QInputDialog::getText(this, tr("Set Name"),
132 tr("Name"), QLineEdit::Normal, context_signal->get_name());
133
134 if(!new_label.isEmpty())
135 context_signal->set_name(new_label);
136}
137
b3b57abc
JH
138void Header::on_action_set_colour_triggered()
139{
8d634081 140 shared_ptr<view::Signal> context_signal = _context_signal;
b3b57abc
JH
141 if(!context_signal)
142 return;
143
144 const QColor new_colour = QColorDialog::getColor(
145 context_signal->get_colour(), this, tr("Set Colour"));
146
147 if(new_colour.isValid())
148 context_signal->set_colour(new_colour);
149}
150
1d8dca91
JH
151} // namespace view
152} // namespace pv