You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PianoKeyBoard.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "PianoKeyBoard.h"
  2. #include "MainWindow.h"
  3. #include <QGraphicsSceneMouseEvent>
  4. #include <QColorDialog>
  5. PianoKeyBoard::PianoKeyBoard(qreal x, qreal y, qreal w, qreal h, MainWindow *parent): QGraphicsScene(x,y,w,h){
  6. addItem(keys[0] = new PianoKey(parent->colorScale.getToneByName("C")));
  7. addItem(keys[1] = new PianoKey(parent->colorScale.getToneByName("Cis")));
  8. addItem(keys[2] = new PianoKey(parent->colorScale.getToneByName("D")));
  9. addItem(keys[3] = new PianoKey(parent->colorScale.getToneByName("Dis")));
  10. addItem(keys[4] = new PianoKey(parent->colorScale.getToneByName("E")));
  11. addItem(keys[5] = new PianoKey(parent->colorScale.getToneByName("F")));
  12. addItem(keys[6] = new PianoKey(parent->colorScale.getToneByName("Fis")));
  13. addItem(keys[7] = new PianoKey(parent->colorScale.getToneByName("G")));
  14. addItem(keys[8] = new PianoKey(parent->colorScale.getToneByName("Gis")));
  15. addItem(keys[9] = new PianoKey(parent->colorScale.getToneByName("A")));
  16. addItem(keys[10] = new PianoKey(parent->colorScale.getToneByName("Ais")));
  17. addItem(keys[11] = new PianoKey(parent->colorScale.getToneByName("H")));
  18. for(int i=0; i<12; i++)
  19. QObject::connect(keys[i]->getTone(), &Tone::colorchanged, keys[i], &PianoKey::redraw);
  20. };
  21. void PianoKeyBoard::mousePressEvent(QGraphicsSceneMouseEvent *event){
  22. QString title = "Bitte Farbe wählen für Ton ";
  23. if(event->button() == Qt::LeftButton){
  24. //first check "black" Piano keys as they are in foreground
  25. for(int i=6; i<12; i++){
  26. if(keys[i]->sceneBoundingRect().contains(event->scenePos())){
  27. keys[i]->getTone()->setColor(
  28. QColorDialog::getColor(
  29. keys[i]->getTone()->getColor(),
  30. nullptr,
  31. title.append(keys[i]->getTone()->getName()),
  32. QColorDialog::DontUseNativeDialog
  33. )
  34. );
  35. return;
  36. }
  37. }
  38. for(int i=0; i<6; i++){
  39. if(keys[i]->sceneBoundingRect().contains(event->scenePos())){
  40. keys[i]->getTone()->setColor(
  41. QColorDialog::getColor(
  42. keys[i]->getTone()->getColor(),
  43. nullptr,
  44. title.append(keys[i]->getTone()->getName()),
  45. QColorDialog::DontUseNativeDialog
  46. )
  47. );
  48. return;
  49. }
  50. }
  51. }
  52. };
  53. PianoKey * PianoKeyBoard::getKey(int number)const{
  54. return keys[number];
  55. };
  56. PianoKeyBoard::~PianoKeyBoard(){
  57. };