X-Git-Url: http://git.johnwright.org/?p=fmit.git;a=blobdiff_plain;f=src%2Fmodules%2FMicrotonalView.cpp;fp=src%2Fmodules%2FMicrotonalView.cpp;h=753f670c6180472aa7b48d92ab4493841b0598b2;hp=7f4935e80fb16755197b4df02ae5bcf7b5020dbc;hb=a8fb82288feb6b258f2c04312f0c36ba8d79b494;hpb=adfba0402927438a1869f1e716bcf0e25574ce1e diff --git a/src/modules/MicrotonalView.cpp b/src/modules/MicrotonalView.cpp index 7f4935e..753f670 100644 --- a/src/modules/MicrotonalView.cpp +++ b/src/modules/MicrotonalView.cpp @@ -156,10 +156,16 @@ static const unsigned char g_icon_ji[] = { using namespace std; #include #include +#ifdef QT3_SUPPORT +#include +#else #include +#endif #include #include #include +#include +#include #include using namespace Music; @@ -167,7 +173,7 @@ using namespace Music; void MScale::load_scala(const QString& file_name) { - ifstream file(file_name.latin1()); + ifstream file(file_name.toAscii().constData()); if(!file.is_open()) throw MicrotonalView::tr("Unkown filename: '")+file_name+"'"; @@ -186,7 +192,7 @@ void MScale::load_scala(const QString& file_name) m_name = QString(buff); m_name = m_name.remove("\r"); m_name = m_name.remove("\n"); - m_name = m_name.simplifyWhiteSpace(); + m_name = m_name.simplified(); lines_red++; } else if(lines_red==1) @@ -249,7 +255,7 @@ bool MScale::operator != (const MScale& scale) // ---------------------- QRoot button ------------------------- QRoot::QRoot(MicrotonalView* view, int ht) -: QPushButton(view, "QRoot:"+QString::number(ht)) +: QPushButton(view) , m_ht(ht) { // cerr << "QRoot::QRoot " << m_ht << endl; @@ -259,11 +265,11 @@ QRoot::QRoot(MicrotonalView* view, int ht) connect(this, SIGNAL(clicked()), this, SLOT(clicked2())); connect(this, SIGNAL(rootClicked(int)), view, SLOT(selectRoot(int))); setFlat(true); - setText(h2n(ht, GetNotesName(), GetTonality(), false)); - setToggleButton(true); + setText(QString::fromStdString(h2n(ht, GetNotesName(), GetTonality(), false))); + setCheckable(true); setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); setFixedWidth(35); // TODO - setFocusPolicy(QPushButton::NoFocus); + setFocusPolicy(Qt::NoFocus); } void QRoot::clicked2() { @@ -273,46 +279,63 @@ void QRoot::clicked2() // --------------------------- MicrotonalView ---------------------------- MicrotonalView::MicrotonalView(QWidget* parent) -: QFrame(parent, "MicrotonalView") -, View("Microtonal", this) -, ui_scale_menu(this) +: QFrame(parent) +, View(tr("Microtonal"), this) +, ui_scale_menu(tr("Used scale"), this) { setting_selectedRoot = -1000; m_AFreq = 440.0f; m_selected_jivalue = NULL; m_tuningFreq = 0.0; + setting_selectedScale = NULL; // settings - QImage img; + QPixmap img; img.loadFromData(g_icon_ji, sizeof(g_icon_ji), "PNG"); - setting_show->setIconSet(QIconSet(QImage(img))); - setting_show->setOn(false); + setting_show->setIcon(QIcon(img)); + setting_show->setChecked(false); hide(); - m_popup_menu.insertItem(new Title(tr("Root's octave"), &m_popup_menu)); - setting_octave = new QSpinBox(-3, 8, 1, &m_popup_menu); - QToolTip::add(setting_octave, tr("Root's octave")); + QHBoxLayout* octaveActionLayout = new QHBoxLayout(&m_popup_menu); + + QLabel* octaveActionTitle = new QLabel(tr("Root's octave"), &m_popup_menu); + octaveActionLayout->addWidget(octaveActionTitle); + + setting_octave = new QSpinBox(&m_popup_menu); + setting_octave->setMinimum(-3); + setting_octave->setMaximum(8); + setting_octave->setSingleStep(1); + setting_octave->setToolTip(tr("Root's octave")); setting_octave->setValue(3); // m_old_octave = setting_octave->value(); // connect(setting_octave, SIGNAL(valueChanged(int)), this, SLOT(octaveChanged(int))); connect(setting_octave, SIGNAL(valueChanged(int)), this, SLOT(emitTuningFreqChanged())); - m_popup_menu.insertItem(setting_octave); + octaveActionLayout->addWidget(setting_octave); + + QWidget* octaveActionWidget = new QWidget(&m_popup_menu); + octaveActionWidget->setLayout(octaveActionLayout); + + QWidgetAction* octaveAction = new QWidgetAction(&m_popup_menu); + octaveAction->setDefaultWidget(octaveActionWidget); + m_popup_menu.addAction(octaveAction); - setting_keepRootToLeft = new QAction(this); - setting_keepRootToLeft->setMenuText(tr("Keep root to left side")); - setting_keepRootToLeft->setToggleAction(true); + setting_keepRootToLeft = new QAction(tr("Keep root to left side"), this); + setting_keepRootToLeft->setCheckable(true); connect(setting_keepRootToLeft, SIGNAL(toggled(bool)), this, SLOT(keepRootToLeft(bool))); - setting_keepRootToLeft->addTo(&m_popup_menu); + m_popup_menu.addAction(setting_keepRootToLeft); - setting_loadScale = new QAction(this); - setting_loadScale->setMenuText(tr("Load Scala file ...")); - setting_loadScale->addTo(&m_popup_menu); + setting_loadScale = new QAction(tr("Load Scala file ..."), this); + m_popup_menu.addAction(setting_loadScale); connect(setting_loadScale, SIGNAL(activated()), this, SLOT(loadScale())); - ui_scale_menu.setCheckable(true); - m_popup_menu.insertItem(tr("used scale"), &ui_scale_menu); - QVBoxLayout* layout = new QVBoxLayout(this, 0, 0, "MicrotonalView:layout"); - QHBoxLayout* roots_layout = new QHBoxLayout(layout, 0, "MicrotonalView:roots_layout"); + m_popup_menu.addMenu(&ui_scale_menu); + + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setMargin(0); + layout->setSpacing(0); + QHBoxLayout* roots_layout = new QHBoxLayout(); + roots_layout->setSpacing(0); + layout->addLayout(roots_layout); for(int i=0; i<13; i++) { m_roots.push_back(new QRoot(this, i+3)); @@ -337,22 +360,22 @@ MicrotonalView::MicrotonalView(QWidget* parent) } void MicrotonalView::save() { - s_settings->writeEntry("octave", setting_octave->value()); - s_settings->writeEntry("keepRootToLeft", setting_keepRootToLeft->isOn()); + s_settings->setValue("octave", setting_octave->value()); + s_settings->setValue("keepRootToLeft", setting_keepRootToLeft->isChecked()); QStringList scale_list; for(size_t i=0; igetPath().isEmpty()) scale_list.push_back(setting_scales[i]->getPath()); - s_settings->writeEntry("scales", scale_list); - s_settings->writeEntry("selectedScale", setting_selectedScale->getName()); - s_settings->writeEntry("lastScalesDirectory", setting_lastScalesDirectory); - s_settings->writeEntry("selectedRoot", setting_selectedRoot); + s_settings->setValue("scales", scale_list); + s_settings->setValue("selectedScale", setting_selectedScale->getName()); + s_settings->setValue("lastScalesDirectory", setting_lastScalesDirectory); + s_settings->setValue("selectedRoot", setting_selectedRoot); } void MicrotonalView::load() { - setting_octave->setValue(s_settings->readNumEntry("octave", setting_octave->value())); - setting_keepRootToLeft->setOn(s_settings->readBoolEntry("keepRootToLeft", setting_keepRootToLeft->isOn())); - QStringList scale_list = s_settings->readListEntry("scales"); + setting_octave->setValue(s_settings->value("octave", setting_octave->value()).toInt()); + setting_keepRootToLeft->setChecked(s_settings->value("keepRootToLeft", setting_keepRootToLeft->isChecked()).toBool()); + QStringList scale_list = s_settings->value("scales").toStringList(); for(QStringList::iterator it=scale_list.begin(); it!=scale_list.end(); ++it) { try @@ -360,22 +383,22 @@ void MicrotonalView::load() MScale* scale = new MScale(*it, MScale::SCALA); setting_scales.push_back(scale); } - catch(QString error){cerr << "MicrotonalView::load " << error << endl;} + catch(QString error){cerr << "MicrotonalView::load " << error.toStdString() << endl;} } refreshScaleList(); - selectScale(s_settings->readEntry("selectedScale", "default")); - setting_lastScalesDirectory = s_settings->readEntry("lastScalesDirectory", "."); - selectRoot(s_settings->readNumEntry("selectedRoot", m_roots[0]->m_ht)); + selectScale(s_settings->value("selectedScale", "default").toString()); + setting_lastScalesDirectory = s_settings->value("lastScalesDirectory", ".").toString(); + selectRoot(s_settings->value("selectedRoot", m_roots[0]->m_ht).toInt()); } void MicrotonalView::clearSettings() { - s_settings->removeEntry("octave"); - s_settings->removeEntry("keepRootToLeft"); - s_settings->removeEntry("scales"); - s_settings->removeEntry("selectedScale"); - s_settings->removeEntry("lastScalesDirectory"); - s_settings->removeEntry("selectedRoot"); + s_settings->remove("octave"); + s_settings->remove("keepRootToLeft"); + s_settings->remove("scales"); + s_settings->remove("selectedScale"); + s_settings->remove("lastScalesDirectory"); + s_settings->remove("selectedRoot"); } /*void MicrotonalView::octaveChanged(int value) @@ -407,23 +430,35 @@ QString MicrotonalView::getTuningNoteName() if(hasTuningFreqSelected()) { if(m_selected_jivalue->is_ratio) - return QString(h2n(setting_selectedRoot+getOctaveShift()))+":"+QString::number(m_selected_jivalue->num)+"/"+QString::number(m_selected_jivalue->den); + return QString::fromStdString(h2n(setting_selectedRoot+getOctaveShift()))+":"+QString::number(m_selected_jivalue->num)+"/"+QString::number(m_selected_jivalue->den); else - return QString(h2n(setting_selectedRoot+getOctaveShift()))+":"+QString::number(m_selected_jivalue->cents); + return QString::fromStdString(h2n(setting_selectedRoot+getOctaveShift()))+":"+QString::number(m_selected_jivalue->cents); } return "no tuning note selected"; } +void MicrotonalView::scaleActionToggled(bool checked) +{ + if (checked) + selectScale(((QAction*)sender())->data().toInt()); +} + void MicrotonalView::selectScale(int index) { assert(index>=0 && index scaleActions = ui_scale_menu.actions(); + + for (int i = 0; i < scaleActions.size(); i++) + { + if (i == index) + scaleActions[i]->setChecked(true); + else + scaleActions[i]->setChecked(false); + } ui_ratios->repaint(); } @@ -441,13 +476,19 @@ void MicrotonalView::selectScale(const QString& name) if(index==-1) { - cerr << "MicrotonalView::selectScale unknown scale '" << name << "'" << endl; + cerr << "MicrotonalView::selectScale unknown scale '" << name.toStdString() << "'" << endl; return; } - for(size_t i=0; i scaleActions = ui_scale_menu.actions(); + + for (int i = 0; i < scaleActions.size(); i++) + { + if (i == index) + scaleActions[i]->setChecked(true); + else + scaleActions[i]->setChecked(false); + } ui_ratios->repaint(); } @@ -463,7 +504,7 @@ int MicrotonalView::getIndex(MScale* scale) void MicrotonalView::notesNameChanged() { for(int i=0; i<13; i++) - m_roots[i]->setText(h2n(m_roots[i]->m_ht, GetNotesName(), GetTonality(), false)); + m_roots[i]->setText(QString::fromStdString(h2n(m_roots[i]->m_ht, GetNotesName(), GetTonality(), false))); } void MicrotonalView::keepRootToLeft(bool keep) @@ -504,11 +545,11 @@ void MicrotonalView::selectRoot(int ht) if(ht==setting_selectedRoot) { for(int i=0; i<13; i++) - m_roots[i]->setOn(m_roots[i]->m_ht==ht); + m_roots[i]->setChecked(m_roots[i]->m_ht==ht); } else { - if(setting_keepRootToLeft->isOn()) + if(setting_keepRootToLeft->isChecked()) { for(int i=0; i<13; i++) m_roots[i]->m_ht = (ht+i)%12; @@ -516,7 +557,7 @@ void MicrotonalView::selectRoot(int ht) } for(int i=0; i<13; i++) - m_roots[i]->setOn(m_roots[i]->m_ht==ht); + m_roots[i]->setChecked(m_roots[i]->m_ht==ht); setting_selectedRoot = ht; @@ -539,14 +580,18 @@ void MicrotonalView::emitTuningFreqChanged() } MicrotonalView::QScaleLabel::QScaleLabel(MicrotonalView* view) -: QLabel(view, "QScaleLabel") +: QLabel(view) , ui_view(view) { setMaximumHeight(15); m_htf = -1000.0f; } -void MicrotonalView::QScaleLabel::drawContents(QPainter* p) +void MicrotonalView::QScaleLabel::paintEvent(QPaintEvent* event) { + QLabel::paintEvent(event); + + QPainter* p = new QPainter(this); + int left = ui_view->m_roots[0]->x() + ui_view->m_roots[0]->width()/2; int right = ui_view->m_roots[12]->x() + ui_view->m_roots[12]->width()/2; int w = right - left; @@ -565,29 +610,30 @@ void MicrotonalView::QScaleLabel::drawContents(QPainter* p) int x = int(left+htw); p->setPen(QColor(0,0,255)); p->setBrush(QColor(191,191,255)); - QPointArray arr; - arr.resize(3); - arr.setPoint(0, x-5, 0); - arr.setPoint(1, x+5, 0); - arr.setPoint(2, x, 2*height()/3); - p->drawConvexPolygon(arr); + QPolygon polygon(3); + polygon.setPoint(0, x-5, 0); + polygon.setPoint(1, x+5, 0); + polygon.setPoint(2, x, 2*height()/3); + p->drawConvexPolygon(polygon); p->drawLine(x, 2*height()/3, x, height()); if(htw<(w/12.0f)/4.0f || -(htw-w)<(w/12.0f)/4.0f) { if(htw<(w/12.0f)/4.0f) x += w; else x -= w; - arr.setPoint(0, x-5, 0); - arr.setPoint(1, x+5, 0); - arr.setPoint(2, x, 2*height()/3); - p->drawConvexPolygon(arr); + polygon.setPoint(0, x-5, 0); + polygon.setPoint(1, x+5, 0); + polygon.setPoint(2, x, 2*height()/3); + p->drawConvexPolygon(polygon); p->drawLine(x, 2*height()/3, x, height()); } } + + delete p; } MicrotonalView::QRatiosLabel::QRatiosLabel(MicrotonalView* view) -: QLabel(view, "QRatiosLabel") +: QLabel(view) , ui_view(view) { setMaximumHeight(12+3*(2*fontMetrics().height()+10)); @@ -604,9 +650,12 @@ void MicrotonalView::QRatiosLabel::drawTicks(QPainter* p, float r, int h) p->drawLine(x, 0, x, h); } } -void MicrotonalView::QRatiosLabel::drawContents(QPainter* p) +void MicrotonalView::QRatiosLabel::paintEvent(QPaintEvent* event) { - QLabel::drawContents(p); + QLabel::paintEvent(event); + + QPainter* p = new QPainter(this); + // p->eraseRect(rect()); int left = ui_view->m_roots[0]->x() + ui_view->m_roots[0]->width()/2; @@ -623,7 +672,7 @@ void MicrotonalView::QRatiosLabel::drawContents(QPainter* p) int dec_h2 = (height()-tick_height-p->fontMetrics().height()+2)/3; dec_h = min(dec_h, dec_h2); int ht = ui_view->setting_selectedRoot - ui_view->m_roots[0]->m_ht; - if(ui_view->setting_keepRootToLeft->isOn()) ht = 0; + if(ui_view->setting_keepRootToLeft->isChecked()) ht = 0; for(list::iterator it=ui_view->setting_selectedScale->values.begin(); it!=ui_view->setting_selectedScale->values.end(); ++it) { float htw = 0.0f; @@ -642,23 +691,23 @@ void MicrotonalView::QRatiosLabel::drawContents(QPainter* p) if((*it).is_ratio) { QRect num_rect = fontMetrics().boundingRect(QString::number((*it).num)); - num_rect.moveBy(x+1, y+p->fontMetrics().height()); + num_rect.translate(x+1, y+p->fontMetrics().height()); QRect den_rect = fontMetrics().boundingRect(QString::number((*it).den)); - den_rect.moveBy(x+1, y+2*p->fontMetrics().height()); + den_rect.translate(x+1, y+2*p->fontMetrics().height()); (*it).bounding_rect = num_rect; (*it).bounding_rect |= den_rect; } else { QRect cents_rect = fontMetrics().boundingRect(QString::number((*it).cents)); - cents_rect.moveBy(x+1, y+2*p->fontMetrics().height()); + cents_rect.translate(x+1, y+2*p->fontMetrics().height()); (*it).bounding_rect = cents_rect; } - (*it).bounding_rect.rLeft() = x; - (*it).bounding_rect.rRight() += 2; - (*it).bounding_rect.rTop() -= 2; - (*it).bounding_rect.rBottom() = y+dec_h; + (*it).bounding_rect.setLeft(x); + (*it).bounding_rect.setRight((*it).bounding_rect.right() + 2); + (*it).bounding_rect.setTop((*it).bounding_rect.top() - 2); + (*it).bounding_rect.setBottom(y+dec_h); if(ui_view->m_selected_jivalue==&(*it)) { @@ -692,11 +741,13 @@ void MicrotonalView::QRatiosLabel::drawContents(QPainter* p) drawTicks(p, 0.5f, tick_height/2); drawTicks(p, 0.25f, tick_height/3); drawTicks(p, 0.125f, tick_height/4); + + delete p; } void MicrotonalView::mouseReleaseEvent(QMouseEvent* e) { - if(e->button()==LeftButton) + if(e->button()==Qt::LeftButton) { MScale::MValue* selected_jivalue_old = m_selected_jivalue; m_selected_jivalue = NULL; @@ -719,34 +770,55 @@ void MicrotonalView::mouseReleaseEvent(QMouseEvent* e) void MicrotonalView::refreshScaleList() { ui_scale_menu.clear(); + + QActionGroup* scaleGroup = new QActionGroup(this); + for(size_t i=0; igetName(), i); - ui_scale_menu.connectItem(i, this, SLOT(selectScale(int))); + QAction* scaleAction = ui_scale_menu.addAction(setting_scales[i]->getName()); + scaleAction->setCheckable(true); + scaleAction->setData((int)i); + scaleGroup->addAction(scaleAction); + if(setting_selectedScale==setting_scales[i]) - ui_scale_menu.setItemChecked(i, true); + scaleAction->setChecked(true); + + connect(scaleAction, SIGNAL(toggled(bool)), this, SLOT(scaleActionToggled(bool))); } } void MicrotonalView::loadScale() { - QFileDialog dlg_file(setting_lastScalesDirectory, "Scala files (*.scl *.SCL)", this, "open file dialog", true); +#ifdef QT3_SUPPORT + Q3FileDialog dlg_file(setting_lastScalesDirectory, "Scala files (*.scl *.SCL)", this, "open file dialog", true); dlg_file.setCaption(tr("Open scale file")); - dlg_file.setMode(QFileDialog::ExistingFile); + dlg_file.setMode(Q3FileDialog::ExistingFile); dlg_file.setInfoPreviewEnabled(true); ScalePreview p(&dlg_file); dlg_file.setInfoPreview(&p,&p); - dlg_file.setPreviewMode(QFileDialog::Info); + dlg_file.setPreviewMode(Q3FileDialog::Info); +#else + QFileDialog dlg_file(this, tr("Open scale file"), setting_lastScalesDirectory, "Scala files (*.scl *.SCL)"); + dlg_file.setFileMode(QFileDialog::ExistingFile); +#endif dlg_file.exec(); if(dlg_file.result()==QDialog::Accepted) { +#ifdef QT3_SUPPORT setting_lastScalesDirectory = dlg_file.dirPath(); - s_settings->writeEntry("MicrotonalView_lastScalesDirectory", setting_lastScalesDirectory); +#else + setting_lastScalesDirectory = dlg_file.directory().path(); +#endif + s_settings->setValue("MicrotonalView_lastScalesDirectory", setting_lastScalesDirectory); // cerr << "setting_lastScalesDirectory=" << setting_lastScalesDirectory << endl; try { +#ifdef QT3_SUPPORT MScale* scale = new MScale(dlg_file.selectedFile(), MScale::SCALA); +#else + MScale* scale = new MScale(dlg_file.selectedFiles()[0], MScale::SCALA); +#endif bool new_one = true; for(size_t i=0; new_one && i