MuseScore/awl/slider.cpp

252 lines
7.5 KiB
C++
Raw Normal View History

2012-05-26 14:49:10 +02:00
//=============================================================================
// Awl
// Audio Widget Library
//
// Copyright (C) 2002-2006 by Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
#include "slider.h"
namespace Awl {
//---------------------------------------------------------
// Slider
//---------------------------------------------------------
Slider::Slider(QWidget* parent)
: AbstractSlider(parent), orient(Qt::Vertical), _sliderSize(14,14)
{
init();
}
//---------------------------------------------------------
// Slider
//---------------------------------------------------------
Slider::Slider(Qt::Orientation orientation, QWidget* parent)
: AbstractSlider(parent), orient(orientation), _sliderSize(14,14)
{
init();
}
//---------------------------------------------------------
// Slider
//---------------------------------------------------------
void Slider::init()
{
if (orient == Qt::Vertical)
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
else
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
dragMode = false;
points = 0;
updateKnob();
}
//---------------------------------------------------------
// sizeHint
//---------------------------------------------------------
QSize Slider::sizeHint() const
{
int w = _sliderSize.width() + scaleWidth();
return orient == Qt::Vertical ? QSize(w, 200) : QSize(200, w);
}
//---------------------------------------------------------
// Slider
//---------------------------------------------------------
Slider::~Slider()
{
if (points)
delete points;
}
//---------------------------------------------------------
// setOrientation
//---------------------------------------------------------
void Slider::setOrientation(Qt::Orientation o)
{
orient = o;
updateKnob();
update();
}
//---------------------------------------------------------
// updateKnob
//---------------------------------------------------------
void Slider::updateKnob()
{
if (points)
delete points;
points = new QPainterPath;
int kh = _sliderSize.height();
int kw = _sliderSize.width();
points->moveTo(0.0, 0.0);
if (orient == Qt::Vertical) {
This commit contains changes required for MuseScore to compile under MSVC with no warnings. This commit contains changes required for MuseScore to compile under MSVC with no warnings. MuseScore is being compiled with the /W4 setting (warning level 4), which is similar to -wall -wextra on clang. This generates lots of warnings on MSVC, mainly for non-standard constructs and for constructs which might be bugs or might lead to bugs. Most warnings are in the following categories: - Name hiding: a variable hides a variable with the same name on a larger scope (or a field, or a function parameter). This can easily lead to bugs, and it is a best practice to avoid hiding variable names (see recommendation ES.12 in the C++ Core Guidelines by Stroustrop & Sutter (http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-reuse : ES.12: Do not reuse names in nested scopes) - Narrowing conversion: a numeric conversion results in loss of significant digits (for example, double -> float). The general recommendation is to use a cast to indicate this is designed behaviour. - Unreachable code: in several instances, there is unreachable code. The unreachable code is commented out. - (Potentially) uninitialized local variable. Just initialized the vars. - foreach(,) -> for(:): this does not generate a warning per-se (only a few of these generate warnings due to name hiding), but changed in keeping with "MuseScore Coding Rules" (https://musescore.org/en/handbook/musescore-coding-rules#Loops), which tells explicitly "Use C++11's "for" instead of Qt's "foreach":" ... "If you happen to be fixing some code and see a "foreach", please change that loop into a "for"." Most changes are in the categories indicated above. The next listing shows detailed changes for files which are *not* of the aforementioned types. - all.h: Disable warning C4127 (conditional expression is constant - generated in Qt header file qvector.h) - awl/aslider.h: unreachable code. - awl/knob.cpp: name hiding - awl/mslider.cpp: name hiding - awl/slider.cpp: name hiding - bww2mxml/parser.cpp: name hiding - effects/compressor/compressor.cpp: narrowing conversion - effects/zita1/zitagui.cpp: name hiding - fluid/fluid.cpp: foreach replacement. Name hiding. - fluid/mod.cpp: name hiding. - fluid/sfont.cpp: foreach replacement. Name hiding. Initialize vars. - fluid/voice.cpp: Name hiding. - libmscore/accidental.cpp: Name hiding. - libmscore/ambitus.cpp: Initialize vars. - libmscore/barline.cpp: Name hiding. Unreachable code. - libmscore/beam.cpp: Name hiding. - libmscore/chordrest.cpp: Unreachable code. - libmscore/scorefile.cpp: Name hiding. - manual/genManual.cpp: Name hiding. foreach replacement. - midi/midifile.cpp: Name hiding. Unreachable code. - omr/importpdf.cpp: Name hiding. foreach replacement. - omr/omr.cpp: Name hiding. foreach replacement. - omr/omrpage.cpp: Name hiding. foreach replacement. - omr/omrview.cpp: Name hiding. foreach replacement. - synthesizer/event.cpp: Unreachable code. - zerberus\channel.cpp: Narrowing conversion. - zerberus\instrument.cpp: Name hiding. - zerberus\sfz.cpp: Name hiding. - zerberus\voice.h: Suppress warning C4201: "nonstandard extension used: nameless struct/union" - zerberus\zerberus.cpp: Name hiding. Unreferenced parameter. - zerberus\zerberusgui.cpp: Name hiding.
2018-05-03 03:04:08 +02:00
int kh1 = _sliderSize.height();
int kh2 = kh1 / 2;
2012-05-26 14:49:10 +02:00
points->lineTo(kw, -kh2);
points->lineTo(kw, kh2);
}
else {
int kw2 = kw/2;
points->lineTo(-kw2, kh);
points->lineTo(kw2, kh);
}
points->lineTo(0.0, 0.0);
}
//---------------------------------------------------------
// setInvertedAppearance
//---------------------------------------------------------
void Slider::setInvertedAppearance(bool val)
{
AbstractSlider::setInvertedAppearance(val);
update();
}
//---------------------------------------------------------
// setSliderSize
//---------------------------------------------------------
void Slider::setSliderSize(const QSize& s)
{
_sliderSize = s;
update();
}
//---------------------------------------------------------
// mousePressEvent
//---------------------------------------------------------
void Slider::mousePressEvent(QMouseEvent* ev)
{
startDrag = ev->pos();
// if (points->boundingRect().toRect().contains(startDrag)) {
emit sliderPressed(__id);
2012-05-26 14:49:10 +02:00
dragMode = true;
int pixel = (orient == Qt::Vertical) ? height() - _sliderSize.height() : width() - _sliderSize.width();
dragppos = int(pixel * (_value - minValue()) / (maxValue() - minValue()));
if (_invert)
dragppos = pixel - dragppos;
// }
}
//---------------------------------------------------------
// mouseReleaseEvent
//---------------------------------------------------------
void Slider::mouseReleaseEvent(QMouseEvent*)
{
if (dragMode) {
emit sliderReleased(__id);
2012-05-26 14:49:10 +02:00
dragMode = false;
}
}
//---------------------------------------------------------
// mouseMoveEvent
//---------------------------------------------------------
void Slider::mouseMoveEvent(QMouseEvent* ev)
{
if (!dragMode)
return;
int delta = orient == Qt::Horizontal ? (startDrag.x() - ev->x()) : (startDrag.y() - ev->y());
// if (_invert)
// delta = -delta;
if (orient == Qt::Horizontal)
delta = -delta;
int ppos = dragppos + delta;
if (ppos < 0)
ppos = 0;
int pixel = (orient == Qt::Vertical) ? height() - _sliderSize.height() : width() - _sliderSize.width();
if (ppos > pixel)
ppos = pixel;
int pos = _invert ? (pixel - ppos) : ppos;
_value = (pos * (maxValue() - minValue()) / pixel) + minValue() - 0.000001;
valueChange();
}
//---------------------------------------------------------
// paint
// r - phys coord system
//---------------------------------------------------------
void Slider::paintEvent(QPaintEvent* /*ev*/)
2012-05-26 14:49:10 +02:00
{
int h = height();
int w = width();
int kw = _sliderSize.width();
int kh = _sliderSize.height();
int pixel = (orient == Qt::Vertical) ? h - kh : w - kw;
double range = maxValue() - minValue();
int ppos = int(pixel * (_value - minValue()) / range);
if ((orient == Qt::Vertical && _invert) || (orient == Qt::Horizontal && !_invert))
ppos = pixel - ppos;
2014-06-17 14:35:16 +02:00
#if 0 // yet(?) unused
2012-05-26 14:49:10 +02:00
QRect rr(ev->rect());
2014-06-17 14:35:16 +02:00
#endif
2012-05-26 14:49:10 +02:00
QPainter p(this);
QColor sc(isEnabled() ? _scaleColor : Qt::gray);
QColor svc(isEnabled() ? _scaleValueColor : Qt::gray);
p.setBrush(svc);
int kh2 = kh/2;
//---------------------------------------------------
// draw scale
//---------------------------------------------------
if (orient == Qt::Vertical) {
int xm = (w - _scaleWidth - _sliderSize.height()) / 2;
int y1 = kh2;
int y2 = h - (ppos + y1);
int y3 = h - y1;
p.fillRect(xm, y1, _scaleWidth, y2-y1, _invert ? svc : sc);
p.fillRect(xm, y2, _scaleWidth, y3-y2, _invert ? sc : svc);
p.translate(QPointF(xm + _scaleWidth/2, y2));
}
else {
int ym = (h - _scaleWidth - _sliderSize.height()) / 2;
int x1 = kh2;
int x2 = w - (ppos + x1);
int x3 = w - x1;
p.fillRect(x1, ym, x2-x1, _scaleWidth, _invert ? sc : svc);
p.fillRect(x2, ym, x3-x2, _scaleWidth, _invert ? svc : sc);
p.translate(QPointF(x2, ym + _scaleWidth/2));
}
//---------------------------------------------------
// draw slider
//---------------------------------------------------
p.setRenderHint(QPainter::Antialiasing, true);
p.setPen(QPen(svc, 0));
p.drawPath(*points);
}
}