Apollo 11.0
自动驾驶开放平台
fixedaspectratiowidget.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <QtGui/QContextMenuEvent>
20#include <QtGui/QPainter>
21#include <QtWidgets/QMenu>
22#include <QtWidgets/QStyleOption>
23
25 : QWidget(parent), index_(index), refresh_timer_(this), viewer_() {
26 viewer_.setParent(this);
27 viewer_.setGeometry(geometry());
28
29 refresh_timer_.setObjectName(tr("_refreshTimer"));
30 refresh_timer_.setInterval(40);
31 connect(&refresh_timer_, SIGNAL(timeout()), &viewer_, SLOT(repaint()));
32 setAutoFillBackground(true);
33}
34
36 if (viewer_.is_init_) {
37 if (b) {
38 refresh_timer_.start();
39 } else {
40 refresh_timer_.stop();
41 }
42 }
43}
44
46 const std::shared_ptr<Texture>& textureObj) {
47 if (textureObj == nullptr) {
48 viewer_.default_image_->setSizeChanged();
49 viewer_.plane_.set_texture(viewer_.default_image_);
50 } else {
51 viewer_.plane_.set_texture(textureObj);
52 }
53}
54
56 if (event->button() == Qt::LeftButton) {
57 emit focusOnThis(this);
58 QWidget::mouseDoubleClickEvent(event);
59 }
60}
61
62void FixedAspectRatioWidget::contextMenuEvent(QContextMenuEvent* event) {
63 emit focusOnThis(this);
64 QMenu m;
65 m.addActions(actions());
66 m.exec(event->globalPos());
67 m.clear();
68
69 QWidget::contextMenuEvent(event);
70}
71
72void FixedAspectRatioWidget::resizeEvent(QResizeEvent* revent) {
73 QSize size = revent->size();
74
75 {
76 double aspect = static_cast<double>(viewer_.plane_.texWidth()) /
77 static_cast<double>(viewer_.plane_.texHeight());
78
79 int wc = 4;
80 int hc = 9;
81 if (aspect == 4.0 / 3.0) {
82 wc = 2;
83 hc = 3;
84 } else if (aspect == 16.0 / 10.0) {
85 wc = 4;
86 hc = 10;
87 }
88
89 int w = size.width();
90 int h = size.height();
91
92 int tmpH = w >> wc;
93 w = tmpH << wc;
94 size.setWidth(w);
95 tmpH *= hc;
96 if (tmpH <= h) {
97 size.setHeight(tmpH);
98 } else {
99 tmpH = h / hc;
100 h = tmpH * hc;
101 size.setHeight(h);
102 size.setWidth(tmpH << wc);
103 }
104 }
105
106 viewer_.setGeometry((revent->size().width() - size.width()) / 2,
107 (revent->size().height() - size.height()) / 2,
108 size.width(), size.height());
109 QWidget::resizeEvent(revent);
110}
111
112void FixedAspectRatioWidget::paintEvent(QPaintEvent* event) {
113 QStyleOption opt;
114 opt.init(this);
115 QPainter p(this);
116 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
117
118 QWidget::paintEvent(event);
119}
void contextMenuEvent(QContextMenuEvent *) override
void resizeEvent(QResizeEvent *) override
FixedAspectRatioWidget(QWidget *parent=nullptr, int index=0)
void paintEvent(QPaintEvent *event) override
void SetupDynamicTexture(const std::shared_ptr< Texture > &textureObj)
void mouseDoubleClickEvent(QMouseEvent *event) override
void focusOnThis(FixedAspectRatioWidget *)
void set_texture(const std::shared_ptr< Texture > &t)
Definition plane.h:40
GLsizei texHeight(void) const
Definition plane.h:48
GLsizei texWidth(void) const
Definition plane.h:47