1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 #include "NoteItem.h"
19 
20 #include "NotePixmapFactory.h"
21 #include "misc/Debug.h"
22 #include "base/Profiler.h"
23 
24 #include <QPainter>
25 
26 namespace Rosegarden
27 {
28 
NoteItem(const NotePixmapParameters & params,QSharedPointer<NoteStyle> style,bool selected,bool shaded,NotePixmapFactory * factory,QGraphicsItem * parent)29 NoteItem::NoteItem(const NotePixmapParameters &params,
30                    QSharedPointer<NoteStyle> style,
31                    bool selected,
32                    bool shaded,
33 		   NotePixmapFactory *factory,
34 		   QGraphicsItem *parent) :
35     QGraphicsItem(parent),
36     m_parameters(params),
37     m_style(style),
38     m_selected(selected),
39     m_shaded(shaded),
40     m_factory(factory),
41     m_haveDimensions(false)
42 {
43 }
44 
~NoteItem()45 NoteItem::~NoteItem()
46 {
47 }
48 
49 QRectF
boundingRect() const50 NoteItem::boundingRect() const
51 {
52     if (!m_haveDimensions) {
53 	getDimensions();
54     }
55 
56     return QRectF(m_offset, m_size);
57 }
58 
59 void
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)60 NoteItem::paint(QPainter *painter,
61 		const QStyleOptionGraphicsItem */* option */,
62 		QWidget */* widget */)
63 {
64     if (!m_haveDimensions) {
65 	getDimensions();
66     }
67 
68     Profiler profiler("NoteItem::paint");
69 
70     QTransform t = painter->worldTransform();
71 
72 //    NOTATION_DEBUG << "note: transform " << t << " differs from last transform " << m_lastTransform << ", or is not a small transform";
73 
74     bool tiny = (t.m11() < 0.15 || t.m22() < 0.15);
75     if (!tiny) {
76         QRectF rect = boundingRect();
77         QRectF target = t.mapRect(rect);
78         tiny = (target.width() < 10 && target.height() < 10);
79     }
80 
81     DrawMode mode;
82     if (tiny) {
83         mode = DrawTiny;
84     } else if (t.m11() < 1.0) {
85         mode = DrawSmall;
86     } else if (t.m11() > 1.0) {
87         mode = DrawLarge;
88     } else {
89         mode = DrawNormal;
90     }
91 
92     painter->save();
93     if (mode == DrawLarge) {
94         painter->setRenderHint(QPainter::Antialiasing, true);
95     } else {
96         painter->setRenderHint(QPainter::Antialiasing, false);
97     }
98     m_factory->setNoteStyle(m_style);
99     m_factory->setSelected(m_selected);
100     m_factory->setShaded(m_shaded);
101     m_factory->drawNoteForItem(m_parameters, m_dimensions, mode, painter);
102     painter->restore();
103 }
104 
105 void
getDimensions() const106 NoteItem::getDimensions() const
107 {
108     Profiler profiler("NoteItem::getDimensions");
109 
110     m_factory->getNoteDimensions(m_parameters, m_dimensions);
111     m_offset = QPoint(-m_dimensions.left,
112                       -m_dimensions.above - m_dimensions.noteBodyHeight / 2);
113     m_size = QSize(m_dimensions.noteBodyWidth + m_dimensions.left + m_dimensions.right,
114                    m_dimensions.noteBodyHeight + m_dimensions.above + m_dimensions.below);
115     m_haveDimensions = true;
116 }
117 
118 QPointF
offset() const119 NoteItem::offset() const
120 {
121     if (!m_haveDimensions) {
122 	getDimensions();
123     }
124 
125     return m_offset;
126 }
127 
128 QPixmap
makePixmap() const129 NoteItem::makePixmap() const
130 {
131     m_factory->setNoteStyle(m_style);
132     m_factory->setSelected(m_selected);
133     m_factory->setShaded(m_shaded);
134 
135     QGraphicsPixmapItem *pi = m_factory->makeNotePixmapItem(m_parameters);
136     QPixmap pixmap = pi->pixmap();
137     delete pi;
138 
139     return pixmap;
140 }
141 
142 
143 }
144 
145