QPoint EventCanvas::raster(const QPoint& p) const { int x = p.x(); if (x < 0) x = 0; x = editor->rasterVal(x); int pitch = y2pitch(p.y()); int y = pitch2y(pitch); return QPoint(x, y); }
void Piano::paintEvent(QPaintEvent* event) { QPainter p(this); const QRect& rr = event->rect(); QRect r; if (_orientation == PIANO_HORIZONTAL) { int w = keyHeight * 52; p.translate(w, 0); p.rotate(90.0); r.setWidth(rr.height()); r.setHeight(rr.width()); r.setX(0); r.setY(0); } else r = rr; p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); int d = int(_ymag)+1; qreal x = qreal(r.x()); qreal y = 0.0; qreal h = (r.height()+d) / _ymag; QPointF offset(x, _ypos / _ymag + keyHeight * 2 + y); p.scale(1.0, _ymag); p.drawTiledPixmap(QRectF(x, y, qreal(r.width()), h), *octave, offset); if (curPitch != -1) { int y = pitch2y(curPitch); QPixmap* pm; switch(curPitch % 12) { case 0: case 5: pm = mk3; break; case 2: case 7: case 9: pm = mk2; break; case 4: case 11: pm = mk1; break; default: pm = mk4; break; } p.drawPixmap(0, y, *pm); } }
void EventCanvas::viewMousePressEvent(QMouseEvent* event)/*{{{*/ { ///keyState = event->state(); _keyState = ((QInputEvent*) event)->modifiers(); _button = event->button(); //printf("viewMousePressEvent buttons:%x mods:%x button:%x\n", (int)event->buttons(), (int)keyState, event->button()); // special events if right button is clicked while operations // like moving or drawing lasso is performed. if (event->buttons() & Qt::RightButton & ~(event->button())) { //printf("viewMousePressEvent special buttons:%x mods:%x button:%x\n", (int)event->buttons(), (int)keyState, event->button()); switch (_drag) { case DRAG_LASSO: _drag = DRAG_OFF; redraw(); return; case DRAG_MOVE: _drag = DRAG_OFF; endMoveItems(_start, MOVE_MOVE, 0); return; default: break; } } // ignore event if (another) button is already active: if (event->buttons() & (Qt::LeftButton | Qt::RightButton | Qt::MidButton) & ~(event->button())) { //printf("viewMousePressEvent ignoring buttons:%x mods:%x button:%x\n", (int)event->buttons(), (int)keyState, event->button()); return; } bool shift = _keyState & Qt::ShiftModifier; bool alt = _keyState & Qt::AltModifier; bool ctrl = _keyState & Qt::ControlModifier; _start = event->pos(); //--------------------------------------------------- // set curItem to item mouse is pointing // (if any) //--------------------------------------------------- CItemList list = _items; if(multiPartSelectionAction && !multiPartSelectionAction->isChecked()) list = getItemlistForCurrentPart(); if (virt()) { _curItem = list.find(_start);//_items.find(_start); } else { _curItem = 0; //selectAtTick(_start.x()); iCItem ius; bool usfound = false; for (iCItem i = list.begin(); i != list.end(); ++i) { MidiTrack* mtrack = (MidiTrack*)i->second->part()->track(); int sy = _start.y(); int p = y2pitch(sy); if(editor->isGlobalEdit()) p += mtrack->getTransposition(); int p2 = pitch2y(p); QPoint lpos(_start.x(), p2); QRect box = i->second->bbox(); int x = rmapxDev(box.x()); int y = rmapyDev(box.y()); int w = rmapxDev(box.width()); int h = rmapyDev(box.height()); QRect r(x, y, w, h); r.translate(i->second->pos().x(), i->second->pos().y()); if(r.contains(lpos)) { if (i->second->isSelected()) { _curItem = i->second; break; } else if (!usfound) { ius = i; usfound = true; } } } if (!_curItem && usfound) _curItem = ius->second; } if(editor->isGlobalEdit() && _curItem) { populateMultiSelect(_curItem); } if (_curItem && (event->button() == Qt::MidButton)) { if (!_curItem->isSelected()) { selectItem(_curItem, true); updateSelection(); redraw(); } startDrag(_curItem, shift); } else if (event->button() == Qt::RightButton) { if (_curItem) { if (shift) { _drag = DRAG_RESIZE; setCursor(); int dx = _start.x() - _curItem->x(); _curItem->setWidth(dx); _start.setX(_curItem->x()); deselectAll(); selectItem(_curItem, true); updateSelection(); redraw(); } else { _itemPopupMenu = genItemPopup(_curItem); if (_itemPopupMenu) { QAction *act = _itemPopupMenu->exec(QCursor::pos()); if (act) itemPopup(_curItem, act->data().toInt(), _start); delete _itemPopupMenu; } } } else { _canvasPopupMenu = genCanvasPopup(true); if (_canvasPopupMenu) { QAction *act = _canvasPopupMenu->exec(QCursor::pos(), 0); if (act) { int actnum = act->data().toInt(); canvasPopup(actnum); if(actnum >= 20) //Nome of the tools have a higher number than 9 { editor->updateCanvas(); los->arranger->updateCanvas(); } } delete _canvasPopupMenu; } } } else if (event->button() == Qt::LeftButton) { switch (_tool) { case PointerTool: if (_curItem) { /*if (_curItem->part() != _curPart) { _curPart = _curItem->part(); _curPartId = _curPart->sn(); curPartChanged(); }*/ itemPressed(_curItem); if (shift) _drag = DRAG_COPY_START; else if (alt) { _drag = DRAG_CLONE_START; } else if (ctrl) { //Select all on the same pitch (e.g. same y-value) deselectAll(); //printf("Yes, ctrl and press\n"); for (iCItem i = _items.begin(); i != _items.end(); ++i) { if (i->second->y() == _curItem->y()) selectItem(i->second, true); } updateSelection(); redraw(); } else _drag = DRAG_MOVE_START; } else _drag = DRAG_LASSO_START; setCursor(); break; case RubberTool: deleteItem(_start); _drag = DRAG_DELETE; setCursor(); break; case PencilTool: if (_curItem) { _drag = DRAG_RESIZE; setCursor(); int dx = _start.x() - _curItem->x(); _curItem->setWidth(dx); _start.setX(_curItem->x()); } else { _drag = DRAG_NEW; setCursor(); _curItem = newItem(_start, event->modifiers()); if (_curItem) _items.add(_curItem); else { _drag = DRAG_OFF; setCursor(); } } deselectAll(); if (_curItem) { selectItem(_curItem, true); // Play the note itemPressed(_curItem); } updateSelection(); redraw(); break; default: break; } } mousePress(event); }/*}}}*/
void Piano::draw(QPainter& p, const QRect& r) { QPoint offset(0, KH*2); p.drawTiledPixmap(r, *octave, r.topLeft()+offset); if (_curSelectedPitch != -1 && _curSelectedPitch != curPitch) { int y = pitch2y(_curSelectedPitch); QPixmap* pm; switch(_curSelectedPitch % 12) { case 0: case 5: pm = mk7; break; case 2: case 7: case 9: pm = mk6; break; case 4: case 11: pm = mk5; break; default: pm = mk8; break; } p.drawPixmap(0, y, *pm); } if (curPitch != -1) { int y = pitch2y(curPitch); QPixmap* pm; switch(curPitch % 12) { case 0: case 5: pm = mk3; break; case 2: case 7: case 9: pm = mk2; break; case 4: case 11: pm = mk1; break; default: pm = mk4; break; } p.drawPixmap(0, y, *pm); } // draw C notes for (int drawKey = 0; drawKey < 8; drawKey++) { int octaveSize=91; int drawY = octaveSize * drawKey + 82 - KH*2; if (drawY > r.y() && drawY < r.y() + r.height()) { p.drawPixmap(0,drawY,*c_keys[drawKey]); } } if(!_midiEditor) return; MusECore::PartList* part_list = _midiEditor->parts(); MusECore::Part* cur_part = _midiEditor->curCanvasPart(); if(!part_list || !cur_part) return; MusECore::MidiTrack* track = (MusECore::MidiTrack*)(cur_part->track()); int channel = track->outChannel(); MusECore::MidiPort* port = &MusEGlobal::midiPorts[track->outPort()]; MusECore::MidiCtrlValListList* cll = port->controller(); const int min = channel << 24; const int max = min + 0x1000000; for(MusECore::ciMidiCtrlValList it = cll->lower_bound(min); it != cll->lower_bound(max); ++it) { MusECore::MidiCtrlValList* cl = it->second; MusECore::MidiController* c = port->midiController(cl->num()); if(!c->isPerNoteController()) continue; int cnum = c->num(); int num = cl->num(); int pitch = num & 0x7f; bool used = false; MusECore::EventList* el = cur_part->events(); for (MusECore::ciEvent ie = el->begin(); ie != el->end(); ++ie) { MusECore::Event e = ie->second; if(e.type() != MusECore::Controller) continue; int ctl_num = e.dataA(); if((ctl_num | 0xff) == cnum && (ctl_num & 0x7f) == pitch) { used = true; break; } } bool off = cl->hwVal() == MusECore::CTRL_VAL_UNKNOWN; // Does it have a value or is it 'off'? int y = pitch2y(pitch) + 3; if(used) { if(off) p.drawPixmap(0, y, 6, 6, *greendot12x12Icon); else p.drawPixmap(0, y, 6, 6, *orangedot12x12Icon); } else { if(off) p.drawPixmap(0, y, 6, 6, *graydot12x12Icon); else p.drawPixmap(0, y, 6, 6, *bluedot12x12Icon); } } }