]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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 | ||
24 | #include "signal.h" | |
25 | #include "../sigsession.h" | |
26 | ||
27 | #include <assert.h> | |
28 | ||
29 | #include <boost/foreach.hpp> | |
30 | ||
31 | #include <QColorDialog> | |
32 | #include <QInputDialog> | |
33 | #include <QMenu> | |
34 | #include <QMouseEvent> | |
35 | #include <QPainter> | |
36 | #include <QRect> | |
37 | ||
38 | using namespace boost; | |
39 | using namespace std; | |
40 | ||
41 | namespace pv { | |
42 | namespace view { | |
43 | ||
44 | Header::Header(View &parent) : | |
45 | QWidget(&parent), | |
46 | _view(parent), | |
47 | _action_set_name(new QAction(tr("Set &Name..."), this)), | |
48 | _action_set_colour(new QAction(tr("Set &Colour..."), this)) | |
49 | { | |
50 | setMouseTracking(true); | |
51 | ||
52 | connect(_action_set_name, SIGNAL(triggered()), | |
53 | this, SLOT(on_action_set_name_triggered())); | |
54 | connect(_action_set_colour, SIGNAL(triggered()), | |
55 | this, SLOT(on_action_set_colour_triggered())); | |
56 | } | |
57 | ||
58 | void 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 | ||
67 | int offset = -_view.v_offset(); | |
68 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) | |
69 | { | |
70 | assert(s); | |
71 | ||
72 | const QRect signal_heading_rect( | |
73 | 0, offset, w, View::SignalHeight); | |
74 | ||
75 | s->paint_label(painter, signal_heading_rect, | |
76 | s->pt_in_label_rect(signal_heading_rect, _mouse_point)); | |
77 | ||
78 | offset += View::SignalHeight; | |
79 | } | |
80 | ||
81 | painter.end(); | |
82 | } | |
83 | ||
84 | void Header::mouseMoveEvent(QMouseEvent *event) | |
85 | { | |
86 | assert(event); | |
87 | _mouse_point = event->pos(); | |
88 | update(); | |
89 | } | |
90 | ||
91 | void Header::leaveEvent(QEvent *event) | |
92 | { | |
93 | _mouse_point = QPoint(-1, -1); | |
94 | update(); | |
95 | } | |
96 | ||
97 | void Header::contextMenuEvent(QContextMenuEvent *event) | |
98 | { | |
99 | const int w = width(); | |
100 | const vector< shared_ptr<Signal> > &sigs = | |
101 | _view.session().get_signals(); | |
102 | ||
103 | int offset = -_view.v_offset(); | |
104 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) | |
105 | { | |
106 | assert(s); | |
107 | ||
108 | const QRect signal_heading_rect( | |
109 | 0, offset, w, View::SignalHeight); | |
110 | ||
111 | if(s->pt_in_label_rect(signal_heading_rect, _mouse_point)) { | |
112 | QMenu menu(this); | |
113 | menu.addAction(_action_set_name); | |
114 | menu.addAction(_action_set_colour); | |
115 | ||
116 | _context_signal = s; | |
117 | menu.exec(event->globalPos()); | |
118 | _context_signal.reset(); | |
119 | ||
120 | break; | |
121 | } | |
122 | ||
123 | offset += View::SignalHeight; | |
124 | } | |
125 | } | |
126 | ||
127 | void Header::on_action_set_name_triggered() | |
128 | { | |
129 | shared_ptr<view::Signal> context_signal = _context_signal; | |
130 | if(!context_signal) | |
131 | return; | |
132 | ||
133 | const QString new_label = QInputDialog::getText(this, tr("Set Name"), | |
134 | tr("Name"), QLineEdit::Normal, context_signal->get_name()); | |
135 | ||
136 | if(!new_label.isEmpty()) | |
137 | context_signal->set_name(new_label); | |
138 | } | |
139 | ||
140 | void Header::on_action_set_colour_triggered() | |
141 | { | |
142 | shared_ptr<view::Signal> context_signal = _context_signal; | |
143 | if(!context_signal) | |
144 | return; | |
145 | ||
146 | const QColor new_colour = QColorDialog::getColor( | |
147 | context_signal->get_colour(), this, tr("Set Colour")); | |
148 | ||
149 | if(new_colour.isValid()) | |
150 | context_signal->set_colour(new_colour); | |
151 | } | |
152 | ||
153 | } // namespace view | |
154 | } // namespace pv |