1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: dlist.cpp,v 1.4 2003/11/19 21:28:40 lunar_shuttle Exp $
// (C) Copyright 1999 Werner Schweer (ws@seh.de)
//=========================================================
#include "icons.h"
#include "dlist.h"
#include <qpainter.h>
#include <stdio.h>
#include <qheader.h>
#include <qcursor.h>
#include "song.h"
#include "scrollscale.h"
#include <qpopupmenu.h>
#include <qlineedit.h>
#include "pitchedit.h"
#include "midiport.h"
#include "drummap.h"
enum DCols { COL_MUTE=0, COL_NAME, COL_QNT, COL_ENOTE, COL_LEN,
COL_ANOTE, COL_CHANNEL, COL_PORT,
COL_LV1, COL_LV2, COL_LV3, COL_LV4, COL_NONE=-1};
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void DList::draw(QPainter& p, const QRect& rect)
{
int x = rect.x();
int y = rect.y();
int w = rect.width();
int h = rect.height();
//---------------------------------------------------
// Tracks
//---------------------------------------------------
p.setPen(black);
for (int i = 0; i < DRUM_MAPSIZE; ++i) {
int yy = i * TH;
if (yy+TH < y)
continue;
if (yy > y + h)
break;
DrumMap* dm = &drumMap[i];
if (dm->selected)
p.fillRect(x, yy, w, TH, yellow);
else
p.eraseRect(x, yy, w, TH);
for (int k = 0; k < header->count(); ++k) {
int x = header->sectionPos(k);
int w = header->sectionSize(k);
QRect r = p.xForm(QRect(x+2, yy, w-4, TH));
QString s;
int align = AlignVCenter | AlignHCenter;
p.save();
p.setWorldXForm(false);
switch (k) {
case COL_QNT:
s.setNum(dm->quant);
break;
case COL_LEN:
s.setNum(dm->len);
break;
case COL_ANOTE:
s = pitch2string(dm->anote);
break;
case COL_ENOTE:
s = pitch2string(dm->enote);
break;
case COL_LV1:
s.setNum(dm->lv1);
break;
case COL_LV2:
s.setNum(dm->lv2);
break;
case COL_LV3:
s.setNum(dm->lv3);
break;
case COL_LV4:
s.setNum(dm->lv4);
break;
case COL_MUTE:
if (dm->mute) {
p.setPen(red);
const QPixmap& pm = *muteIcon;
p.drawPixmap(
r.x() + r.width()/2 - pm.width()/2,
r.y() + r.height()/2 - pm.height()/2,
pm);
p.setPen(black);
}
break;
case COL_NAME:
s = dm->name;
align = AlignVCenter | AlignLeft;
break;
case COL_CHANNEL:
s.setNum(dm->channel+1);
break;
case COL_PORT:
s = midiPorts[dm->port].portname();
align = AlignVCenter | AlignLeft;
break;
}
if (!s.isEmpty())
p.drawText(r, align, s);
p.restore();
}
}
//---------------------------------------------------
// horizontal lines
//---------------------------------------------------
p.setPen(gray);
int yy = (y / TH) * TH;
for (; yy < y + h; yy += TH) {
p.drawLine(x, yy, x + w, yy);
}
if (drag == DRAG) {
int y = (startY/TH) * TH;
int dy = startY - y;
int yy = curY - dy;
p.setPen(green);
p.drawLine(x, yy, x + w, yy);
p.drawLine(x, yy+TH, x + w, yy+TH);
p.setPen(gray);
}
//---------------------------------------------------
// vertical Lines
//---------------------------------------------------
p.setWorldXForm(false);
int n = header->count();
for (int i = 1; i < n; i++) {
int x = header->sectionPos(i);
p.drawLine(x, 0, x, height());
}
p.setWorldXForm(true);
}
//---------------------------------------------------------
// devicesPopupMenu
//---------------------------------------------------------
void DList::devicesPopupMenu(DrumMap* t, int x, int y, bool changeAll)
{
QPopupMenu* p = midiPortsPopup(this);
int n = p->exec(mapToGlobal(QPoint(x, y)), 0);
if (n != -1) {
if (!changeAll)
t->port = n;
else
for (int i=0; i<DRUM_MAPSIZE; i++)
drumMap[i].port = n;
}
delete p;
}
//---------------------------------------------------------
// viewMousePressEvent
//---------------------------------------------------------
void DList::viewMousePressEvent(QMouseEvent* ev)
{
int x = ev->x();
int y = ev->y();
int instr = y/TH;
int button = ev->button();
bool shift = ev->state() & ShiftButton;
unsigned pitch = y / TH;
DrumMap* dm = &drumMap[pitch];
if (currentlySelected != 0) {
if (dm != currentlySelected) {
currentlySelected->selected = false;
dm->selected = true;
currentlySelected = dm;
emit selectionChanged(currentlySelected);
emit keyFilterChanged(instr);
song->update(SC_DRUMMAP);
repaint();
}
}
else {
currentlySelected = dm;
dm->selected = true;
emit selectionChanged(currentlySelected);
emit keyFilterChanged(instr);
song->update(SC_DRUMMAP);
repaint();
}
startY = y;
sPitch = pitch;
drag = START_DRAG;
DCols col = DCols(x2col(x));
int val;
int incVal = 0;
if (button == QMouseEvent::RightButton)
incVal = 1;
else if (button == QMouseEvent::MidButton)
incVal = -1;
// Check if we're already editing anything and have pressed the mouse elsewhere
// In that case, treat it as if a return was pressed
if (button == QMouseEvent::LeftButton) {
if ((editEntry && editEntry != dm || col != selectedColumn) && editEntry != 0) {
returnPressed();
}
}
switch (col) {
case COL_NONE:
break;
case COL_MUTE:
if (button == QMouseEvent::LeftButton)
dm->mute = !dm->mute;
break;
case COL_PORT:
if (button == QMouseEvent::RightButton) {
bool changeAll = ev->state() & ControlButton;
devicesPopupMenu(dm, mapx(x), mapy(pitch * TH), changeAll);
}
break;
case COL_QNT:
dm->quant += incVal;
// ?? range
break;
case COL_ENOTE:
val = dm->enote + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
// Check if there is any other drumMap with the same inmap value
// If so, switch the inmap between the instruments
for (int i=0; i<DRUM_MAPSIZE; i++) {
if (drumMap[i].enote == val && &drumMap[i] != dm) {
drumInmap[dm->enote] = i;
drumMap[i].enote = dm->enote;
break;
}
}
dm->enote = val;
drumInmap[val] = pitch; // The inmap for the particular note now points to the instrument we're currently at
break;
case COL_LEN:
val = dm->len + incVal;
if (val < 0)
val = 0;
dm->len = val;
break;
case COL_ANOTE:
val = dm->anote + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
dm->anote = val;
emit keyPressed(dm->enote, shift); //Send input-key
break;
case COL_CHANNEL:
val = dm->channel + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
if (ev->state() & ControlButton) {
for (int i=0; i<DRUM_MAPSIZE; i++)
drumMap[i].channel = val;
}
else
dm->channel = val;
break;
case COL_LV1:
val = dm->lv1 + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
dm->lv1 = val;
break;
case COL_LV2:
val = dm->lv2 + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
dm->lv2 = val;
break;
case COL_LV3:
val = dm->lv3 + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
dm->lv3 = val;
break;
case COL_LV4:
val = dm->lv4 + incVal;
if (val < 0)
val = 0;
else if (val > 127)
val = 127;
dm->lv4 = val;
break;
case COL_NAME:
emit keyPressed(dm->enote, shift); //Send input key
break;
#if 0
case COL_CHANNEL:
{
int channel = t->channel();
if (button == QMouseEvent::RightButton) {
if (channel < 15)
++channel;
}
else if (button == QMouseEvent::MidButton) {
if (channel > 0)
--channel;
}
if (channel != t->channel()) {
t->setChannel(channel);
emit channelChanged();
}
}
#endif
default:
break;
}
redraw();
}
//---------------------------------------------------------
// viewMouseDoubleClickEvent
//---------------------------------------------------------
void DList::viewMouseDoubleClickEvent(QMouseEvent* ev)
{
int x = ev->x();
int y = ev->y();
unsigned pitch = y / TH;
DrumMap* dm = &drumMap[pitch];
int section = header->sectionAt(x);
if (section == COL_NAME || section == COL_LEN || section == COL_LV1 ||
section == COL_LV2 || section == COL_LV3 || section == COL_LV4) {
editEntry = dm;
if (editor == 0) {
editor = new QLineEdit(this);
connect(editor, SIGNAL(returnPressed()),
SLOT(returnPressed()));
editor->setFrame(true);
}
int colx = mapx(header->sectionPos(section));
int colw = rmapx(header->sectionSize(section));
int coly = mapy(pitch * TH);
int colh = rmapy(TH);
selectedColumn = section; //Store selected column to have an idea of which one was selected when return is pressed
switch (section) {
case COL_NAME:
editor->setText(dm->name);
break;
case COL_LEN: {
editor->setText(QString::number(dm->len));
break;
}
case COL_LV1:
editor->setText(QString::number(dm->lv1));
break;
case COL_LV2:
editor->setText(QString::number(dm->lv2));
break;
case COL_LV3:
editor->setText(QString::number(dm->lv3));
break;
case COL_LV4:
editor->setText(QString::number(dm->lv4));
break;
}
editor->end(false);
editor->setGeometry(colx, coly, colw, colh);
//In all cases but the column name, select all text:
if (section != COL_NAME)
editor->selectAll();
editor->show();
}
else
viewMousePressEvent(ev);
}
//---------------------------------------------------------
// x2col
//---------------------------------------------------------
int DList::x2col(int x) const
{
int col = 0;
int w = 0;
for (; col < header->count(); col++) {
w += header->cellSize(col);
if (x < w)
break;
}
if (col == header->count())
return -1;
return header->mapToLogical(col);
}
//---------------------------------------------------------
// moveSelection
//---------------------------------------------------------
void DList::moveSelection(int)
{
}
//---------------------------------------------------------
// sizeChange
//---------------------------------------------------------
void DList::sizeChange(int, int, int)
{
redraw();
}
//---------------------------------------------------------
// returnPressed
//---------------------------------------------------------
void DList::returnPressed()
{
int val = -1;
if (selectedColumn != COL_NAME) {
val = atoi(editor->text().ascii());
if (selectedColumn != COL_LEN) {
if (val > 127) //Check bounds for lv1-lv4 values
val = 127;
if (val < 0)
val = 0;
}
}
switch(selectedColumn) {
case COL_NAME:
editEntry->name = editor->text();
break;
case COL_LEN:
editEntry->len = atoi(editor->text().ascii());
break;
case COL_LV1:
editEntry->lv1 = val;
break;
case COL_LV2:
editEntry->lv2 = val;
break;
case COL_LV3:
editEntry->lv3 = val;
break;
case COL_LV4:
editEntry->lv4 = val;
break;
default:
printf("Return pressed in unknown column\n");
break;
}
selectedColumn = -1;
editor->hide();
editEntry = 0;
setFocus();
redraw();
}
//---------------------------------------------------------
// moved
//---------------------------------------------------------
void DList::moved(int, int)
{
redraw();
}
//---------------------------------------------------------
// tracklistChanged
//---------------------------------------------------------
void DList::tracklistChanged()
{
}
//---------------------------------------------------------
// songChanged
//---------------------------------------------------------
void DList::songChanged(int flags)
{
if (flags & SC_DRUMMAP)
redraw();
}
//---------------------------------------------------------
// DList
//---------------------------------------------------------
DList::DList(QHeader* h, QWidget* parent, int ymag)
: View(parent, 1, ymag)
{
setBg(white);
header = h;
scroll = 0;
header->setTracking(true);
connect(header, SIGNAL(sizeChange(int,int,int)),
SLOT(sizeChange(int,int,int)));
connect(header, SIGNAL(moved(int,int)), SLOT(moved(int,int)));
setFocusPolicy(QWidget::StrongFocus);
drag = NORMAL;
editor = 0;
editEntry = 0;
currentlySelected = 0;
selectedColumn = -1;
}
//---------------------------------------------------------
// ~DList
//---------------------------------------------------------
DList::~DList()
{
if (currentlySelected != 0)
currentlySelected->selected = false; //Reset the global
}
//---------------------------------------------------------
// viewMouseMoveEvent
//---------------------------------------------------------
void DList::viewMouseMoveEvent(QMouseEvent* ev)
{
curY = ev->y();
int delta = curY - startY;
switch (drag) {
case START_DRAG:
if (delta < 0)
delta = -delta;
if (delta <= 2)
return;
drag = DRAG;
setCursor(QCursor(sizeVerCursor));
redraw();
break;
case NORMAL:
break;
case DRAG:
redraw();
break;
}
}
//---------------------------------------------------------
// viewMouseReleaseEvent
//---------------------------------------------------------
void DList::viewMouseReleaseEvent(QMouseEvent* ev)
{
if (drag == DRAG) {
int y = ev->y();
unsigned dPitch = y / TH;
DrumMap dm = drumMap[sPitch];
drumMap[sPitch] = drumMap[dPitch];
drumMap[dPitch] = dm;
setCursor(QCursor(arrowCursor));
drumInmap[drumMap[sPitch].enote] = sPitch;
drumOutmap[drumMap[sPitch].anote] = sPitch;
drumInmap[drumMap[dPitch].enote] = dPitch;
drumOutmap[drumMap[dPitch].anote] = dPitch;
currentlySelected = &drumMap[dPitch];
emit mapChanged(sPitch, dPitch); //Pitch change throughout the whole track done in canvas
}
drag = NORMAL;
redraw();
if (editEntry)
editor->setFocus();
int x = ev->x();
int y = ev->y();
bool shift = ev->state() & ShiftButton;
unsigned pitch = y / TH;
DrumMap* dm = &drumMap[pitch];
DCols col = DCols(x2col(x));
switch (col) {
case COL_NAME:
emit keyReleased(dm->enote, shift);
break;
case COL_ANOTE:
emit keyReleased(dm->enote, shift);
break;
default:
break;
}
}
int DList::getSelectedInstrument()
{
if (currentlySelected == 0)
return -1;
return drumInmap[currentlySelected->enote];
}
|