Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 9efcec55 authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink

add a small example that shows how you can use the music rendering/engraving...

add a small example that shows how you can use the music rendering/engraving stuff in a simple qt application

svn path=/trunk/koffice/; revision=923128
parent 49538b35
add_subdirectory( fonts )
add_subdirectory( pics )
add_subdirectory( core )
add_subdirectory( tests)
include_directories( ${KOGUIUTILS_INCLUDES} )
......
include_directories( ${QT_QTGUI_INCLUDE_DIR} ${QT_QTCORE_INCLUDE_DIR} ${QT_INCLUDE_DIR} )
set( musicwidgettest_SRCS
MusicWidget.cpp
MusicWidgetTest.cpp
../core/Bar.cpp
../core/Chord.cpp
../core/Clef.cpp
../core/Global.cpp
../core/KeySignature.cpp
../core/Note.cpp
../core/Part.cpp
../core/PartGroup.cpp
../core/Sheet.cpp
../core/Staff.cpp
../core/StaffElement.cpp
../core/StaffSystem.cpp
../core/TimeSignature.cpp
../core/Voice.cpp
../core/VoiceBar.cpp
../core/VoiceElement.cpp
../Engraver.cpp
../Renderer.cpp
../MusicStyle.cpp
)
kde4_add_executable(musicwidgettest TEST ${musicwidgettest_SRCS})
target_link_libraries(musicwidgettest ${QT_QTGUI_LIBRARY})
/* This file is part of the KDE project
* Copyright (C) 2009 Marijn Kruisselbrink <m.kruisselbrink@student.tue.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "MusicWidget.h"
#include <QPainter>
#include "../core/Sheet.h"
MusicWidget::MusicWidget(QWidget* parent)
: QWidget(parent), m_renderer(&m_style), m_sheet(0), m_scale(1.0), m_lastSystem(0)
{
}
void MusicWidget::setSheet(MusicCore::Sheet* sheet)
{
m_sheet = sheet;
engrave();
}
MusicCore::Sheet* MusicWidget::sheet() const
{
return m_sheet;
}
void MusicWidget::setScale(qreal scale)
{
m_scale = scale;
engrave();
}
qreal MusicWidget::scale() const
{
return m_scale;
}
void MusicWidget::engrave()
{
if (m_sheet) {
m_engraver.engraveSheet(m_sheet, 0, QSizeF((width() - 1) / m_scale, height() / m_scale), true, &m_lastSystem);
}
}
void MusicWidget::paintEvent(QPaintEvent* event)
{
if (!m_sheet) return;
QPainter painter(this);
m_renderer.renderSheet(painter, m_sheet, 0, m_lastSystem);
}
void MusicWidget::resizeEvent(QResizeEvent* event)
{
engrave();
}
/* This file is part of the KDE project
* Copyright (C) 2009 Marijn Kruisselbrink <m.kruisselbrink@student.tue.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef MUSICWIDGET_H
#define MUSICWIDGET_H
#include <QWidget>
#include "../Engraver.h"
#include "../Renderer.h"
#include "../MusicStyle.h"
namespace MusicCore {
class Sheet;
}
class Engraver;
class MusicWidget : public QWidget {
Q_OBJECT
public:
explicit MusicWidget(QWidget* parent = 0);
/**
* Set the sheet that is displayed in this MusicWidget. The widget doesn't take ownership of the sheet,
* (unless it is already the parent QObject of it), but it is still probably not a good idea to have the
* same sheet in multiple widgets, as the layout of the sheet is stored in the sheet itself.
*/
void setSheet(MusicCore::Sheet* sheet);
/**
* Get the sheet that is currently displayed in this widget.
*/
MusicCore::Sheet* sheet() const;
/**
* Sets the scale at which the music should be painted in this widget.
*/
void setScale(qreal scale);
/**
* Gets the scale at which the music is painted in this widget.
*/
qreal scale() const;
public slots:
/**
* Call this method when the contents of the sheet have changed in such a way that a re-engraving
* of the music is required. This is done automatically if the widget changes size or scale.
*/
void engrave();
protected:
void paintEvent(QPaintEvent* event);
void resizeEvent(QResizeEvent* event);
private:
Engraver m_engraver;
MusicStyle m_style;
MusicRenderer m_renderer;
MusicCore::Sheet* m_sheet;
qreal m_scale;
int m_lastSystem;
};
#endif // MUSICWIDGET_H
/* This file is part of the KDE project
* Copyright (C) 2009 Marijn Kruisselbrink <m.kruisselbrink@student.tue.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QApplication>
#include <QFontDatabase>
#include <QDebug>
#include "MusicWidget.h"
#include "../core/Sheet.h"
#include "../core/Bar.h"
#include "../core/Part.h"
#include "../core/Clef.h"
#include "../core/Chord.h"
#include "../core/VoiceBar.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
// Load the font that is used by the music widget
QString fontFile = KDESRCDIR "/../fonts/Emmentaler-14.ttf";
if (QFontDatabase::addApplicationFont(fontFile) == -1) {
qWarning() << "Could not load emmentaler font";
}
MusicWidget w;
// Setup a simple sheet, with one part exising of one staff and one voice, and a single bar
MusicCore::Sheet* sheet = new MusicCore::Sheet(&w);
MusicCore::Bar* bar = sheet->addBar();
MusicCore::Part* part = sheet->addPart("Part 1");
MusicCore::Staff* staff = part->addStaff();
MusicCore::Voice* voice = part->addVoice();
// Add a clef to the staff
MusicCore::Clef* clef = new MusicCore::Clef(staff, 0, MusicCore::Clef::Trebble, 2, 0);
bar->addStaffElement(clef);
// And add some notes (middle C up-to C one octave higher)
for (int i = 0; i < 8; i++) {
MusicCore::Chord* chord = new MusicCore::Chord(MusicCore::HalfNote);
MusicCore::Note* note = chord->addNote(staff, i); // central C
bar->voice(voice)->addElement(chord);
}
w.setSheet(sheet);
w.show();
return app.exec();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment