PianoRoll.cpp 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995
  1. /*
  2. * PianoRoll.cpp - implementation of piano roll which is used for actual
  3. * writing of melodies
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. * Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #include "PianoRoll.h"
  27. #include <QApplication>
  28. #include <QClipboard>
  29. #include <QKeyEvent>
  30. #include <QLabel>
  31. #include <QLayout>
  32. #include <QMdiArea>
  33. #include <QPainter>
  34. #include <QPointer>
  35. #include <QScrollBar>
  36. #include <QStyleOption>
  37. #include <QtMath>
  38. #include <QToolButton>
  39. #ifndef __USE_XOPEN
  40. #define __USE_XOPEN
  41. #endif
  42. #include <math.h>
  43. #include <utility>
  44. #include "AutomationEditor.h"
  45. #include "ActionGroup.h"
  46. #include "BBTrackContainer.h"
  47. #include "Clipboard.h"
  48. #include "ComboBox.h"
  49. #include "ConfigManager.h"
  50. #include "DataFile.h"
  51. #include "debug.h"
  52. #include "DeprecationHelper.h"
  53. #include "DetuningHelper.h"
  54. #include "embed.h"
  55. #include "GuiApplication.h"
  56. #include "gui_templates.h"
  57. #include "InstrumentTrack.h"
  58. #include "MainWindow.h"
  59. #include "Pattern.h"
  60. #include "SongEditor.h"
  61. #include "StepRecorderWidget.h"
  62. #include "TextFloat.h"
  63. #include "TimeLineWidget.h"
  64. using std::move;
  65. typedef AutomationPattern::timeMap timeMap;
  66. // some constants...
  67. const int INITIAL_PIANOROLL_WIDTH = 860;
  68. const int INITIAL_PIANOROLL_HEIGHT = 485;
  69. const int SCROLLBAR_SIZE = 12;
  70. const int PIANO_X = 0;
  71. const int WHITE_KEY_WIDTH = 64;
  72. const int BLACK_KEY_WIDTH = 41;
  73. const int DEFAULT_KEY_LINE_HEIGHT = 12;
  74. const int DEFAULT_CELL_WIDTH = 12;
  75. const int NOTE_EDIT_RESIZE_BAR = 6;
  76. const int NOTE_EDIT_MIN_HEIGHT = 50;
  77. const int KEY_AREA_MIN_HEIGHT = DEFAULT_KEY_LINE_HEIGHT * 10;
  78. const int PR_BOTTOM_MARGIN = SCROLLBAR_SIZE;
  79. const int PR_TOP_MARGIN = 18;
  80. const int PR_RIGHT_MARGIN = SCROLLBAR_SIZE;
  81. // width of area used for resizing (the grip at the end of a note)
  82. const int RESIZE_AREA_WIDTH = 9;
  83. // width of line for setting volume/panning of note
  84. const int NOTE_EDIT_LINE_WIDTH = 3;
  85. // key where to start
  86. const int INITIAL_START_KEY = Key_C + Octave_4 * KeysPerOctave;
  87. // number of each note to provide in quantization and note lengths
  88. const int NUM_EVEN_LENGTHS = 6;
  89. const int NUM_TRIPLET_LENGTHS = 5;
  90. QPixmap * PianoRoll::s_toolDraw = NULL;
  91. QPixmap * PianoRoll::s_toolErase = NULL;
  92. QPixmap * PianoRoll::s_toolSelect = NULL;
  93. QPixmap * PianoRoll::s_toolMove = NULL;
  94. QPixmap * PianoRoll::s_toolOpen = NULL;
  95. QPixmap* PianoRoll::s_toolRazor = nullptr;
  96. TextFloat * PianoRoll::s_textFloat = NULL;
  97. static QString s_noteStrings[12] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
  98. static QString getNoteString( int key )
  99. {
  100. return s_noteStrings[key % 12] + QString::number( static_cast<int>( key / KeysPerOctave ) );
  101. }
  102. // used for drawing of piano
  103. PianoRoll::PianoRollKeyTypes PianoRoll::prKeyOrder[] =
  104. {
  105. PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY,
  106. PR_WHITE_KEY_SMALL, PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG,
  107. PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY, PR_WHITE_KEY_SMALL
  108. } ;
  109. const int DEFAULT_PR_PPB = DEFAULT_CELL_WIDTH * DefaultStepsPerBar;
  110. const QVector<double> PianoRoll::m_zoomLevels =
  111. {0.125f, 0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 4.0f, 8.0f};
  112. const QVector<double> PianoRoll::m_zoomYLevels =
  113. {0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 4.0f};
  114. PianoRoll::PianoRoll() :
  115. m_nemStr( QVector<QString>() ),
  116. m_noteEditMenu( NULL ),
  117. m_semiToneMarkerMenu( NULL ),
  118. m_zoomingModel(),
  119. m_zoomingYModel(),
  120. m_quantizeModel(),
  121. m_noteLenModel(),
  122. m_scaleModel(),
  123. m_chordModel(),
  124. m_pattern( NULL ),
  125. m_currentPosition(),
  126. m_recording( false ),
  127. m_currentNote( NULL ),
  128. m_action( ActionNone ),
  129. m_noteEditMode( NoteEditVolume ),
  130. m_moveBoundaryLeft( 0 ),
  131. m_moveBoundaryTop( 0 ),
  132. m_moveBoundaryRight( 0 ),
  133. m_moveBoundaryBottom( 0 ),
  134. m_mouseDownKey( 0 ),
  135. m_mouseDownTick( 0 ),
  136. m_lastMouseX( 0 ),
  137. m_lastMouseY( 0 ),
  138. m_notesEditHeight( 100 ),
  139. m_userSetNotesEditHeight(100),
  140. m_ppb( DEFAULT_PR_PPB ),
  141. m_keyLineHeight(DEFAULT_KEY_LINE_HEIGHT),
  142. m_octaveHeight(m_keyLineHeight * KeysPerOctave),
  143. m_whiteKeySmallHeight(qFloor(m_keyLineHeight * 1.5)),
  144. m_whiteKeyBigHeight(m_keyLineHeight * 2),
  145. m_blackKeyHeight(m_keyLineHeight),
  146. m_lenOfNewNotes( TimePos( 0, DefaultTicksPerBar/4 ) ),
  147. m_lastNoteVolume( DefaultVolume ),
  148. m_lastNotePanning( DefaultPanning ),
  149. m_minResizeLen( 0 ),
  150. m_startKey( INITIAL_START_KEY ),
  151. m_lastKey( 0 ),
  152. m_editMode( ModeDraw ),
  153. m_ctrlMode( ModeDraw ),
  154. m_mouseDownRight( false ),
  155. m_firstRazorSplit(false),
  156. m_scrollBack( false ),
  157. m_stepRecorderWidget(this, DEFAULT_PR_PPB, PR_TOP_MARGIN, PR_BOTTOM_MARGIN + m_notesEditHeight, WHITE_KEY_WIDTH, 0),
  158. m_stepRecorder(*this, m_stepRecorderWidget),
  159. m_barLineColor( 0, 0, 0 ),
  160. m_beatLineColor( 0, 0, 0 ),
  161. m_lineColor( 0, 0, 0 ),
  162. m_noteModeColor( 0, 0, 0 ),
  163. m_noteColor( 0, 0, 0 ),
  164. m_ghostNoteColor( 0, 0, 0 ),
  165. m_ghostNoteTextColor( 0, 0, 0 ),
  166. m_barColor( 0, 0, 0 ),
  167. m_selectedNoteColor( 0, 0, 0 ),
  168. m_textColor( 0, 0, 0 ),
  169. m_textColorLight( 0, 0, 0 ),
  170. m_textShadow( 0, 0, 0 ),
  171. m_markedSemitoneColor( 0, 0, 0 ),
  172. m_razorCutLineColor(0, 0, 0),
  173. m_noteOpacity( 255 ),
  174. m_ghostNoteOpacity( 255 ),
  175. m_noteBorders( true ),
  176. m_ghostNoteBorders( true ),
  177. m_backgroundShade( 0, 0, 0 ),
  178. m_whiteKeyWidth(WHITE_KEY_WIDTH),
  179. m_blackKeyWidth(BLACK_KEY_WIDTH)
  180. {
  181. // gui names of edit modes
  182. m_nemStr.push_back( tr( "Note Velocity" ) );
  183. m_nemStr.push_back( tr( "Note Panning" ) );
  184. m_noteEditMenu = new QMenu( this );
  185. m_noteEditMenu->clear();
  186. for( int i = 0; i < m_nemStr.size(); ++i )
  187. {
  188. QAction * act = new QAction( m_nemStr.at(i), this );
  189. connect( act, &QAction::triggered, [this, i](){ changeNoteEditMode(i); } );
  190. m_noteEditMenu->addAction( act );
  191. }
  192. m_semiToneMarkerMenu = new QMenu( this );
  193. QAction* markSemitoneAction = new QAction( tr("Mark/unmark current semitone"), this );
  194. QAction* markAllOctaveSemitonesAction = new QAction( tr("Mark/unmark all corresponding octave semitones"), this );
  195. QAction* markScaleAction = new QAction( tr("Mark current scale"), this );
  196. QAction* markChordAction = new QAction( tr("Mark current chord"), this );
  197. QAction* unmarkAllAction = new QAction( tr("Unmark all"), this );
  198. QAction* copyAllNotesAction = new QAction( tr("Select all notes on this key"), this);
  199. connect( markSemitoneAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentSemiTone); });
  200. connect( markAllOctaveSemitonesAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkAllOctaveSemiTones); });
  201. connect( markScaleAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentScale); });
  202. connect( markChordAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentChord); });
  203. connect( unmarkAllAction, &QAction::triggered, [this](){ markSemiTone(stmaUnmarkAll); });
  204. connect( copyAllNotesAction, &QAction::triggered, [this](){ markSemiTone(stmaCopyAllNotesOnKey); });
  205. markScaleAction->setEnabled( false );
  206. markChordAction->setEnabled( false );
  207. connect( this, SIGNAL(semiToneMarkerMenuScaleSetEnabled(bool)), markScaleAction, SLOT(setEnabled(bool)) );
  208. connect( this, SIGNAL(semiToneMarkerMenuChordSetEnabled(bool)), markChordAction, SLOT(setEnabled(bool)) );
  209. m_semiToneMarkerMenu->addAction( markSemitoneAction );
  210. m_semiToneMarkerMenu->addAction( markAllOctaveSemitonesAction );
  211. m_semiToneMarkerMenu->addAction( markScaleAction );
  212. m_semiToneMarkerMenu->addAction( markChordAction );
  213. m_semiToneMarkerMenu->addAction( unmarkAllAction );
  214. m_semiToneMarkerMenu->addAction( copyAllNotesAction );
  215. // init pixmaps
  216. if( s_toolDraw == NULL )
  217. {
  218. s_toolDraw = new QPixmap( embed::getIconPixmap( "edit_draw" ) );
  219. }
  220. if( s_toolErase == NULL )
  221. {
  222. s_toolErase= new QPixmap( embed::getIconPixmap( "edit_erase" ) );
  223. }
  224. if( s_toolSelect == NULL )
  225. {
  226. s_toolSelect = new QPixmap( embed::getIconPixmap( "edit_select" ) );
  227. }
  228. if( s_toolMove == NULL )
  229. {
  230. s_toolMove = new QPixmap( embed::getIconPixmap( "edit_move" ) );
  231. }
  232. if( s_toolOpen == NULL )
  233. {
  234. s_toolOpen = new QPixmap( embed::getIconPixmap( "automation" ) );
  235. }
  236. if (s_toolRazor == nullptr)
  237. {
  238. s_toolRazor = new QPixmap(embed::getIconPixmap("razor"));
  239. }
  240. // init text-float
  241. if( s_textFloat == NULL )
  242. {
  243. s_textFloat = new TextFloat;
  244. }
  245. setAttribute( Qt::WA_OpaquePaintEvent, true );
  246. // add time-line
  247. m_timeLine = new TimeLineWidget(m_whiteKeyWidth, 0, m_ppb,
  248. Engine::getSong()->getPlayPos(
  249. Song::Mode_PlayPattern ),
  250. m_currentPosition,
  251. Song::Mode_PlayPattern, this );
  252. connect( this, SIGNAL( positionChanged( const TimePos & ) ),
  253. m_timeLine, SLOT( updatePosition( const TimePos & ) ) );
  254. connect( m_timeLine, SIGNAL( positionChanged( const TimePos & ) ),
  255. this, SLOT( updatePosition( const TimePos & ) ) );
  256. // white position line follows timeline marker
  257. m_positionLine = new PositionLine(this);
  258. //update timeline when in step-recording mode
  259. connect( &m_stepRecorderWidget, SIGNAL( positionChanged( const TimePos & ) ),
  260. this, SLOT( updatePositionStepRecording( const TimePos & ) ) );
  261. // update timeline when in record-accompany mode
  262. connect( Engine::getSong()->getPlayPos( Song::Mode_PlaySong ).m_timeLine,
  263. SIGNAL( positionChanged( const TimePos & ) ),
  264. this,
  265. SLOT( updatePositionAccompany( const TimePos & ) ) );
  266. // TODO
  267. /* connect( engine::getSong()->getPlayPos( Song::Mode_PlayBB ).m_timeLine,
  268. SIGNAL( positionChanged( const TimePos & ) ),
  269. this,
  270. SLOT( updatePositionAccompany( const TimePos & ) ) );*/
  271. removeSelection();
  272. // init scrollbars
  273. m_leftRightScroll = new QScrollBar( Qt::Horizontal, this );
  274. m_leftRightScroll->setSingleStep( 1 );
  275. connect( m_leftRightScroll, SIGNAL( valueChanged( int ) ), this,
  276. SLOT( horScrolled( int ) ) );
  277. m_topBottomScroll = new QScrollBar( Qt::Vertical, this );
  278. m_topBottomScroll->setSingleStep( 1 );
  279. m_topBottomScroll->setPageStep( 20 );
  280. connect( m_topBottomScroll, SIGNAL( valueChanged( int ) ), this,
  281. SLOT( verScrolled( int ) ) );
  282. // setup zooming-stuff
  283. for( float const & zoomLevel : m_zoomLevels )
  284. {
  285. m_zoomingModel.addItem( QString( "%1\%" ).arg( zoomLevel * 100 ) );
  286. }
  287. m_zoomingModel.setValue( m_zoomingModel.findText( "100%" ) );
  288. connect( &m_zoomingModel, SIGNAL( dataChanged() ),
  289. this, SLOT( zoomingChanged() ) );
  290. // zoom y
  291. for (float const & zoomLevel : m_zoomYLevels)
  292. {
  293. m_zoomingYModel.addItem(QString( "%1\%" ).arg(zoomLevel * 100));
  294. }
  295. m_zoomingYModel.setValue(m_zoomingYModel.findText("100%"));
  296. connect(&m_zoomingYModel, SIGNAL(dataChanged()),
  297. this, SLOT(zoomingYChanged()));
  298. // Set up quantization model
  299. m_quantizeModel.addItem( tr( "Note lock" ) );
  300. for (auto q : Quantizations) {
  301. m_quantizeModel.addItem(QString("1/%1").arg(q));
  302. }
  303. m_quantizeModel.setValue( m_quantizeModel.findText( "1/16" ) );
  304. connect( &m_quantizeModel, SIGNAL( dataChanged() ),
  305. this, SLOT( quantizeChanged() ) );
  306. // Set up note length model
  307. m_noteLenModel.addItem( tr( "Last note" ),
  308. std::make_unique<PixmapLoader>( "edit_draw" ) );
  309. const QString pixmaps[] = { "whole", "half", "quarter", "eighth",
  310. "sixteenth", "thirtysecond", "triplethalf",
  311. "tripletquarter", "tripleteighth",
  312. "tripletsixteenth", "tripletthirtysecond" } ;
  313. for( int i = 0; i < NUM_EVEN_LENGTHS; ++i )
  314. {
  315. auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i] );
  316. m_noteLenModel.addItem( "1/" + QString::number( 1 << i ), ::move(loader) );
  317. }
  318. for( int i = 0; i < NUM_TRIPLET_LENGTHS; ++i )
  319. {
  320. auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i+NUM_EVEN_LENGTHS] );
  321. m_noteLenModel.addItem( "1/" + QString::number( (1 << i) * 3 ), ::move(loader) );
  322. }
  323. m_noteLenModel.setValue( 0 );
  324. // Note length change can cause a redraw if Q is set to lock
  325. connect( &m_noteLenModel, SIGNAL( dataChanged() ),
  326. this, SLOT( noteLengthChanged() ) );
  327. // Set up key selection dropdown
  328. m_keyModel.addItem(tr("No key"));
  329. // Use piano roll note strings for key dropdown
  330. for (int i = 0; i < 12; i++) { m_keyModel.addItem(s_noteStrings[i]); }
  331. m_keyModel.setValue(0); // start with "No key"
  332. connect(&m_keyModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
  333. // Set up scale model
  334. const InstrumentFunctionNoteStacking::ChordTable& chord_table =
  335. InstrumentFunctionNoteStacking::ChordTable::getInstance();
  336. m_scaleModel.addItem( tr("No scale") );
  337. for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
  338. {
  339. if( chord.isScale() )
  340. {
  341. m_scaleModel.addItem( chord.getName() );
  342. }
  343. }
  344. m_scaleModel.setValue( 0 );
  345. // connect scale change to key change so it auto-highlights with scale as well
  346. connect(&m_scaleModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
  347. // change can update m_semiToneMarkerMenu
  348. connect( &m_scaleModel, SIGNAL( dataChanged() ),
  349. this, SLOT( updateSemiToneMarkerMenu() ) );
  350. // Set up chord model
  351. m_chordModel.addItem( tr("No chord") );
  352. for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
  353. {
  354. if( ! chord.isScale() )
  355. {
  356. m_chordModel.addItem( chord.getName() );
  357. }
  358. }
  359. m_chordModel.setValue( 0 );
  360. // change can update m_semiToneMarkerMenu
  361. connect( &m_chordModel, SIGNAL( dataChanged() ),
  362. this, SLOT( updateSemiToneMarkerMenu() ) );
  363. setFocusPolicy( Qt::StrongFocus );
  364. setFocus();
  365. setMouseTracking( true );
  366. connect( &m_scaleModel, SIGNAL( dataChanged() ),
  367. this, SLOT( updateSemiToneMarkerMenu() ) );
  368. connect( Engine::getSong(), SIGNAL( timeSignatureChanged( int, int ) ),
  369. this, SLOT( update() ) );
  370. //connection for selecion from timeline
  371. connect( m_timeLine, SIGNAL( regionSelectedFromPixels( int, int ) ),
  372. this, SLOT( selectRegionFromPixels( int, int ) ) );
  373. m_stepRecorder.initialize();
  374. }
  375. void PianoRoll::reset()
  376. {
  377. m_lastNoteVolume = DefaultVolume;
  378. m_lastNotePanning = DefaultPanning;
  379. clearGhostPattern();
  380. }
  381. void PianoRoll::showTextFloat(const QString &text, const QPoint &pos, int timeout)
  382. {
  383. s_textFloat->setText( text );
  384. // show the float, offset slightly so as to not obscure anything
  385. s_textFloat->moveGlobal( this, pos + QPoint(4, 16) );
  386. if (timeout == -1)
  387. {
  388. s_textFloat->show();
  389. }
  390. else
  391. {
  392. s_textFloat->setVisibilityTimeOut( timeout );
  393. }
  394. }
  395. void PianoRoll::showVolTextFloat(volume_t vol, const QPoint &pos, int timeout)
  396. {
  397. //! \todo display velocity for MIDI-based instruments
  398. // possibly dBFS values too? not sure if it makes sense for note volumes...
  399. showTextFloat( tr("Velocity: %1%").arg( vol ), pos, timeout );
  400. }
  401. void PianoRoll::showPanTextFloat(panning_t pan, const QPoint &pos, int timeout)
  402. {
  403. QString text;
  404. if( pan < 0 )
  405. {
  406. text = tr("Panning: %1% left").arg( qAbs( pan ) );
  407. }
  408. else if( pan > 0 )
  409. {
  410. text = tr("Panning: %1% right").arg( qAbs( pan ) );
  411. }
  412. else
  413. {
  414. text = tr("Panning: center");
  415. }
  416. showTextFloat( text, pos, timeout );
  417. }
  418. void PianoRoll::changeNoteEditMode( int i )
  419. {
  420. m_noteEditMode = (NoteEditMode) i;
  421. repaint();
  422. }
  423. void PianoRoll::markSemiTone(int i, bool fromMenu)
  424. {
  425. const int key = fromMenu
  426. ? getKey(mapFromGlobal(m_semiToneMarkerMenu->pos()).y())
  427. : m_keyModel.value() - 1;
  428. const InstrumentFunctionNoteStacking::Chord * chord = nullptr;
  429. // if "No key" is selected, key is -1, unmark all semitones
  430. // or if scale changed from toolbar to "No scale", unmark all semitones
  431. if (!fromMenu && (key < 0 || m_scaleModel.value() == 0)) { i = stmaUnmarkAll; }
  432. switch( static_cast<SemiToneMarkerAction>( i ) )
  433. {
  434. case stmaUnmarkAll:
  435. m_markedSemiTones.clear();
  436. break;
  437. case stmaMarkCurrentSemiTone:
  438. {
  439. QList<int>::iterator it = std::find( m_markedSemiTones.begin(), m_markedSemiTones.end(), key );
  440. if( it != m_markedSemiTones.end() )
  441. {
  442. m_markedSemiTones.erase( it );
  443. }
  444. else
  445. {
  446. m_markedSemiTones.push_back( key );
  447. }
  448. break;
  449. }
  450. case stmaMarkAllOctaveSemiTones:
  451. {
  452. QList<int> aok = getAllOctavesForKey(key);
  453. if ( m_markedSemiTones.contains(key) )
  454. {
  455. // lets erase all of the ones that match this by octave
  456. QList<int>::iterator i;
  457. for (int ix = 0; ix < aok.size(); ++ix)
  458. {
  459. i = std::find(m_markedSemiTones.begin(), m_markedSemiTones.end(), aok.at(ix));
  460. if (i != m_markedSemiTones.end())
  461. {
  462. m_markedSemiTones.erase(i);
  463. }
  464. }
  465. }
  466. else
  467. {
  468. // we should add all of the ones that match this by octave
  469. m_markedSemiTones.append(aok);
  470. }
  471. break;
  472. }
  473. case stmaMarkCurrentScale:
  474. chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
  475. .getScaleByName( m_scaleModel.currentText() );
  476. case stmaMarkCurrentChord:
  477. {
  478. if( ! chord )
  479. {
  480. chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
  481. .getChordByName( m_chordModel.currentText() );
  482. }
  483. if( chord->isEmpty() )
  484. {
  485. break;
  486. }
  487. else if( chord->isScale() )
  488. {
  489. m_markedSemiTones.clear();
  490. }
  491. const int first = chord->isScale() ? 0 : key;
  492. const int last = chord->isScale() ? NumKeys : key + chord->last();
  493. const int cap = ( chord->isScale() || chord->last() == 0 ) ? KeysPerOctave : chord->last();
  494. for( int i = first; i <= last; i++ )
  495. {
  496. if( chord->hasSemiTone( ( i + cap - ( key % cap ) ) % cap ) )
  497. {
  498. m_markedSemiTones.push_back( i );
  499. }
  500. }
  501. break;
  502. }
  503. case stmaCopyAllNotesOnKey:
  504. {
  505. selectNotesOnKey();
  506. break;
  507. }
  508. default:
  509. ;
  510. }
  511. std::sort( m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>() );
  512. QList<int>::iterator new_end = std::unique( m_markedSemiTones.begin(), m_markedSemiTones.end() );
  513. m_markedSemiTones.erase( new_end, m_markedSemiTones.end() );
  514. // until we move the mouse the window won't update, force redraw
  515. update();
  516. }
  517. PianoRoll::~PianoRoll()
  518. {
  519. }
  520. void PianoRoll::setGhostPattern( Pattern* newPattern )
  521. {
  522. // Expects a pointer to a pattern or nullptr.
  523. m_ghostNotes.clear();
  524. if( newPattern != nullptr )
  525. {
  526. for( Note *note : newPattern->notes() )
  527. {
  528. Note * new_note = new Note( note->length(), note->pos(), note->key() );
  529. m_ghostNotes.push_back( new_note );
  530. }
  531. emit ghostPatternSet( true );
  532. }
  533. }
  534. void PianoRoll::loadGhostNotes( const QDomElement & de )
  535. {
  536. // Load ghost notes from DOM element.
  537. if( de.isElement() )
  538. {
  539. QDomNode node = de.firstChild();
  540. while( !node.isNull() )
  541. {
  542. Note * n = new Note;
  543. n->restoreState( node.toElement() );
  544. m_ghostNotes.push_back( n );
  545. node = node.nextSibling();
  546. }
  547. emit ghostPatternSet( true );
  548. }
  549. }
  550. void PianoRoll::clearGhostPattern()
  551. {
  552. setGhostPattern( nullptr );
  553. emit ghostPatternSet( false );
  554. update();
  555. }
  556. void PianoRoll::glueNotes()
  557. {
  558. if (hasValidPattern())
  559. {
  560. NoteVector selectedNotes = getSelectedNotes();
  561. if (selectedNotes.empty())
  562. {
  563. TextFloat::displayMessage( tr( "Glue notes failed" ),
  564. tr( "Please select notes to glue first." ),
  565. embed::getIconPixmap( "glue", 24, 24 ),
  566. 3000 );
  567. return;
  568. }
  569. // Make undo possible
  570. m_pattern->addJournalCheckPoint();
  571. // Sort notes on key and then pos.
  572. std::sort(selectedNotes.begin(), selectedNotes.end(),
  573. [](const Note * note, const Note * compareNote) -> bool
  574. {
  575. if (note->key() == compareNote->key())
  576. {
  577. return note->pos() < compareNote->pos();
  578. }
  579. return note->key() < compareNote->key();
  580. });
  581. QList<Note *> noteToRemove;
  582. NoteVector::iterator note = selectedNotes.begin();
  583. auto nextNote = note+1;
  584. NoteVector::iterator end = selectedNotes.end();
  585. while (note != end && nextNote != end)
  586. {
  587. // key and position match for glue. The notes are already
  588. // sorted so we don't need to test that nextNote is the same
  589. // position or next in sequence.
  590. if ((*note)->key() == (*nextNote)->key()
  591. && (*nextNote)->pos() <= (*note)->pos()
  592. + qMax(TimePos(0), (*note)->length()))
  593. {
  594. (*note)->setLength(qMax((*note)->length(),
  595. TimePos((*nextNote)->endPos() - (*note)->pos())));
  596. noteToRemove.push_back(*nextNote);
  597. ++nextNote;
  598. }
  599. // key or position doesn't match
  600. else
  601. {
  602. note = nextNote;
  603. nextNote = note+1;
  604. }
  605. }
  606. // Remove old notes
  607. for (int i = 0; i < noteToRemove.count(); ++i)
  608. {
  609. m_pattern->removeNote(noteToRemove[i]);
  610. }
  611. update();
  612. }
  613. }
  614. void PianoRoll::loadMarkedSemiTones(const QDomElement & de)
  615. {
  616. // clear marked semitones to prevent leftover marks
  617. m_markedSemiTones.clear();
  618. if (de.isElement())
  619. {
  620. QDomNode node = de.firstChild();
  621. while (!node.isNull())
  622. {
  623. bool ok;
  624. int key = node.toElement().attribute(
  625. QString("key"), QString("-1")).toInt(&ok, 10);
  626. if (ok && key >= 0)
  627. {
  628. m_markedSemiTones.append(key);
  629. }
  630. node = node.nextSibling();
  631. }
  632. }
  633. // from markSemiTone, required otherwise marks will not show
  634. std::sort(m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>());
  635. QList<int>::iterator new_end = std::unique(m_markedSemiTones.begin(), m_markedSemiTones.end());
  636. m_markedSemiTones.erase(new_end, m_markedSemiTones.end());
  637. }
  638. void PianoRoll::setCurrentPattern( Pattern* newPattern )
  639. {
  640. if( hasValidPattern() )
  641. {
  642. m_pattern->instrumentTrack()->disconnect( this );
  643. }
  644. // force the song-editor to stop playing if it played pattern before
  645. if( Engine::getSong()->isPlaying() &&
  646. Engine::getSong()->playMode() == Song::Mode_PlayPattern )
  647. {
  648. Engine::getSong()->playPattern( NULL );
  649. }
  650. if(m_stepRecorder.isRecording())
  651. {
  652. m_stepRecorder.stop();
  653. }
  654. // set new data
  655. m_pattern = newPattern;
  656. m_currentPosition = 0;
  657. m_currentNote = NULL;
  658. m_startKey = INITIAL_START_KEY;
  659. m_stepRecorder.setCurrentPattern(newPattern);
  660. if( ! hasValidPattern() )
  661. {
  662. //resizeEvent( NULL );
  663. update();
  664. emit currentPatternChanged();
  665. return;
  666. }
  667. m_leftRightScroll->setValue( 0 );
  668. // determine the central key so that we can scroll to it
  669. int central_key = 0;
  670. int total_notes = 0;
  671. for( const Note *note : m_pattern->notes() )
  672. {
  673. if( note->length() > 0 )
  674. {
  675. central_key += note->key();
  676. ++total_notes;
  677. }
  678. }
  679. if( total_notes > 0 )
  680. {
  681. central_key = central_key / total_notes -
  682. ( KeysPerOctave * NumOctaves - m_totalKeysToScroll ) / 2;
  683. m_startKey = qBound( 0, central_key, NumOctaves * KeysPerOctave );
  684. }
  685. // resizeEvent() does the rest for us (scrolling, range-checking
  686. // of start-notes and so on...)
  687. resizeEvent( NULL );
  688. // make sure to always get informed about the pattern being destroyed
  689. connect( m_pattern, SIGNAL( destroyedPattern( Pattern* ) ), this, SLOT( hidePattern( Pattern* ) ) );
  690. connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOn( const Note& ) ), this, SLOT( startRecordNote( const Note& ) ) );
  691. connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOff( const Note& ) ), this, SLOT( finishRecordNote( const Note& ) ) );
  692. connect( m_pattern->instrumentTrack()->pianoModel(), SIGNAL( dataChanged() ), this, SLOT( update() ) );
  693. update();
  694. emit currentPatternChanged();
  695. }
  696. void PianoRoll::hidePattern( Pattern* pattern )
  697. {
  698. if( m_pattern == pattern )
  699. {
  700. setCurrentPattern( NULL );
  701. }
  702. }
  703. void PianoRoll::selectRegionFromPixels( int xStart, int xEnd )
  704. {
  705. xStart -= m_whiteKeyWidth;
  706. xEnd -= m_whiteKeyWidth;
  707. // select an area of notes
  708. int posTicks = xStart * TimePos::ticksPerBar() / m_ppb +
  709. m_currentPosition;
  710. int keyNum = 0;
  711. m_selectStartTick = posTicks;
  712. m_selectedTick = 0;
  713. m_selectStartKey = keyNum;
  714. m_selectedKeys = 1;
  715. // change size of selection
  716. // get tick in which the cursor is posated
  717. posTicks = xEnd * TimePos::ticksPerBar() / m_ppb +
  718. m_currentPosition;
  719. keyNum = 120;
  720. m_selectedTick = posTicks - m_selectStartTick;
  721. if( (int) m_selectStartTick + m_selectedTick < 0 )
  722. {
  723. m_selectedTick = -static_cast<int>(
  724. m_selectStartTick );
  725. }
  726. m_selectedKeys = keyNum - m_selectStartKey;
  727. if( keyNum <= m_selectStartKey )
  728. {
  729. --m_selectedKeys;
  730. }
  731. computeSelectedNotes( false );
  732. }
  733. void PianoRoll::drawNoteRect( QPainter & p, int x, int y,
  734. int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
  735. const QColor & selCol, const int noteOpc, const bool borders, bool drawNoteName )
  736. {
  737. ++x;
  738. ++y;
  739. width -= 2;
  740. if( width <= 0 )
  741. {
  742. width = 2;
  743. }
  744. // Volume
  745. float const volumeRange = static_cast<float>(MaxVolume - MinVolume);
  746. float const volumeSpan = static_cast<float>(n->getVolume() - MinVolume);
  747. float const volumeRatio = volumeSpan / volumeRange;
  748. int volVal = qMin( 255, 100 + static_cast<int>( volumeRatio * 155.0f) );
  749. // Panning
  750. float const panningRange = static_cast<float>(PanningRight - PanningLeft);
  751. float const leftPanSpan = static_cast<float>(PanningRight - n->getPanning());
  752. float const rightPanSpan = static_cast<float>(n->getPanning() - PanningLeft);
  753. float leftPercent = qMin<float>( 1.0f, leftPanSpan / panningRange * 2.0f );
  754. float rightPercent = qMin<float>( 1.0f, rightPanSpan / panningRange * 2.0f );
  755. QColor col = QColor( noteCol );
  756. QPen pen;
  757. if( n->selected() )
  758. {
  759. col = QColor( selCol );
  760. }
  761. const int borderWidth = borders ? 1 : 0;
  762. const int noteHeight = m_keyLineHeight - 1 - borderWidth;
  763. int noteWidth = width + 1 - borderWidth;
  764. // adjust note to make it a bit faded if it has a lower volume
  765. // in stereo using gradients
  766. QColor lcol = QColor::fromHsv( col.hue(), col.saturation(),
  767. static_cast<int>(volVal * leftPercent), noteOpc );
  768. QColor rcol = QColor::fromHsv( col.hue(), col.saturation(),
  769. static_cast<int>(volVal * rightPercent), noteOpc );
  770. QLinearGradient gradient( x, y, x, y + noteHeight );
  771. gradient.setColorAt( 0, rcol );
  772. gradient.setColorAt( 1, lcol );
  773. p.setBrush( gradient );
  774. if ( borders )
  775. {
  776. p.setPen( col );
  777. }
  778. else
  779. {
  780. p.setPen( Qt::NoPen );
  781. }
  782. p.drawRect( x, y, noteWidth, noteHeight );
  783. // Draw note key text
  784. if (drawNoteName)
  785. {
  786. p.save();
  787. int const noteTextHeight = static_cast<int>(noteHeight * 0.8);
  788. if (noteTextHeight > 6)
  789. {
  790. QString noteKeyString = getNoteString(n->key());
  791. QFont noteFont(p.font());
  792. noteFont.setPixelSize(noteTextHeight);
  793. QFontMetrics fontMetrics(noteFont);
  794. QSize textSize = fontMetrics.size(Qt::TextSingleLine, noteKeyString);
  795. int const distanceToBorder = 2;
  796. int const xOffset = borderWidth + distanceToBorder;
  797. // noteTextHeight, textSize are not suitable for determining vertical spacing,
  798. // capHeight() can be used for this, but requires Qt 5.8.
  799. // We use boundingRect() with QChar (the QString version returns wrong value).
  800. QRect const boundingRect = fontMetrics.boundingRect(QChar::fromLatin1('H'));
  801. int const yOffset = (noteHeight - boundingRect.top() - boundingRect.bottom()) / 2;
  802. if (textSize.width() < noteWidth - xOffset)
  803. {
  804. p.setPen(noteTextColor);
  805. p.setFont(noteFont);
  806. QPoint textStart(x + xOffset, y + yOffset);
  807. p.drawText(textStart, noteKeyString);
  808. }
  809. }
  810. p.restore();
  811. }
  812. // draw the note endmark, to hint the user to resize
  813. p.setBrush( col );
  814. if( width > 2 )
  815. {
  816. const int endmarkWidth = 3 - borderWidth;
  817. p.drawRect( x + noteWidth - endmarkWidth, y, endmarkWidth, noteHeight );
  818. }
  819. }
  820. void PianoRoll::drawDetuningInfo( QPainter & _p, const Note * _n, int _x,
  821. int _y ) const
  822. {
  823. int middle_y = _y + m_keyLineHeight / 2;
  824. _p.setPen(m_noteColor);
  825. _p.setClipRect(
  826. m_whiteKeyWidth,
  827. PR_TOP_MARGIN,
  828. width() - m_whiteKeyWidth,
  829. keyAreaBottom() - PR_TOP_MARGIN);
  830. int old_x = 0;
  831. int old_y = 0;
  832. timeMap & map = _n->detuning()->automationPattern()->getTimeMap();
  833. for( timeMap::ConstIterator it = map.begin(); it != map.end(); ++it )
  834. {
  835. int pos_ticks = it.key();
  836. int pos_x = _x + pos_ticks * m_ppb / TimePos::ticksPerBar();
  837. const float level = it.value();
  838. int pos_y = middle_y - level * m_keyLineHeight;
  839. if( old_x != 0 && old_y != 0 )
  840. {
  841. switch( _n->detuning()->automationPattern()->progressionType() )
  842. {
  843. case AutomationPattern::DiscreteProgression:
  844. _p.drawLine( old_x, old_y, pos_x, old_y );
  845. _p.drawLine( pos_x, old_y, pos_x, pos_y );
  846. break;
  847. case AutomationPattern::CubicHermiteProgression: /* TODO */
  848. case AutomationPattern::LinearProgression:
  849. _p.drawLine( old_x, old_y, pos_x, pos_y );
  850. break;
  851. }
  852. }
  853. _p.drawLine( pos_x - 1, pos_y, pos_x + 1, pos_y );
  854. _p.drawLine( pos_x, pos_y - 1, pos_x, pos_y + 1 );
  855. old_x = pos_x;
  856. old_y = pos_y;
  857. }
  858. }
  859. void PianoRoll::removeSelection()
  860. {
  861. m_selectStartTick = 0;
  862. m_selectedTick = 0;
  863. m_selectStartKey = 0;
  864. m_selectedKeys = 0;
  865. }
  866. void PianoRoll::clearSelectedNotes()
  867. {
  868. if( m_pattern != NULL )
  869. {
  870. for( Note *note : m_pattern->notes() )
  871. {
  872. note->setSelected( false );
  873. }
  874. }
  875. }
  876. void PianoRoll::shiftSemiTone(int amount) //Shift notes by amount semitones
  877. {
  878. if (!hasValidPattern()) { return; }
  879. auto selectedNotes = getSelectedNotes();
  880. //If no notes are selected, shift all of them, otherwise shift selection
  881. if (selectedNotes.empty()) { return shiftSemiTone(m_pattern->notes(), amount); }
  882. else { return shiftSemiTone(selectedNotes, amount); }
  883. }
  884. void PianoRoll::shiftSemiTone(NoteVector notes, int amount)
  885. {
  886. m_pattern->addJournalCheckPoint();
  887. for (Note *note : notes) { note->setKey( note->key() + amount ); }
  888. m_pattern->rearrangeAllNotes();
  889. m_pattern->dataChanged();
  890. //We modified the song
  891. update();
  892. gui->songEditor()->update();
  893. }
  894. void PianoRoll::shiftPos(int amount) //Shift notes pos by amount
  895. {
  896. if (!hasValidPattern()) { return; }
  897. auto selectedNotes = getSelectedNotes();
  898. //If no notes are selected, shift all of them, otherwise shift selection
  899. if (selectedNotes.empty()) { return shiftPos(m_pattern->notes(), amount); }
  900. else { return shiftPos(selectedNotes, amount); }
  901. }
  902. void PianoRoll::shiftPos(NoteVector notes, int amount)
  903. {
  904. m_pattern->addJournalCheckPoint();
  905. auto leftMostPos = notes.first()->pos();
  906. //Limit leftwards shifts to prevent moving left of pattern start
  907. auto shiftAmount = (leftMostPos > -amount) ? amount : -leftMostPos;
  908. if (shiftAmount == 0) { return; }
  909. for (Note *note : notes) { note->setPos( note->pos() + shiftAmount ); }
  910. m_pattern->rearrangeAllNotes();
  911. m_pattern->updateLength();
  912. m_pattern->dataChanged();
  913. // we modified the song
  914. update();
  915. gui->songEditor()->update();
  916. }
  917. bool PianoRoll::isSelection() const // are any notes selected?
  918. {
  919. for( const Note *note : m_pattern->notes() )
  920. {
  921. if( note->selected() )
  922. {
  923. return true;
  924. }
  925. }
  926. return false;
  927. }
  928. int PianoRoll::selectionCount() const // how many notes are selected?
  929. {
  930. return getSelectedNotes().size();
  931. }
  932. void PianoRoll::keyPressEvent(QKeyEvent* ke)
  933. {
  934. if(m_stepRecorder.isRecording())
  935. {
  936. bool handled = m_stepRecorder.keyPressEvent(ke);
  937. if(handled)
  938. {
  939. ke->accept();
  940. update();
  941. return;
  942. }
  943. }
  944. if( hasValidPattern() && ke->modifiers() == Qt::NoModifier )
  945. {
  946. const int key_num = PianoView::getKeyFromKeyEvent( ke ) + ( DefaultOctave - 1 ) * KeysPerOctave;
  947. if (!ke->isAutoRepeat() && key_num > -1)
  948. {
  949. m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(key_num);
  950. // if a chord is set, play all chord notes (simulate click on all):
  951. playChordNotes(key_num);
  952. ke->accept();
  953. }
  954. }
  955. switch( ke->key() )
  956. {
  957. case Qt::Key_Up:
  958. case Qt::Key_Down:
  959. {
  960. int direction = (ke->key() == Qt::Key_Up ? +1 : -1);
  961. if( ( ke->modifiers() & Qt::ControlModifier ) && m_action == ActionNone )
  962. {
  963. // shift selection up an octave
  964. // if nothing selected, shift _everything_
  965. if (hasValidPattern())
  966. {
  967. shiftSemiTone( 12 * direction );
  968. }
  969. }
  970. else if((ke->modifiers() & Qt::ShiftModifier) && m_action == ActionNone)
  971. {
  972. // Move selected notes up by one semitone
  973. if (hasValidPattern())
  974. {
  975. shiftSemiTone( 1 * direction );
  976. }
  977. }
  978. else
  979. {
  980. // scroll
  981. m_topBottomScroll->setValue( m_topBottomScroll->value() -
  982. cm_scrollAmtVert * direction );
  983. // if they are moving notes around or resizing,
  984. // recalculate the note/resize position
  985. if( m_action == ActionMoveNote ||
  986. m_action == ActionResizeNote )
  987. {
  988. dragNotes( m_lastMouseX, m_lastMouseY,
  989. ke->modifiers() & Qt::AltModifier,
  990. ke->modifiers() & Qt::ShiftModifier,
  991. ke->modifiers() & Qt::ControlModifier );
  992. }
  993. }
  994. ke->accept();
  995. break;
  996. }
  997. case Qt::Key_Right:
  998. case Qt::Key_Left:
  999. {
  1000. int direction = (ke->key() == Qt::Key_Right ? +1 : -1);
  1001. if( ke->modifiers() & Qt::ControlModifier && m_action == ActionNone )
  1002. {
  1003. // Move selected notes by one bar to the left
  1004. if (hasValidPattern())
  1005. {
  1006. shiftPos( direction * TimePos::ticksPerBar() );
  1007. }
  1008. }
  1009. else if( ke->modifiers() & Qt::ShiftModifier && m_action == ActionNone)
  1010. {
  1011. // move notes
  1012. if (hasValidPattern())
  1013. {
  1014. bool quantized = ! ( ke->modifiers() & Qt::AltModifier );
  1015. int amt = quantized ? quantization() : 1;
  1016. shiftPos( direction * amt );
  1017. }
  1018. }
  1019. else if( ke->modifiers() & Qt::AltModifier)
  1020. {
  1021. // switch to editing a pattern adjacent to this one in the song editor
  1022. if (hasValidPattern())
  1023. {
  1024. Pattern * p = direction > 0 ? m_pattern->nextPattern()
  1025. : m_pattern->previousPattern();
  1026. if(p != NULL)
  1027. {
  1028. setCurrentPattern(p);
  1029. }
  1030. }
  1031. }
  1032. else
  1033. {
  1034. // scroll
  1035. m_leftRightScroll->setValue( m_leftRightScroll->value() +
  1036. direction * cm_scrollAmtHoriz );
  1037. // if they are moving notes around or resizing,
  1038. // recalculate the note/resize position
  1039. if( m_action == ActionMoveNote ||
  1040. m_action == ActionResizeNote )
  1041. {
  1042. dragNotes( m_lastMouseX, m_lastMouseY,
  1043. ke->modifiers() & Qt::AltModifier,
  1044. ke->modifiers() & Qt::ShiftModifier,
  1045. ke->modifiers() & Qt::ControlModifier );
  1046. }
  1047. }
  1048. ke->accept();
  1049. break;
  1050. }
  1051. case Qt::Key_A:
  1052. if( ke->modifiers() & Qt::ControlModifier )
  1053. {
  1054. ke->accept();
  1055. if (ke->modifiers() & Qt::ShiftModifier)
  1056. {
  1057. // Ctrl + Shift + A = deselect all notes
  1058. clearSelectedNotes();
  1059. }
  1060. else
  1061. {
  1062. // Ctrl + A = select all notes
  1063. selectAll();
  1064. }
  1065. update();
  1066. }
  1067. break;
  1068. case Qt::Key_Escape:
  1069. // On the Razor mode, ESC cancels it
  1070. if (m_editMode == ModeEditRazor)
  1071. {
  1072. cancelRazorAction();
  1073. }
  1074. else
  1075. {
  1076. // Same as Ctrl + Shift + A
  1077. clearSelectedNotes();
  1078. }
  1079. break;
  1080. case Qt::Key_Backspace:
  1081. case Qt::Key_Delete:
  1082. deleteSelectedNotes();
  1083. ke->accept();
  1084. break;
  1085. case Qt::Key_Home:
  1086. m_timeLine->pos().setTicks( 0 );
  1087. m_timeLine->updatePosition();
  1088. ke->accept();
  1089. break;
  1090. case Qt::Key_0:
  1091. case Qt::Key_1:
  1092. case Qt::Key_2:
  1093. case Qt::Key_3:
  1094. case Qt::Key_4:
  1095. case Qt::Key_5:
  1096. case Qt::Key_6:
  1097. case Qt::Key_7:
  1098. case Qt::Key_8:
  1099. case Qt::Key_9:
  1100. {
  1101. int len = 1 + ke->key() - Qt::Key_0;
  1102. if( len == 10 )
  1103. {
  1104. len = 0;
  1105. }
  1106. if( ke->modifiers() & ( Qt::ControlModifier | Qt::KeypadModifier ) )
  1107. {
  1108. m_noteLenModel.setValue( len );
  1109. ke->accept();
  1110. }
  1111. else if( ke->modifiers() & Qt::AltModifier )
  1112. {
  1113. m_quantizeModel.setValue( len );
  1114. ke->accept();
  1115. }
  1116. break;
  1117. }
  1118. case Qt::Key_Control:
  1119. // Ctrl will not enter selection mode if we are
  1120. // in Razor mode, but unquantize it
  1121. if (m_editMode == ModeEditRazor)
  1122. {
  1123. break;
  1124. }
  1125. // Enter selection mode if:
  1126. // -> this window is active
  1127. // -> shift is not pressed
  1128. // (<S-C-drag> is shortcut for sticky note resize)
  1129. if ( !( ke->modifiers() & Qt::ShiftModifier ) && isActiveWindow() )
  1130. {
  1131. m_ctrlMode = m_editMode;
  1132. m_editMode = ModeSelect;
  1133. setCursor( Qt::ArrowCursor );
  1134. ke->accept();
  1135. }
  1136. break;
  1137. default:
  1138. break;
  1139. }
  1140. update();
  1141. }
  1142. void PianoRoll::keyReleaseEvent(QKeyEvent* ke )
  1143. {
  1144. if( hasValidPattern() && ke->modifiers() == Qt::NoModifier )
  1145. {
  1146. const int key_num = PianoView::getKeyFromKeyEvent( ke ) + ( DefaultOctave - 1 ) * KeysPerOctave;
  1147. if (!ke->isAutoRepeat() && key_num > -1)
  1148. {
  1149. m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease(key_num);
  1150. // if a chord is set, simulate click release on all chord notes
  1151. pauseChordNotes(key_num);
  1152. ke->accept();
  1153. }
  1154. }
  1155. switch( ke->key() )
  1156. {
  1157. case Qt::Key_Control:
  1158. if (m_editMode == ModeEditRazor)
  1159. {
  1160. break;
  1161. }
  1162. computeSelectedNotes( ke->modifiers() & Qt::ShiftModifier);
  1163. m_editMode = m_ctrlMode;
  1164. update();
  1165. break;
  1166. case Qt::Key_Shift:
  1167. if (m_editMode == ModeEditRazor && !m_firstRazorSplit)
  1168. {
  1169. cancelRazorAction();
  1170. }
  1171. // update after undo/redo
  1172. case Qt::Key_Z:
  1173. case Qt::Key_R:
  1174. if( hasValidPattern() && ke->modifiers() == Qt::ControlModifier )
  1175. {
  1176. update();
  1177. }
  1178. break;
  1179. }
  1180. update();
  1181. }
  1182. void PianoRoll::leaveEvent(QEvent * e )
  1183. {
  1184. QWidget::leaveEvent( e );
  1185. s_textFloat->hide();
  1186. update(); // cleaning inner mouse-related graphics
  1187. }
  1188. int PianoRoll::noteEditTop() const
  1189. {
  1190. return keyAreaBottom() + NOTE_EDIT_RESIZE_BAR;
  1191. }
  1192. int PianoRoll::noteEditBottom() const
  1193. {
  1194. return height() - PR_BOTTOM_MARGIN;
  1195. }
  1196. int PianoRoll::noteEditRight() const
  1197. {
  1198. return width() - PR_RIGHT_MARGIN;
  1199. }
  1200. int PianoRoll::noteEditLeft() const
  1201. {
  1202. return m_whiteKeyWidth;
  1203. }
  1204. int PianoRoll::keyAreaTop() const
  1205. {
  1206. return PR_TOP_MARGIN;
  1207. }
  1208. int PianoRoll::keyAreaBottom() const
  1209. {
  1210. return height() - PR_BOTTOM_MARGIN - m_notesEditHeight;
  1211. }
  1212. void PianoRoll::mousePressEvent(QMouseEvent * me )
  1213. {
  1214. m_startedWithShift = me->modifiers() & Qt::ShiftModifier;
  1215. if( ! hasValidPattern() )
  1216. {
  1217. return;
  1218. }
  1219. // -- Razor
  1220. if (m_editMode == ModeEditRazor && me->button() == Qt::LeftButton)
  1221. {
  1222. NoteVector n;
  1223. Note* note = noteUnderMouse();
  1224. if (note)
  1225. {
  1226. n.append(note);
  1227. updateRazorPos(me);
  1228. // Call splitNotes for the note
  1229. m_pattern->splitNotes(n, TimePos(m_razorTickPos));
  1230. // Allow cancel razor mode when shift is released (if hold down).
  1231. m_firstRazorSplit = false;
  1232. }
  1233. // Keep in razor mode while SHIFT is hold during cut
  1234. if (!(me->modifiers() & Qt::ShiftModifier))
  1235. {
  1236. cancelRazorAction();
  1237. }
  1238. update();
  1239. return;
  1240. }
  1241. if( m_editMode == ModeEditDetuning && noteUnderMouse() )
  1242. {
  1243. static QPointer<AutomationPattern> detuningPattern = nullptr;
  1244. if (detuningPattern.data() != nullptr)
  1245. {
  1246. detuningPattern->disconnect(this);
  1247. }
  1248. Note* n = noteUnderMouse();
  1249. if (n->detuning() == nullptr)
  1250. {
  1251. n->createDetuning();
  1252. }
  1253. detuningPattern = n->detuning()->automationPattern();
  1254. connect(detuningPattern.data(), SIGNAL(dataChanged()), this, SLOT(update()));
  1255. gui->automationEditor()->open(detuningPattern);
  1256. return;
  1257. }
  1258. // if holding control, go to selection mode unless shift is also pressed
  1259. if( me->modifiers() & Qt::ControlModifier && m_editMode != ModeSelect )
  1260. {
  1261. m_ctrlMode = m_editMode;
  1262. m_editMode = ModeSelect;
  1263. setCursor( Qt::ArrowCursor );
  1264. update();
  1265. }
  1266. // keep track of the point where the user clicked down
  1267. if( me->button() == Qt::LeftButton )
  1268. {
  1269. m_moveStartX = me->x();
  1270. m_moveStartY = me->y();
  1271. }
  1272. if(me->button() == Qt::LeftButton &&
  1273. me->y() > keyAreaBottom() && me->y() < noteEditTop())
  1274. {
  1275. // resizing the note edit area
  1276. m_action = ActionResizeNoteEditArea;
  1277. return;
  1278. }
  1279. if( me->y() > PR_TOP_MARGIN )
  1280. {
  1281. bool edit_note = ( me->y() > noteEditTop() );
  1282. int key_num = getKey( me->y() );
  1283. int x = me->x();
  1284. if (x > m_whiteKeyWidth)
  1285. {
  1286. // set, move or resize note
  1287. x -= m_whiteKeyWidth;
  1288. // get tick in which the user clicked
  1289. int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
  1290. m_currentPosition;
  1291. // get note-vector of current pattern
  1292. const NoteVector & notes = m_pattern->notes();
  1293. // will be our iterator in the following loop
  1294. NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
  1295. // loop through whole note-vector...
  1296. for( int i = 0; i < notes.size(); ++i )
  1297. {
  1298. Note *note = *it;
  1299. TimePos len = note->length();
  1300. if( len < 0 )
  1301. {
  1302. len = 4;
  1303. }
  1304. // and check whether the user clicked on an
  1305. // existing note or an edit-line
  1306. if( pos_ticks >= note->pos() &&
  1307. len > 0 &&
  1308. (
  1309. ( ! edit_note &&
  1310. pos_ticks <= note->pos() + len &&
  1311. note->key() == key_num )
  1312. ||
  1313. ( edit_note &&
  1314. pos_ticks <= note->pos() +
  1315. NOTE_EDIT_LINE_WIDTH * TimePos::ticksPerBar() / m_ppb )
  1316. )
  1317. )
  1318. {
  1319. break;
  1320. }
  1321. --it;
  1322. }
  1323. // first check whether the user clicked in note-edit-
  1324. // area
  1325. if( edit_note )
  1326. {
  1327. m_pattern->addJournalCheckPoint();
  1328. // scribble note edit changes
  1329. mouseMoveEvent( me );
  1330. return;
  1331. }
  1332. // left button??
  1333. else if( me->button() == Qt::LeftButton &&
  1334. m_editMode == ModeDraw )
  1335. {
  1336. // whether this action creates new note(s) or not
  1337. bool is_new_note = false;
  1338. Note * created_new_note = NULL;
  1339. // did it reach end of vector because
  1340. // there's no note??
  1341. if( it == notes.begin()-1 )
  1342. {
  1343. is_new_note = true;
  1344. m_pattern->addJournalCheckPoint();
  1345. // then set new note
  1346. // clear selection and select this new note
  1347. clearSelectedNotes();
  1348. // +32 to quanitize the note correctly when placing notes with
  1349. // the mouse. We do this here instead of in note.quantized
  1350. // because live notes should still be quantized at the half.
  1351. TimePos note_pos( pos_ticks - ( quantization() / 2 ) );
  1352. TimePos note_len( newNoteLen() );
  1353. Note new_note( note_len, note_pos, key_num );
  1354. new_note.setSelected( true );
  1355. new_note.setPanning( m_lastNotePanning );
  1356. new_note.setVolume( m_lastNoteVolume );
  1357. created_new_note = m_pattern->addNote( new_note );
  1358. const InstrumentFunctionNoteStacking::Chord & chord = InstrumentFunctionNoteStacking::ChordTable::getInstance()
  1359. .getChordByName( m_chordModel.currentText() );
  1360. if( ! chord.isEmpty() )
  1361. {
  1362. // if a chord is selected, create following notes in chord
  1363. // or arpeggio mode
  1364. const bool arpeggio = me->modifiers() & Qt::ShiftModifier;
  1365. for( int i = 1; i < chord.size(); i++ )
  1366. {
  1367. if( arpeggio )
  1368. {
  1369. note_pos += note_len;
  1370. }
  1371. Note new_note( note_len, note_pos, key_num + chord[i] );
  1372. new_note.setSelected( true );
  1373. new_note.setPanning( m_lastNotePanning );
  1374. new_note.setVolume( m_lastNoteVolume );
  1375. m_pattern->addNote( new_note );
  1376. }
  1377. }
  1378. // reset it so that it can be used for
  1379. // ops (move, resize) after this
  1380. // code-block
  1381. it = notes.begin();
  1382. while( it != notes.end() && *it != created_new_note )
  1383. {
  1384. ++it;
  1385. }
  1386. }
  1387. Note *current_note = *it;
  1388. m_currentNote = current_note;
  1389. m_lastNotePanning = current_note->getPanning();
  1390. m_lastNoteVolume = current_note->getVolume();
  1391. m_lenOfNewNotes = current_note->length();
  1392. // remember which key and tick we started with
  1393. m_mouseDownKey = m_startKey;
  1394. m_mouseDownTick = m_currentPosition;
  1395. //If clicked on an unselected note, remove selection and select that new note
  1396. if (!m_currentNote->selected())
  1397. {
  1398. clearSelectedNotes();
  1399. m_currentNote->setSelected( true );
  1400. }
  1401. auto selectedNotes = getSelectedNotes();
  1402. m_moveBoundaryLeft = selectedNotes.first()->pos().getTicks();
  1403. m_moveBoundaryRight = selectedNotes.first()->endPos();
  1404. m_moveBoundaryBottom = selectedNotes.first()->key();
  1405. m_moveBoundaryTop = m_moveBoundaryBottom;
  1406. //Figure out the bounding box of all the selected notes
  1407. for (Note *note: selectedNotes)
  1408. {
  1409. // remember note starting positions
  1410. note->setOldKey( note->key() );
  1411. note->setOldPos( note->pos() );
  1412. note->setOldLength( note->length() );
  1413. m_moveBoundaryLeft = qMin(note->pos().getTicks(), (tick_t) m_moveBoundaryLeft);
  1414. m_moveBoundaryRight = qMax((int) note->endPos(), m_moveBoundaryRight);
  1415. m_moveBoundaryBottom = qMin(note->key(), m_moveBoundaryBottom);
  1416. m_moveBoundaryTop = qMax(note->key(), m_moveBoundaryTop);
  1417. }
  1418. // clicked at the "tail" of the note?
  1419. if( pos_ticks * m_ppb / TimePos::ticksPerBar() >
  1420. m_currentNote->endPos() * m_ppb / TimePos::ticksPerBar() - RESIZE_AREA_WIDTH
  1421. && m_currentNote->length() > 0 )
  1422. {
  1423. m_pattern->addJournalCheckPoint();
  1424. // then resize the note
  1425. m_action = ActionResizeNote;
  1426. //Calculate the minimum length we should allow when resizing
  1427. //each note, and let all notes use the smallest one found
  1428. m_minResizeLen = quantization();
  1429. for (Note *note : selectedNotes)
  1430. {
  1431. //Notes from the BB editor can have a negative length, so
  1432. //change their length to the displayed one before resizing
  1433. if (note->oldLength() <= 0) { note->setOldLength(4); }
  1434. //Let the note be sized down by quantized increments, stopping
  1435. //when the next step down would result in a negative length
  1436. int thisMin = note->oldLength() % quantization();
  1437. //The initial value for m_minResizeLen is the minimum length of
  1438. //a note divisible by the current Q. Therefore we ignore notes
  1439. //where thisMin == 0 when checking for a new minimum
  1440. if (thisMin > 0 && thisMin < m_minResizeLen) { m_minResizeLen = thisMin; }
  1441. }
  1442. // set resize-cursor
  1443. setCursor( Qt::SizeHorCursor );
  1444. }
  1445. else
  1446. {
  1447. if( ! created_new_note )
  1448. {
  1449. m_pattern->addJournalCheckPoint();
  1450. }
  1451. // otherwise move it
  1452. m_action = ActionMoveNote;
  1453. // set move-cursor
  1454. setCursor( Qt::SizeAllCursor );
  1455. // if they're holding shift, copy all selected notes
  1456. if( ! is_new_note && me->modifiers() & Qt::ShiftModifier )
  1457. {
  1458. for (Note *note: selectedNotes)
  1459. {
  1460. Note *newNote = m_pattern->addNote(*note, false);
  1461. newNote->setSelected(false);
  1462. }
  1463. if (!selectedNotes.empty())
  1464. {
  1465. // added new notes, so must update engine, song, etc
  1466. Engine::getSong()->setModified();
  1467. update();
  1468. gui->songEditor()->update();
  1469. }
  1470. }
  1471. // play the note
  1472. testPlayNote( m_currentNote );
  1473. }
  1474. Engine::getSong()->setModified();
  1475. }
  1476. else if( ( me->buttons() == Qt::RightButton &&
  1477. m_editMode == ModeDraw ) ||
  1478. m_editMode == ModeErase )
  1479. {
  1480. // erase single note
  1481. m_mouseDownRight = true;
  1482. if( it != notes.begin()-1 )
  1483. {
  1484. m_pattern->addJournalCheckPoint();
  1485. m_pattern->removeNote( *it );
  1486. Engine::getSong()->setModified();
  1487. }
  1488. }
  1489. else if( me->button() == Qt::LeftButton &&
  1490. m_editMode == ModeSelect )
  1491. {
  1492. // select an area of notes
  1493. m_selectStartTick = pos_ticks;
  1494. m_selectedTick = 0;
  1495. m_selectStartKey = key_num;
  1496. m_selectedKeys = 1;
  1497. m_action = ActionSelectNotes;
  1498. // call mousemove to fix glitch where selection
  1499. // appears in wrong spot on mousedown
  1500. mouseMoveEvent( me );
  1501. }
  1502. update();
  1503. }
  1504. else if( me->y() < keyAreaBottom() )
  1505. {
  1506. // reference to last key needed for both
  1507. // right click (used for copy all keys on note)
  1508. // and for playing the key when left-clicked
  1509. m_lastKey = key_num;
  1510. // clicked on keyboard on the left
  1511. if( me->buttons() == Qt::RightButton )
  1512. {
  1513. // right click - tone marker contextual menu
  1514. m_pianoKeySelected = getKey( me->y() );
  1515. m_semiToneMarkerMenu->popup( mapToGlobal( QPoint( me->x(), me->y() ) ) );
  1516. }
  1517. else if( me->buttons() == Qt::LeftButton )
  1518. {
  1519. // left click - play the note
  1520. int v = ((float) x) / ((float) m_whiteKeyWidth) * MidiDefaultVelocity;
  1521. m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(key_num, v);
  1522. // if a chord is set, play the chords notes as well:
  1523. playChordNotes(key_num, v);
  1524. }
  1525. }
  1526. else
  1527. {
  1528. if( me->buttons() == Qt::LeftButton )
  1529. {
  1530. // clicked in the box below the keys to the left of note edit area
  1531. m_noteEditMode = (NoteEditMode)(((int)m_noteEditMode)+1);
  1532. if( m_noteEditMode == NoteEditCount )
  1533. {
  1534. m_noteEditMode = (NoteEditMode) 0;
  1535. }
  1536. repaint();
  1537. }
  1538. else if( me->buttons() == Qt::RightButton )
  1539. {
  1540. // pop menu asking which one they want to edit
  1541. m_noteEditMenu->popup( mapToGlobal( QPoint( me->x(), me->y() ) ) );
  1542. }
  1543. }
  1544. }
  1545. }
  1546. void PianoRoll::mouseDoubleClickEvent(QMouseEvent * me )
  1547. {
  1548. if( ! hasValidPattern() )
  1549. {
  1550. return;
  1551. }
  1552. // if they clicked in the note edit area, enter value for the volume bar
  1553. if( me->x() > noteEditLeft() && me->x() < noteEditRight()
  1554. && me->y() > noteEditTop() && me->y() < noteEditBottom() )
  1555. {
  1556. // get values for going through notes
  1557. int pixel_range = 4;
  1558. int x = me->x() - m_whiteKeyWidth;
  1559. const int ticks_start = ( x-pixel_range/2 ) *
  1560. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  1561. const int ticks_end = ( x+pixel_range/2 ) *
  1562. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  1563. const int ticks_middle = x * TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  1564. // go through notes to figure out which one we want to change
  1565. bool altPressed = me->modifiers() & Qt::AltModifier;
  1566. NoteVector nv;
  1567. for ( Note * i : m_pattern->notes() )
  1568. {
  1569. if( i->withinRange( ticks_start, ticks_end ) || ( i->selected() && !altPressed ) )
  1570. {
  1571. nv += i;
  1572. }
  1573. }
  1574. // make sure we're on a note
  1575. if( nv.size() > 0 )
  1576. {
  1577. const Note * closest = NULL;
  1578. int closest_dist = 9999999;
  1579. // if we caught multiple notes and we're not editing a
  1580. // selection, find the closest...
  1581. if( nv.size() > 1 && !isSelection() )
  1582. {
  1583. for ( const Note * i : nv )
  1584. {
  1585. const int dist = qAbs( i->pos().getTicks() - ticks_middle );
  1586. if( dist < closest_dist ) { closest = i; closest_dist = dist; }
  1587. }
  1588. // ... then remove all notes from the vector that aren't on the same exact time
  1589. NoteVector::Iterator it = nv.begin();
  1590. while( it != nv.end() )
  1591. {
  1592. const Note *note = *it;
  1593. if( note->pos().getTicks() != closest->pos().getTicks() )
  1594. {
  1595. it = nv.erase( it );
  1596. }
  1597. else
  1598. {
  1599. it++;
  1600. }
  1601. }
  1602. }
  1603. enterValue( &nv );
  1604. }
  1605. }
  1606. else
  1607. {
  1608. QWidget::mouseDoubleClickEvent(me);
  1609. }
  1610. }
  1611. void PianoRoll::testPlayNote( Note * n )
  1612. {
  1613. m_lastKey = n->key();
  1614. if( ! n->isPlaying() && ! m_recording && ! m_stepRecorder.isRecording())
  1615. {
  1616. n->setIsPlaying( true );
  1617. const int baseVelocity = m_pattern->instrumentTrack()->midiPort()->baseVelocity();
  1618. m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(n->key(), n->midiVelocity(baseVelocity));
  1619. // if a chord is set, play the chords notes as well:
  1620. playChordNotes(n->key(), n->midiVelocity(baseVelocity));
  1621. MidiEvent event( MidiMetaEvent, -1, n->key(), panningToMidi( n->getPanning() ) );
  1622. event.setMetaEvent( MidiNotePanning );
  1623. m_pattern->instrumentTrack()->processInEvent( event, 0 );
  1624. }
  1625. }
  1626. void PianoRoll::pauseTestNotes( bool pause )
  1627. {
  1628. for (Note *note : m_pattern->notes())
  1629. {
  1630. if( note->isPlaying() )
  1631. {
  1632. if( pause )
  1633. {
  1634. // stop note
  1635. m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( note->key() );
  1636. // if a chord was set, stop the chords notes as well:
  1637. pauseChordNotes(note->key());
  1638. }
  1639. else
  1640. {
  1641. // start note
  1642. note->setIsPlaying( false );
  1643. testPlayNote( note );
  1644. }
  1645. }
  1646. }
  1647. }
  1648. void PianoRoll::playChordNotes(int key, int velocity)
  1649. {
  1650. // if a chord is set, play the chords notes beside the base note.
  1651. Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
  1652. const InstrumentFunctionNoteStacking::Chord & chord =
  1653. InstrumentFunctionNoteStacking::ChordTable::getInstance().getChordByName(
  1654. m_chordModel.currentText());
  1655. if (!chord.isEmpty())
  1656. {
  1657. for (int i = 1; i < chord.size(); ++i)
  1658. {
  1659. pianoModel->handleKeyPress(key + chord[i], velocity);
  1660. }
  1661. }
  1662. }
  1663. void PianoRoll::pauseChordNotes(int key)
  1664. {
  1665. // if a chord was set, stop the chords notes beside the base note.
  1666. Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
  1667. const InstrumentFunctionNoteStacking::Chord & chord =
  1668. InstrumentFunctionNoteStacking::ChordTable::getInstance().getChordByName(
  1669. m_chordModel.currentText());
  1670. if (!chord.isEmpty())
  1671. {
  1672. for (int i = 1; i < chord.size(); ++i)
  1673. {
  1674. pianoModel->handleKeyRelease(key + chord[i]);
  1675. }
  1676. }
  1677. }
  1678. void PianoRoll::setRazorAction()
  1679. {
  1680. if (m_editMode != ModeEditRazor)
  1681. {
  1682. m_firstRazorSplit = true;
  1683. m_razorMode = m_editMode;
  1684. m_editMode = ModeEditRazor;
  1685. m_action = ActionRazor;
  1686. setCursor(Qt::ArrowCursor);
  1687. update();
  1688. }
  1689. }
  1690. void PianoRoll::cancelRazorAction()
  1691. {
  1692. m_editMode = m_razorMode;
  1693. m_action = ActionNone;
  1694. update();
  1695. }
  1696. void PianoRoll::testPlayKey( int key, int velocity, int pan )
  1697. {
  1698. Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
  1699. // turn off old key
  1700. pianoModel->handleKeyRelease( m_lastKey );
  1701. // if a chord was set, stop the chords notes as well
  1702. pauseChordNotes(m_lastKey);
  1703. // remember which one we're playing
  1704. m_lastKey = key;
  1705. // play new key
  1706. pianoModel->handleKeyPress( key, velocity );
  1707. // and if a chord is set, play chord notes:
  1708. playChordNotes(key, velocity);
  1709. }
  1710. void PianoRoll::computeSelectedNotes(bool shift)
  1711. {
  1712. if( m_selectStartTick == 0 &&
  1713. m_selectedTick == 0 &&
  1714. m_selectStartKey == 0 &&
  1715. m_selectedKeys == 0 )
  1716. {
  1717. // don't bother, there's no selection
  1718. return;
  1719. }
  1720. // setup selection-vars
  1721. int sel_pos_start = m_selectStartTick;
  1722. int sel_pos_end = m_selectStartTick+m_selectedTick;
  1723. if( sel_pos_start > sel_pos_end )
  1724. {
  1725. qSwap<int>( sel_pos_start, sel_pos_end );
  1726. }
  1727. int sel_key_start = m_selectStartKey - m_startKey + 1;
  1728. int sel_key_end = sel_key_start + m_selectedKeys;
  1729. if( sel_key_start > sel_key_end )
  1730. {
  1731. qSwap<int>( sel_key_start, sel_key_end );
  1732. }
  1733. //int y_base = noteEditTop() - 1;
  1734. if( hasValidPattern() )
  1735. {
  1736. for( Note *note : m_pattern->notes() )
  1737. {
  1738. // make a new selection unless they're holding shift
  1739. if( ! shift )
  1740. {
  1741. note->setSelected( false );
  1742. }
  1743. int len_ticks = note->length();
  1744. if( len_ticks == 0 )
  1745. {
  1746. continue;
  1747. }
  1748. else if( len_ticks < 0 )
  1749. {
  1750. len_ticks = 4;
  1751. }
  1752. const int key = note->key() - m_startKey + 1;
  1753. int pos_ticks = note->pos();
  1754. // if the selection even barely overlaps the note
  1755. if( key > sel_key_start &&
  1756. key <= sel_key_end &&
  1757. pos_ticks + len_ticks > sel_pos_start &&
  1758. pos_ticks < sel_pos_end )
  1759. {
  1760. // remove from selection when holding shift
  1761. bool selected = shift && note->selected();
  1762. note->setSelected( ! selected);
  1763. }
  1764. }
  1765. }
  1766. removeSelection();
  1767. update();
  1768. }
  1769. void PianoRoll::mouseReleaseEvent( QMouseEvent * me )
  1770. {
  1771. bool mustRepaint = false;
  1772. s_textFloat->hide();
  1773. // Quit razor mode if we pressed and released the right mouse button
  1774. if (m_editMode == ModeEditRazor && me->button() == Qt::RightButton)
  1775. {
  1776. cancelRazorAction();
  1777. }
  1778. if( me->button() & Qt::LeftButton )
  1779. {
  1780. mustRepaint = true;
  1781. if( m_action == ActionSelectNotes && m_editMode == ModeSelect )
  1782. {
  1783. // select the notes within the selection rectangle and
  1784. // then destroy the selection rectangle
  1785. computeSelectedNotes(
  1786. me->modifiers() & Qt::ShiftModifier );
  1787. }
  1788. else if( m_action == ActionMoveNote )
  1789. {
  1790. // we moved one or more notes so they have to be
  1791. // moved properly according to new starting-
  1792. // time in the note-array of pattern
  1793. m_pattern->rearrangeAllNotes();
  1794. }
  1795. if( m_action == ActionMoveNote || m_action == ActionResizeNote )
  1796. {
  1797. // if we only moved one note, deselect it so we can
  1798. // edit the notes in the note edit area
  1799. if( selectionCount() == 1 )
  1800. {
  1801. clearSelectedNotes();
  1802. }
  1803. }
  1804. }
  1805. if( me->button() & Qt::RightButton )
  1806. {
  1807. m_mouseDownRight = false;
  1808. mustRepaint = true;
  1809. }
  1810. if( hasValidPattern() )
  1811. {
  1812. // turn off all notes that are playing
  1813. for ( Note *note : m_pattern->notes() )
  1814. {
  1815. if( note->isPlaying() )
  1816. {
  1817. m_pattern->instrumentTrack()->pianoModel()->
  1818. handleKeyRelease( note->key() );
  1819. pauseChordNotes(note->key());
  1820. note->setIsPlaying( false );
  1821. }
  1822. }
  1823. // stop playing keys that we let go of
  1824. m_pattern->instrumentTrack()->pianoModel()->
  1825. handleKeyRelease( m_lastKey );
  1826. pauseChordNotes(m_lastKey);
  1827. }
  1828. m_currentNote = NULL;
  1829. if (m_action != ActionRazor)
  1830. {
  1831. m_action = ActionNone;
  1832. }
  1833. if( m_editMode == ModeDraw )
  1834. {
  1835. setCursor( Qt::ArrowCursor );
  1836. }
  1837. if( mustRepaint )
  1838. {
  1839. repaint();
  1840. }
  1841. }
  1842. void PianoRoll::mouseMoveEvent( QMouseEvent * me )
  1843. {
  1844. if( ! hasValidPattern() )
  1845. {
  1846. update();
  1847. return;
  1848. }
  1849. if( m_action == ActionNone && me->buttons() == 0 )
  1850. {
  1851. // When cursor is between note editing area and volume/panning
  1852. // area show vertical size cursor.
  1853. if( me->y() > keyAreaBottom() && me->y() < noteEditTop() )
  1854. {
  1855. setCursor( Qt::SizeVerCursor );
  1856. return;
  1857. }
  1858. }
  1859. else if( m_action == ActionResizeNoteEditArea )
  1860. {
  1861. // Don't try to show more keys than the full keyboard, bail if trying to
  1862. if (m_pianoKeysVisible == NumKeys && me->y() > m_moveStartY)
  1863. {
  1864. return;
  1865. }
  1866. int newHeight = height() - me->y();
  1867. if (me->y() < KEY_AREA_MIN_HEIGHT)
  1868. {
  1869. newHeight = height() - KEY_AREA_MIN_HEIGHT -
  1870. PR_TOP_MARGIN - PR_BOTTOM_MARGIN; // - NOTE_EDIT_RESIZE_BAR
  1871. }
  1872. // change m_notesEditHeight and then repaint
  1873. m_notesEditHeight = qMax(NOTE_EDIT_MIN_HEIGHT, newHeight);
  1874. m_userSetNotesEditHeight = m_notesEditHeight;
  1875. m_stepRecorderWidget.setBottomMargin(PR_BOTTOM_MARGIN + m_notesEditHeight);
  1876. updateScrollbars();
  1877. updatePositionLineHeight();
  1878. repaint();
  1879. return;
  1880. }
  1881. // Update Razor position if we are on Razor mode
  1882. if (m_editMode == ModeEditRazor)
  1883. {
  1884. updateRazorPos(me);
  1885. }
  1886. if( me->y() > PR_TOP_MARGIN || m_action != ActionNone )
  1887. {
  1888. bool edit_note = ( me->y() > noteEditTop() )
  1889. && m_action != ActionSelectNotes;
  1890. int key_num = getKey( me->y() );
  1891. int x = me->x();
  1892. // see if they clicked on the keyboard on the left
  1893. if (x < m_whiteKeyWidth && m_action == ActionNone
  1894. && ! edit_note && key_num != m_lastKey
  1895. && me->buttons() & Qt::LeftButton )
  1896. {
  1897. // clicked on a key, play the note
  1898. testPlayKey(key_num, ((float) x) / ((float) m_whiteKeyWidth) * MidiDefaultVelocity, 0);
  1899. update();
  1900. return;
  1901. }
  1902. x -= m_whiteKeyWidth;
  1903. if( me->buttons() & Qt::LeftButton
  1904. && m_editMode == ModeDraw
  1905. && (m_action == ActionMoveNote || m_action == ActionResizeNote ) )
  1906. {
  1907. // handle moving notes and resizing them
  1908. bool replay_note = key_num != m_lastKey
  1909. && m_action == ActionMoveNote;
  1910. if( replay_note || ( m_action == ActionMoveNote && ( me->modifiers() & Qt::ShiftModifier ) && ! m_startedWithShift ) )
  1911. {
  1912. pauseTestNotes();
  1913. }
  1914. dragNotes( me->x(), me->y(),
  1915. me->modifiers() & Qt::AltModifier,
  1916. me->modifiers() & Qt::ShiftModifier,
  1917. me->modifiers() & Qt::ControlModifier );
  1918. if( replay_note && m_action == ActionMoveNote && ! ( ( me->modifiers() & Qt::ShiftModifier ) && ! m_startedWithShift ) )
  1919. {
  1920. pauseTestNotes( false );
  1921. }
  1922. }
  1923. else if( m_editMode != ModeErase &&
  1924. ( edit_note || m_action == ActionChangeNoteProperty ) &&
  1925. ( me->buttons() & Qt::LeftButton || me->buttons() & Qt::MiddleButton
  1926. || ( me->buttons() & Qt::RightButton && me->modifiers() & Qt::ShiftModifier ) ) )
  1927. {
  1928. // editing note properties
  1929. // Change notes within a certain pixel range of where
  1930. // the mouse cursor is
  1931. int pixel_range = 14;
  1932. // convert to ticks so that we can check which notes
  1933. // are in the range
  1934. int ticks_start = ( x-pixel_range/2 ) *
  1935. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  1936. int ticks_end = ( x+pixel_range/2 ) *
  1937. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  1938. // get note-vector of current pattern
  1939. const NoteVector & notes = m_pattern->notes();
  1940. // determine what volume/panning to set note to
  1941. // if middle-click, set to defaults
  1942. volume_t vol = DefaultVolume;
  1943. panning_t pan = DefaultPanning;
  1944. if( me->buttons() & Qt::LeftButton )
  1945. {
  1946. vol = qBound<int>( MinVolume,
  1947. MinVolume +
  1948. ( ( (float)noteEditBottom() ) - ( (float)me->y() ) ) /
  1949. ( (float)( noteEditBottom() - noteEditTop() ) ) *
  1950. ( MaxVolume - MinVolume ),
  1951. MaxVolume );
  1952. pan = qBound<int>( PanningLeft,
  1953. PanningLeft +
  1954. ( (float)( noteEditBottom() - me->y() ) ) /
  1955. ( (float)( noteEditBottom() - noteEditTop() ) ) *
  1956. ( (float)( PanningRight - PanningLeft ) ),
  1957. PanningRight);
  1958. }
  1959. if( m_noteEditMode == NoteEditVolume )
  1960. {
  1961. m_lastNoteVolume = vol;
  1962. showVolTextFloat( vol, me->pos() );
  1963. }
  1964. else if( m_noteEditMode == NoteEditPanning )
  1965. {
  1966. m_lastNotePanning = pan;
  1967. showPanTextFloat( pan, me->pos() );
  1968. }
  1969. // When alt is pressed we only edit the note under the cursor
  1970. bool altPressed = me->modifiers() & Qt::AltModifier;
  1971. // We iterate from last note in pattern to the first,
  1972. // chronologically
  1973. NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
  1974. for( int i = 0; i < notes.size(); ++i )
  1975. {
  1976. Note* n = *it;
  1977. bool isUnderPosition = n->withinRange( ticks_start, ticks_end );
  1978. // Play note under the cursor
  1979. if ( isUnderPosition ) { testPlayNote( n ); }
  1980. // If note is:
  1981. // Under the cursor, when there is no selection
  1982. // Selected, and alt is not pressed
  1983. // Under the cursor, selected, and alt is pressed
  1984. if ( ( isUnderPosition && !isSelection() ) ||
  1985. ( n->selected() && !altPressed ) ||
  1986. ( isUnderPosition && n->selected() && altPressed )
  1987. )
  1988. {
  1989. if( m_noteEditMode == NoteEditVolume )
  1990. {
  1991. n->setVolume( vol );
  1992. const int baseVelocity = m_pattern->instrumentTrack()->midiPort()->baseVelocity();
  1993. m_pattern->instrumentTrack()->processInEvent( MidiEvent( MidiKeyPressure, -1, n->key(), n->midiVelocity( baseVelocity ) ) );
  1994. }
  1995. else if( m_noteEditMode == NoteEditPanning )
  1996. {
  1997. n->setPanning( pan );
  1998. MidiEvent evt( MidiMetaEvent, -1, n->key(), panningToMidi( pan ) );
  1999. evt.setMetaEvent( MidiNotePanning );
  2000. m_pattern->instrumentTrack()->processInEvent( evt );
  2001. }
  2002. }
  2003. else if( n->isPlaying() && !isSelection() )
  2004. {
  2005. // mouse not over this note, stop playing it.
  2006. m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( n->key() );
  2007. pauseChordNotes(n->key());
  2008. n->setIsPlaying( false );
  2009. }
  2010. --it;
  2011. }
  2012. // Emit pattern has changed
  2013. m_pattern->dataChanged();
  2014. }
  2015. else if( me->buttons() == Qt::NoButton && m_editMode == ModeDraw )
  2016. {
  2017. // set move- or resize-cursor
  2018. // get tick in which the cursor is posated
  2019. int pos_ticks = ( x * TimePos::ticksPerBar() ) /
  2020. m_ppb + m_currentPosition;
  2021. // get note-vector of current pattern
  2022. const NoteVector & notes = m_pattern->notes();
  2023. // will be our iterator in the following loop
  2024. NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
  2025. // loop through whole note-vector...
  2026. for( int i = 0; i < notes.size(); ++i )
  2027. {
  2028. Note *note = *it;
  2029. // and check whether the cursor is over an
  2030. // existing note
  2031. if( pos_ticks >= note->pos() &&
  2032. pos_ticks <= note->pos() +
  2033. note->length() &&
  2034. note->key() == key_num &&
  2035. note->length() > 0 )
  2036. {
  2037. break;
  2038. }
  2039. --it;
  2040. }
  2041. // did it reach end of vector because there's
  2042. // no note??
  2043. if( it != notes.begin()-1 )
  2044. {
  2045. Note *note = *it;
  2046. // x coordinate of the right edge of the note
  2047. int noteRightX = ( note->pos() + note->length() -
  2048. m_currentPosition) * m_ppb/TimePos::ticksPerBar();
  2049. // cursor at the "tail" of the note?
  2050. bool atTail = note->length() > 0 && x > noteRightX -
  2051. RESIZE_AREA_WIDTH;
  2052. Qt::CursorShape cursorShape = atTail ? Qt::SizeHorCursor :
  2053. Qt::SizeAllCursor;
  2054. setCursor( cursorShape );
  2055. }
  2056. else
  2057. {
  2058. // the cursor is over no note, so restore cursor
  2059. setCursor( Qt::ArrowCursor );
  2060. }
  2061. }
  2062. else if( me->buttons() & Qt::LeftButton &&
  2063. m_editMode == ModeSelect &&
  2064. m_action == ActionSelectNotes )
  2065. {
  2066. // change size of selection
  2067. // get tick in which the cursor is posated
  2068. int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
  2069. m_currentPosition;
  2070. m_selectedTick = pos_ticks - m_selectStartTick;
  2071. if( (int) m_selectStartTick + m_selectedTick < 0 )
  2072. {
  2073. m_selectedTick = -static_cast<int>(
  2074. m_selectStartTick );
  2075. }
  2076. m_selectedKeys = key_num - m_selectStartKey;
  2077. if( key_num <= m_selectStartKey )
  2078. {
  2079. --m_selectedKeys;
  2080. }
  2081. }
  2082. else if( ( m_editMode == ModeDraw && me->buttons() & Qt::RightButton )
  2083. || ( m_editMode == ModeErase && me->buttons() ) )
  2084. {
  2085. // holding down right-click to delete notes or holding down
  2086. // any key if in erase mode
  2087. // get tick in which the user clicked
  2088. int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
  2089. m_currentPosition;
  2090. // get note-vector of current pattern
  2091. const NoteVector & notes = m_pattern->notes();
  2092. // will be our iterator in the following loop
  2093. NoteVector::ConstIterator it = notes.begin();
  2094. // loop through whole note-vector...
  2095. while( it != notes.end() )
  2096. {
  2097. Note *note = *it;
  2098. TimePos len = note->length();
  2099. if( len < 0 )
  2100. {
  2101. len = 4;
  2102. }
  2103. // and check whether the user clicked on an
  2104. // existing note or an edit-line
  2105. if( pos_ticks >= note->pos() &&
  2106. len > 0 &&
  2107. (
  2108. ( ! edit_note &&
  2109. pos_ticks <= note->pos() + len &&
  2110. note->key() == key_num )
  2111. ||
  2112. ( edit_note &&
  2113. pos_ticks <= note->pos() +
  2114. NOTE_EDIT_LINE_WIDTH *
  2115. TimePos::ticksPerBar() /
  2116. m_ppb )
  2117. )
  2118. )
  2119. {
  2120. // delete this note
  2121. m_pattern->removeNote( note );
  2122. Engine::getSong()->setModified();
  2123. }
  2124. else
  2125. {
  2126. ++it;
  2127. }
  2128. }
  2129. }
  2130. else if (me->buttons() == Qt::NoButton && m_editMode != ModeDraw && m_editMode != ModeEditRazor)
  2131. {
  2132. // Is needed to restore cursor when it previously was set to
  2133. // Qt::SizeVerCursor (between keyAreaBottom and noteEditTop)
  2134. setCursor( Qt::ArrowCursor );
  2135. }
  2136. }
  2137. else
  2138. {
  2139. if( me->buttons() & Qt::LeftButton &&
  2140. m_editMode == ModeSelect &&
  2141. m_action == ActionSelectNotes )
  2142. {
  2143. int x = me->x() - m_whiteKeyWidth;
  2144. if( x < 0 && m_currentPosition > 0 )
  2145. {
  2146. x = 0;
  2147. QCursor::setPos( mapToGlobal( QPoint(
  2148. m_whiteKeyWidth,
  2149. me->y() ) ) );
  2150. if( m_currentPosition >= 4 )
  2151. {
  2152. m_leftRightScroll->setValue(
  2153. m_currentPosition - 4 );
  2154. }
  2155. else
  2156. {
  2157. m_leftRightScroll->setValue( 0 );
  2158. }
  2159. }
  2160. else if (x > width() - m_whiteKeyWidth)
  2161. {
  2162. x = width() - m_whiteKeyWidth;
  2163. QCursor::setPos( mapToGlobal( QPoint( width(),
  2164. me->y() ) ) );
  2165. m_leftRightScroll->setValue( m_currentPosition +
  2166. 4 );
  2167. }
  2168. // get tick in which the cursor is posated
  2169. int pos_ticks = x * TimePos::ticksPerBar()/ m_ppb +
  2170. m_currentPosition;
  2171. m_selectedTick = pos_ticks -
  2172. m_selectStartTick;
  2173. if( (int) m_selectStartTick + m_selectedTick <
  2174. 0 )
  2175. {
  2176. m_selectedTick = -static_cast<int>(
  2177. m_selectStartTick );
  2178. }
  2179. int key_num = getKey( me->y() );
  2180. int visible_keys = ( height() - PR_TOP_MARGIN -
  2181. PR_BOTTOM_MARGIN -
  2182. m_notesEditHeight ) /
  2183. m_keyLineHeight + 2;
  2184. const int s_key = m_startKey - 1;
  2185. if( key_num <= s_key )
  2186. {
  2187. QCursor::setPos( mapToGlobal( QPoint( me->x(),
  2188. keyAreaBottom() ) ) );
  2189. m_topBottomScroll->setValue(
  2190. m_topBottomScroll->value() + 1 );
  2191. key_num = s_key;
  2192. }
  2193. else if( key_num >= s_key + visible_keys )
  2194. {
  2195. QCursor::setPos( mapToGlobal( QPoint( me->x(),
  2196. PR_TOP_MARGIN ) ) );
  2197. m_topBottomScroll->setValue(
  2198. m_topBottomScroll->value() - 1 );
  2199. key_num = s_key + visible_keys;
  2200. }
  2201. m_selectedKeys = key_num - m_selectStartKey;
  2202. if( key_num <= m_selectStartKey )
  2203. {
  2204. --m_selectedKeys;
  2205. }
  2206. }
  2207. setCursor( Qt::ArrowCursor );
  2208. }
  2209. m_lastMouseX = me->x();
  2210. m_lastMouseY = me->y();
  2211. update();
  2212. }
  2213. void PianoRoll::updateRazorPos(QMouseEvent* me)
  2214. {
  2215. // Calculate the TimePos from the mouse
  2216. int mouseViewportPos = me->x() - m_whiteKeyWidth;
  2217. int mouseTickPos = mouseViewportPos / (m_ppb / TimePos::ticksPerBar()) + m_currentPosition;
  2218. // If ctrl is not pressed, quantize the position
  2219. if (!(me->modifiers() & Qt::ControlModifier))
  2220. {
  2221. mouseTickPos = floor(mouseTickPos / quantization()) * quantization();
  2222. }
  2223. m_razorTickPos = mouseTickPos;
  2224. }
  2225. void PianoRoll::dragNotes( int x, int y, bool alt, bool shift, bool ctrl )
  2226. {
  2227. // dragging one or more notes around
  2228. // convert pixels to ticks and keys
  2229. int off_x = x - m_moveStartX;
  2230. int off_ticks = off_x * TimePos::ticksPerBar() / m_ppb;
  2231. int off_key = getKey( y ) - getKey( m_moveStartY );
  2232. // handle scroll changes while dragging
  2233. off_ticks -= m_mouseDownTick - m_currentPosition;
  2234. off_key -= m_mouseDownKey - m_startKey;
  2235. // if they're not holding alt, quantize the offset
  2236. if( ! alt )
  2237. {
  2238. off_ticks = floor( off_ticks / quantization() )
  2239. * quantization();
  2240. }
  2241. // make sure notes won't go outside boundary conditions
  2242. if( m_action == ActionMoveNote && ! ( shift && ! m_startedWithShift ) )
  2243. {
  2244. if( m_moveBoundaryLeft + off_ticks < 0 )
  2245. {
  2246. off_ticks -= (off_ticks + m_moveBoundaryLeft);
  2247. }
  2248. if( m_moveBoundaryTop + off_key > NumKeys )
  2249. {
  2250. off_key -= NumKeys - (m_moveBoundaryTop + off_key);
  2251. }
  2252. if( m_moveBoundaryBottom + off_key < 0 )
  2253. {
  2254. off_key -= (m_moveBoundaryBottom + off_key);
  2255. }
  2256. }
  2257. // get note-vector of current pattern
  2258. const NoteVector & notes = m_pattern->notes();
  2259. if (m_action == ActionMoveNote)
  2260. {
  2261. for (Note *note : getSelectedNotes())
  2262. {
  2263. if (shift && ! m_startedWithShift)
  2264. {
  2265. // quick resize, toggled by holding shift after starting a note move, but not before
  2266. int ticks_new = note->oldLength().getTicks() + off_ticks;
  2267. if( ticks_new <= 0 )
  2268. {
  2269. ticks_new = 1;
  2270. }
  2271. note->setLength( TimePos( ticks_new ) );
  2272. m_lenOfNewNotes = note->length();
  2273. }
  2274. else
  2275. {
  2276. // moving note
  2277. int pos_ticks = note->oldPos().getTicks() + off_ticks;
  2278. int key_num = note->oldKey() + off_key;
  2279. // ticks can't be negative
  2280. pos_ticks = qMax(0, pos_ticks);
  2281. // upper/lower bound checks on key_num
  2282. key_num = qMax(0, key_num);
  2283. key_num = qMin(key_num, NumKeys);
  2284. note->setPos( TimePos( pos_ticks ) );
  2285. note->setKey( key_num );
  2286. }
  2287. }
  2288. }
  2289. else if (m_action == ActionResizeNote)
  2290. {
  2291. // When resizing notes:
  2292. // If shift is not pressed, resize the selected notes but do not rearrange them
  2293. // If shift is pressed we resize and rearrange only the selected notes
  2294. // If shift + ctrl then we also rearrange all posterior notes (sticky)
  2295. // If shift is pressed but only one note is selected, apply sticky
  2296. auto selectedNotes = getSelectedNotes();
  2297. if (shift)
  2298. {
  2299. // Algorithm:
  2300. // Relative to the starting point of the left-most selected note,
  2301. // all selected note start-points and *endpoints* (not length) should be scaled by a calculated factor.
  2302. // This factor is such that the endpoint of the note whose handle is being dragged should lie under the cursor.
  2303. // first, determine the start-point of the left-most selected note:
  2304. int stretchStartTick = -1;
  2305. for (const Note *note : selectedNotes)
  2306. {
  2307. if (stretchStartTick < 0 || note->oldPos().getTicks() < stretchStartTick)
  2308. {
  2309. stretchStartTick = note->oldPos().getTicks();
  2310. }
  2311. }
  2312. // determine the ending tick of the right-most selected note
  2313. const Note *posteriorNote = nullptr;
  2314. for (const Note *note : selectedNotes)
  2315. {
  2316. if (posteriorNote == nullptr ||
  2317. note->oldPos().getTicks() + note->oldLength().getTicks() >
  2318. posteriorNote->oldPos().getTicks() + posteriorNote->oldLength().getTicks())
  2319. {
  2320. posteriorNote = note;
  2321. }
  2322. }
  2323. int posteriorEndTick = posteriorNote->pos().getTicks() + posteriorNote->length().getTicks();
  2324. // end-point of the note whose handle is being dragged:
  2325. int stretchEndTick = m_currentNote->oldPos().getTicks() + m_currentNote->oldLength().getTicks();
  2326. // Calculate factor by which to scale the start-point and end-point of all selected notes
  2327. float scaleFactor = (float)(stretchEndTick - stretchStartTick + off_ticks) / qMax(1, stretchEndTick - stretchStartTick);
  2328. scaleFactor = qMax(0.0f, scaleFactor);
  2329. // process all selected notes & determine how much the endpoint of the right-most note was shifted
  2330. int posteriorDeltaThisFrame = 0;
  2331. for (Note *note : selectedNotes)
  2332. {
  2333. // scale relative start and end positions by scaleFactor
  2334. int newStart = stretchStartTick + scaleFactor *
  2335. (note->oldPos().getTicks() - stretchStartTick);
  2336. int newEnd = stretchStartTick + scaleFactor *
  2337. (note->oldPos().getTicks()+note->oldLength().getTicks() - stretchStartTick);
  2338. // if not holding alt, quantize the offsets
  2339. if (!alt)
  2340. {
  2341. // quantize start time
  2342. int oldStart = note->oldPos().getTicks();
  2343. int startDiff = newStart - oldStart;
  2344. startDiff = floor(startDiff / quantization()) * quantization();
  2345. newStart = oldStart + startDiff;
  2346. // quantize end time
  2347. int oldEnd = oldStart + note->oldLength().getTicks();
  2348. int endDiff = newEnd - oldEnd;
  2349. endDiff = floor(endDiff / quantization()) * quantization();
  2350. newEnd = oldEnd + endDiff;
  2351. }
  2352. int newLength = qMax(1, newEnd-newStart);
  2353. if (note == posteriorNote)
  2354. {
  2355. posteriorDeltaThisFrame = (newStart+newLength) -
  2356. (note->pos().getTicks() + note->length().getTicks());
  2357. }
  2358. note->setLength( TimePos(newLength) );
  2359. note->setPos( TimePos(newStart) );
  2360. m_lenOfNewNotes = note->length();
  2361. }
  2362. if (ctrl || selectionCount() == 1)
  2363. {
  2364. // if holding ctrl or only one note is selected, reposition posterior notes
  2365. for (Note *note : notes)
  2366. {
  2367. if (!note->selected() && note->pos().getTicks() >= posteriorEndTick)
  2368. {
  2369. int newStart = note->pos().getTicks() + posteriorDeltaThisFrame;
  2370. note->setPos( TimePos(newStart) );
  2371. }
  2372. }
  2373. }
  2374. }
  2375. else
  2376. {
  2377. // shift is not pressed; stretch length of selected notes but not their position
  2378. int minLength = alt ? 1 : m_minResizeLen.getTicks();
  2379. for (Note *note : selectedNotes)
  2380. {
  2381. int newLength = qMax(minLength, note->oldLength() + off_ticks);
  2382. note->setLength(TimePos(newLength));
  2383. m_lenOfNewNotes = note->length();
  2384. }
  2385. }
  2386. }
  2387. m_pattern->updateLength();
  2388. m_pattern->dataChanged();
  2389. Engine::getSong()->setModified();
  2390. }
  2391. void PianoRoll::paintEvent(QPaintEvent * pe )
  2392. {
  2393. bool drawNoteNames = ConfigManager::inst()->value( "ui", "printnotelabels").toInt();
  2394. QStyleOption opt;
  2395. opt.initFrom( this );
  2396. QPainter p( this );
  2397. style()->drawPrimitive( QStyle::PE_Widget, &opt, &p, this );
  2398. QBrush bgColor = p.background();
  2399. // fill with bg color
  2400. p.fillRect( 0, 0, width(), height(), bgColor );
  2401. // set font-size to 80% of key line height
  2402. QFont f = p.font();
  2403. f.setPixelSize(m_keyLineHeight * 0.8);
  2404. p.setFont(f); // font size doesn't change without this for some reason
  2405. QFontMetrics fontMetrics(p.font());
  2406. // G4 is one of the widest
  2407. QRect const boundingRect = fontMetrics.boundingRect(QString("G4"));
  2408. // Order of drawing
  2409. // - vertical quantization lines
  2410. // - piano roll + horizontal key lines
  2411. // - alternating bar colors
  2412. // - vertical beat lines
  2413. // - vertical bar lines
  2414. // - marked semitones
  2415. // - note editing
  2416. // - notes
  2417. // - selection frame
  2418. // - highlight hovered note
  2419. // - note edit area resize bar
  2420. // - cursor mode icon
  2421. if (hasValidPattern())
  2422. {
  2423. int pianoAreaHeight, partialKeyVisible, topKey, topNote;
  2424. pianoAreaHeight = keyAreaBottom() - keyAreaTop();
  2425. m_pianoKeysVisible = pianoAreaHeight / m_keyLineHeight;
  2426. partialKeyVisible = pianoAreaHeight % m_keyLineHeight;
  2427. // check if we're below the minimum key area size
  2428. if (m_pianoKeysVisible * m_keyLineHeight < KEY_AREA_MIN_HEIGHT)
  2429. {
  2430. m_pianoKeysVisible = KEY_AREA_MIN_HEIGHT / m_keyLineHeight;
  2431. partialKeyVisible = KEY_AREA_MIN_HEIGHT % m_keyLineHeight;
  2432. // if we have a partial key, just show it
  2433. if (partialKeyVisible > 0)
  2434. {
  2435. m_pianoKeysVisible += 1;
  2436. partialKeyVisible = 0;
  2437. }
  2438. // have to modifiy the notes edit area height instead
  2439. m_notesEditHeight = height() - (m_pianoKeysVisible * m_keyLineHeight)
  2440. - PR_TOP_MARGIN - PR_BOTTOM_MARGIN;
  2441. }
  2442. // check if we're trying to show more keys than available
  2443. else if (m_pianoKeysVisible >= NumKeys)
  2444. {
  2445. m_pianoKeysVisible = NumKeys;
  2446. // have to modify the notes edit area height instead
  2447. m_notesEditHeight = height() - (NumKeys * m_keyLineHeight) -
  2448. PR_TOP_MARGIN - PR_BOTTOM_MARGIN;
  2449. partialKeyVisible = 0;
  2450. }
  2451. topKey = qBound(0, m_startKey + m_pianoKeysVisible - 1, NumKeys - 1);
  2452. topNote = topKey % KeysPerOctave;
  2453. // if not resizing the note edit area, we can change m_notesEditHeight
  2454. if (m_action != ActionResizeNoteEditArea && partialKeyVisible != 0)
  2455. {
  2456. // calculate the height change adding and subtracting the partial key
  2457. int noteAreaPlus = (m_notesEditHeight + partialKeyVisible) - m_userSetNotesEditHeight;
  2458. int noteAreaMinus = m_userSetNotesEditHeight - (m_notesEditHeight - partialKeyVisible);
  2459. // if adding the partial key to height is more distant from the set height
  2460. // we want to subtract the partial key
  2461. if (noteAreaPlus > noteAreaMinus)
  2462. {
  2463. m_notesEditHeight -= partialKeyVisible;
  2464. // since we're adding a partial key, we add one to the number visible
  2465. m_pianoKeysVisible += 1;
  2466. }
  2467. // otherwise we add height
  2468. else { m_notesEditHeight += partialKeyVisible; }
  2469. }
  2470. updatePositionLineHeight();
  2471. int x, q = quantization(), tick;
  2472. // draw vertical quantization lines
  2473. // If we're over 100% zoom, we allow all quantization level grids
  2474. if (m_zoomingModel.value() <= 3)
  2475. {
  2476. // we're under 100% zoom
  2477. // allow quantization grid up to 1/24 for triplets
  2478. if (q % 3 != 0 && q < 8) { q = 8; }
  2479. // allow quantization grid up to 1/32 for normal notes
  2480. else if (q < 6) { q = 6; }
  2481. }
  2482. auto xCoordOfTick = [=](int tick) {
  2483. return m_whiteKeyWidth + (
  2484. (tick - m_currentPosition) * m_ppb / TimePos::ticksPerBar()
  2485. );
  2486. };
  2487. p.setPen(m_lineColor);
  2488. for (tick = m_currentPosition - m_currentPosition % q,
  2489. x = xCoordOfTick(tick);
  2490. x <= width();
  2491. tick += q, x = xCoordOfTick(tick))
  2492. {
  2493. p.drawLine(x, keyAreaTop(), x, noteEditBottom());
  2494. }
  2495. // draw horizontal grid lines and piano notes
  2496. p.setClipRect(0, keyAreaTop(), width(), keyAreaBottom() - keyAreaTop());
  2497. // the first grid line from the top Y position
  2498. int grid_line_y = keyAreaTop() + m_keyLineHeight - 1;
  2499. // lambda function for returning the height of a key
  2500. auto keyHeight = [&](
  2501. const int key
  2502. ) -> int
  2503. {
  2504. switch (prKeyOrder[key % KeysPerOctave])
  2505. {
  2506. case PR_WHITE_KEY_BIG:
  2507. return m_whiteKeyBigHeight;
  2508. case PR_WHITE_KEY_SMALL:
  2509. return m_whiteKeySmallHeight;
  2510. case PR_BLACK_KEY:
  2511. return m_blackKeyHeight;
  2512. }
  2513. return 0; // should never happen
  2514. };
  2515. // lambda function for returning the distance to the top of a key
  2516. auto gridCorrection = [&](
  2517. const int key
  2518. ) -> int
  2519. {
  2520. const int keyCode = key % KeysPerOctave;
  2521. switch (prKeyOrder[keyCode])
  2522. {
  2523. case PR_WHITE_KEY_BIG:
  2524. return m_whiteKeySmallHeight;
  2525. case PR_WHITE_KEY_SMALL:
  2526. // These two keys need to adjust up small height instead of only key line height
  2527. if (keyCode == Key_C || keyCode == Key_F)
  2528. {
  2529. return m_whiteKeySmallHeight;
  2530. }
  2531. case PR_BLACK_KEY:
  2532. return m_blackKeyHeight;
  2533. }
  2534. return 0; // should never happen
  2535. };
  2536. auto keyWidth = [&](
  2537. const int key
  2538. ) -> int
  2539. {
  2540. switch (prKeyOrder[key % KeysPerOctave])
  2541. {
  2542. case PR_WHITE_KEY_SMALL:
  2543. case PR_WHITE_KEY_BIG:
  2544. return m_whiteKeyWidth;
  2545. case PR_BLACK_KEY:
  2546. return m_blackKeyWidth;
  2547. }
  2548. return 0; // should never happen
  2549. };
  2550. // lambda function to draw a key
  2551. auto drawKey = [&](
  2552. const int key,
  2553. const int yb)
  2554. {
  2555. const bool pressed = m_pattern->instrumentTrack()->pianoModel()->isKeyPressed(key);
  2556. const int keyCode = key % KeysPerOctave;
  2557. const int yt = yb - gridCorrection(key);
  2558. const int kh = keyHeight(key);
  2559. const int kw = keyWidth(key);
  2560. // set key colors
  2561. p.setPen(QColor(0, 0, 0));
  2562. switch (prKeyOrder[keyCode])
  2563. {
  2564. case PR_WHITE_KEY_SMALL:
  2565. case PR_WHITE_KEY_BIG:
  2566. p.setBrush(pressed ? m_whiteKeyActiveBackground : m_whiteKeyInactiveBackground);
  2567. break;
  2568. case PR_BLACK_KEY:
  2569. p.setBrush(pressed ? m_blackKeyActiveBackground : m_blackKeyInactiveBackground);
  2570. }
  2571. // draw key
  2572. p.drawRect(PIANO_X, yt, kw, kh);
  2573. // draw note name
  2574. if (keyCode == Key_C || (drawNoteNames && Piano::isWhiteKey(key)))
  2575. {
  2576. // small font sizes have 1 pixel offset instead of 2
  2577. auto zoomOffset = m_zoomYLevels[m_zoomingYModel.value()] > 1.0f ? 2 : 1;
  2578. QString noteString = getNoteString(key);
  2579. QRect textRect(
  2580. m_whiteKeyWidth - boundingRect.width() - 2,
  2581. yb - m_keyLineHeight + zoomOffset,
  2582. boundingRect.width(),
  2583. boundingRect.height()
  2584. );
  2585. p.setPen(pressed ? m_whiteKeyActiveTextShadow : m_whiteKeyInactiveTextShadow);
  2586. p.drawText(textRect.adjusted(0, 1, 1, 0), Qt::AlignRight | Qt::AlignHCenter, noteString);
  2587. p.setPen(pressed ? m_whiteKeyActiveTextColor : m_whiteKeyInactiveTextColor);
  2588. // if (keyCode == Key_C) { p.setPen(textColor()); }
  2589. // else { p.setPen(textColorLight()); }
  2590. p.drawText(textRect, Qt::AlignRight | Qt::AlignHCenter, noteString);
  2591. }
  2592. };
  2593. // lambda for drawing the horizontal grid line
  2594. auto drawHorizontalLine = [&](
  2595. const int key,
  2596. const int y
  2597. )
  2598. {
  2599. if (key % KeysPerOctave == Key_C) { p.setPen(m_beatLineColor); }
  2600. else { p.setPen(m_lineColor); }
  2601. p.drawLine(m_whiteKeyWidth, y, width(), y);
  2602. };
  2603. // correct y offset of the top key
  2604. switch (prKeyOrder[topNote])
  2605. {
  2606. case PR_WHITE_KEY_SMALL:
  2607. case PR_WHITE_KEY_BIG:
  2608. break;
  2609. case PR_BLACK_KEY:
  2610. // draw extra white key
  2611. drawKey(topKey + 1, grid_line_y - m_keyLineHeight);
  2612. }
  2613. // loop through visible keys
  2614. const int lastKey = qMax(0, topKey - m_pianoKeysVisible);
  2615. for (int key = topKey; key > lastKey; --key)
  2616. {
  2617. bool whiteKey = Piano::isWhiteKey(key);
  2618. if (whiteKey)
  2619. {
  2620. drawKey(key, grid_line_y);
  2621. drawHorizontalLine(key, grid_line_y);
  2622. grid_line_y += m_keyLineHeight;
  2623. }
  2624. else
  2625. {
  2626. // draw next white key
  2627. drawKey(key - 1, grid_line_y + m_keyLineHeight);
  2628. drawHorizontalLine(key - 1, grid_line_y + m_keyLineHeight);
  2629. // draw black key over previous and next white key
  2630. drawKey(key, grid_line_y);
  2631. drawHorizontalLine(key, grid_line_y);
  2632. // drew two grid keys so skip ahead properly
  2633. grid_line_y += m_keyLineHeight + m_keyLineHeight;
  2634. // capture double key draw
  2635. --key;
  2636. }
  2637. }
  2638. // don't draw over keys
  2639. p.setClipRect(m_whiteKeyWidth, keyAreaTop(), width(), noteEditBottom() - keyAreaTop());
  2640. // draw alternating shading on bars
  2641. float timeSignature =
  2642. static_cast<float>(Engine::getSong()->getTimeSigModel().getNumerator()) /
  2643. static_cast<float>(Engine::getSong()->getTimeSigModel().getDenominator());
  2644. float zoomFactor = m_zoomLevels[m_zoomingModel.value()];
  2645. //the bars which disappears at the left side by scrolling
  2646. int leftBars = m_currentPosition * zoomFactor / TimePos::ticksPerBar();
  2647. //iterates the visible bars and draw the shading on uneven bars
  2648. for (int x = m_whiteKeyWidth, barCount = leftBars;
  2649. x < width() + m_currentPosition * zoomFactor / timeSignature;
  2650. x += m_ppb, ++barCount)
  2651. {
  2652. if ((barCount + leftBars) % 2 != 0)
  2653. {
  2654. p.fillRect(x - m_currentPosition * zoomFactor / timeSignature,
  2655. PR_TOP_MARGIN,
  2656. m_ppb,
  2657. height() - (PR_BOTTOM_MARGIN + PR_TOP_MARGIN),
  2658. m_backgroundShade);
  2659. }
  2660. }
  2661. // draw vertical beat lines
  2662. int ticksPerBeat = DefaultTicksPerBar /
  2663. Engine::getSong()->getTimeSigModel().getDenominator();
  2664. p.setPen(m_beatLineColor);
  2665. for(tick = m_currentPosition - m_currentPosition % ticksPerBeat,
  2666. x = xCoordOfTick( tick );
  2667. x <= width();
  2668. tick += ticksPerBeat, x = xCoordOfTick(tick))
  2669. {
  2670. p.drawLine(x, PR_TOP_MARGIN, x, noteEditBottom());
  2671. }
  2672. // draw vertical bar lines
  2673. p.setPen(m_barLineColor);
  2674. for(tick = m_currentPosition - m_currentPosition % TimePos::ticksPerBar(),
  2675. x = xCoordOfTick( tick );
  2676. x <= width();
  2677. tick += TimePos::ticksPerBar(), x = xCoordOfTick(tick))
  2678. {
  2679. p.drawLine(x, PR_TOP_MARGIN, x, noteEditBottom());
  2680. }
  2681. // draw marked semitones after the grid
  2682. for(x = 0; x < m_markedSemiTones.size(); ++x)
  2683. {
  2684. const int key_num = m_markedSemiTones.at(x);
  2685. const int y = keyAreaBottom() + 5 - m_keyLineHeight *
  2686. (key_num - m_startKey + 1);
  2687. if(y > keyAreaBottom()) { break; }
  2688. p.fillRect(m_whiteKeyWidth + 1,
  2689. y - m_keyLineHeight / 2,
  2690. width() - 10,
  2691. m_keyLineHeight + 1,
  2692. m_markedSemitoneColor);
  2693. }
  2694. }
  2695. // reset clip
  2696. p.setClipRect(0, 0, width(), height());
  2697. // erase the area below the piano, because there might be keys that
  2698. // should be only half-visible
  2699. p.fillRect( QRect( 0, keyAreaBottom(),
  2700. m_whiteKeyWidth, noteEditBottom() - keyAreaBottom()), bgColor);
  2701. // display note editing info
  2702. //QFont f = p.font();
  2703. f.setBold( false );
  2704. p.setFont( pointSize<10>( f ) );
  2705. p.setPen(m_noteModeColor);
  2706. p.drawText( QRect( 0, keyAreaBottom(),
  2707. m_whiteKeyWidth, noteEditBottom() - keyAreaBottom()),
  2708. Qt::AlignCenter | Qt::TextWordWrap,
  2709. m_nemStr.at( m_noteEditMode ) + ":" );
  2710. // set clipping area, because we are not allowed to paint over
  2711. // keyboard...
  2712. p.setClipRect(
  2713. m_whiteKeyWidth,
  2714. PR_TOP_MARGIN,
  2715. width() - m_whiteKeyWidth,
  2716. height() - PR_TOP_MARGIN - PR_BOTTOM_MARGIN);
  2717. // following code draws all notes in visible area
  2718. // and the note editing stuff (volume, panning, etc)
  2719. // setup selection-vars
  2720. int sel_pos_start = m_selectStartTick;
  2721. int sel_pos_end = m_selectStartTick+m_selectedTick;
  2722. if( sel_pos_start > sel_pos_end )
  2723. {
  2724. qSwap<int>( sel_pos_start, sel_pos_end );
  2725. }
  2726. int sel_key_start = m_selectStartKey - m_startKey + 1;
  2727. int sel_key_end = sel_key_start + m_selectedKeys;
  2728. if( sel_key_start > sel_key_end )
  2729. {
  2730. qSwap<int>( sel_key_start, sel_key_end );
  2731. }
  2732. int y_base = keyAreaBottom() - 1;
  2733. if( hasValidPattern() )
  2734. {
  2735. p.setClipRect(
  2736. m_whiteKeyWidth,
  2737. PR_TOP_MARGIN,
  2738. width() - m_whiteKeyWidth,
  2739. height() - PR_TOP_MARGIN);
  2740. const int topKey = qBound(0, m_startKey + m_pianoKeysVisible - 1, NumKeys - 1);
  2741. const int bottomKey = topKey - m_pianoKeysVisible;
  2742. QPolygonF editHandles;
  2743. // -- Begin ghost pattern
  2744. if( !m_ghostNotes.empty() )
  2745. {
  2746. for( const Note *note : m_ghostNotes )
  2747. {
  2748. int len_ticks = note->length();
  2749. if( len_ticks == 0 )
  2750. {
  2751. continue;
  2752. }
  2753. else if( len_ticks < 0 )
  2754. {
  2755. len_ticks = 4;
  2756. }
  2757. const int key = note->key() - m_startKey + 1;
  2758. int pos_ticks = note->pos();
  2759. int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
  2760. const int x = ( pos_ticks - m_currentPosition ) *
  2761. m_ppb / TimePos::ticksPerBar();
  2762. // skip this note if not in visible area at all
  2763. if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
  2764. {
  2765. continue;
  2766. }
  2767. // is the note in visible area?
  2768. if (note->key() > bottomKey && note->key() <= topKey)
  2769. {
  2770. // we've done and checked all, let's draw the note
  2771. drawNoteRect(
  2772. p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
  2773. note, m_ghostNoteColor, m_ghostNoteTextColor, m_selectedNoteColor,
  2774. m_ghostNoteOpacity, m_ghostNoteBorders, drawNoteNames);
  2775. }
  2776. }
  2777. }
  2778. // -- End ghost pattern
  2779. for( const Note *note : m_pattern->notes() )
  2780. {
  2781. int len_ticks = note->length();
  2782. if( len_ticks == 0 )
  2783. {
  2784. continue;
  2785. }
  2786. else if( len_ticks < 0 )
  2787. {
  2788. len_ticks = 4;
  2789. }
  2790. const int key = note->key() - m_startKey + 1;
  2791. int pos_ticks = note->pos();
  2792. int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
  2793. const int x = ( pos_ticks - m_currentPosition ) *
  2794. m_ppb / TimePos::ticksPerBar();
  2795. // skip this note if not in visible area at all
  2796. if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
  2797. {
  2798. continue;
  2799. }
  2800. // is the note in visible area?
  2801. if (note->key() > bottomKey && note->key() <= topKey)
  2802. {
  2803. // we've done and checked all, let's draw the note
  2804. drawNoteRect(
  2805. p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
  2806. note, m_noteColor, m_noteTextColor, m_selectedNoteColor,
  2807. m_noteOpacity, m_noteBorders, drawNoteNames);
  2808. }
  2809. // draw note editing stuff
  2810. int editHandleTop = 0;
  2811. if( m_noteEditMode == NoteEditVolume )
  2812. {
  2813. QColor color = m_barColor.lighter(30 + (note->getVolume() * 90 / MaxVolume));
  2814. if( note->selected() )
  2815. {
  2816. color = m_selectedNoteColor;
  2817. }
  2818. p.setPen( QPen( color, NOTE_EDIT_LINE_WIDTH ) );
  2819. editHandleTop = noteEditBottom() -
  2820. ( (float)( note->getVolume() - MinVolume ) ) /
  2821. ( (float)( MaxVolume - MinVolume ) ) *
  2822. ( (float)( noteEditBottom() - noteEditTop() ) );
  2823. p.drawLine( QLineF ( noteEditLeft() + x + 0.5, editHandleTop + 0.5,
  2824. noteEditLeft() + x + 0.5, noteEditBottom() + 0.5 ) );
  2825. }
  2826. else if( m_noteEditMode == NoteEditPanning )
  2827. {
  2828. QColor color = m_noteColor;
  2829. if( note->selected() )
  2830. {
  2831. color = m_selectedNoteColor;
  2832. }
  2833. p.setPen( QPen( color, NOTE_EDIT_LINE_WIDTH ) );
  2834. editHandleTop = noteEditBottom() -
  2835. ( (float)( note->getPanning() - PanningLeft ) ) /
  2836. ( (float)( (PanningRight - PanningLeft ) ) ) *
  2837. ( (float)( noteEditBottom() - noteEditTop() ) );
  2838. p.drawLine( QLine( noteEditLeft() + x, noteEditTop() +
  2839. ( (float)( noteEditBottom() - noteEditTop() ) ) / 2.0f,
  2840. noteEditLeft() + x , editHandleTop ) );
  2841. }
  2842. editHandles << QPoint ( x + noteEditLeft(),
  2843. editHandleTop );
  2844. if( note->hasDetuningInfo() )
  2845. {
  2846. drawDetuningInfo(p, note, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight);
  2847. p.setClipRect(
  2848. m_whiteKeyWidth,
  2849. PR_TOP_MARGIN,
  2850. width() - m_whiteKeyWidth,
  2851. height() - PR_TOP_MARGIN);
  2852. }
  2853. }
  2854. // -- Razor tool (draw cut line)
  2855. if (m_action == ActionRazor)
  2856. {
  2857. auto xCoordOfTick = [this](int tick) {
  2858. return m_whiteKeyWidth + (
  2859. (tick - m_currentPosition) * m_ppb / TimePos::ticksPerBar());
  2860. };
  2861. Note* n = noteUnderMouse();
  2862. if (n)
  2863. {
  2864. const int key = n->key() - m_startKey + 1;
  2865. int y = y_base - key * m_keyLineHeight;
  2866. int x = xCoordOfTick(m_razorTickPos);
  2867. if (x > xCoordOfTick(n->pos()) &&
  2868. x < xCoordOfTick(n->pos() + n->length()))
  2869. {
  2870. p.setPen(QPen(m_razorCutLineColor, 1));
  2871. p.drawLine(x, y, x, y + m_keyLineHeight);
  2872. setCursor(Qt::BlankCursor);
  2873. }
  2874. else
  2875. {
  2876. setCursor(Qt::ArrowCursor);
  2877. }
  2878. }
  2879. else
  2880. {
  2881. setCursor(Qt::ArrowCursor);
  2882. }
  2883. }
  2884. // -- End razor tool
  2885. //draw current step recording notes
  2886. for( const Note *note : m_stepRecorder.getCurStepNotes() )
  2887. {
  2888. int len_ticks = note->length();
  2889. if( len_ticks == 0 )
  2890. {
  2891. continue;
  2892. }
  2893. const int key = note->key() - m_startKey + 1;
  2894. int pos_ticks = note->pos();
  2895. int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
  2896. const int x = ( pos_ticks - m_currentPosition ) *
  2897. m_ppb / TimePos::ticksPerBar();
  2898. // skip this note if not in visible area at all
  2899. if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
  2900. {
  2901. continue;
  2902. }
  2903. // is the note in visible area?
  2904. if (note->key() > bottomKey && note->key() <= topKey)
  2905. {
  2906. // we've done and checked all, let's draw the note
  2907. drawNoteRect(
  2908. p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
  2909. note, m_stepRecorder.curStepNoteColor(), m_noteTextColor, m_selectedNoteColor,
  2910. m_noteOpacity, m_noteBorders, drawNoteNames);
  2911. }
  2912. }
  2913. p.setPen(QPen(m_noteColor, NOTE_EDIT_LINE_WIDTH + 2));
  2914. p.drawPoints( editHandles );
  2915. }
  2916. else
  2917. {
  2918. QFont f = p.font();
  2919. f.setBold( true );
  2920. p.setFont( pointSize<14>( f ) );
  2921. p.setPen( QApplication::palette().color( QPalette::Active,
  2922. QPalette::BrightText ) );
  2923. p.drawText(m_whiteKeyWidth + 20, PR_TOP_MARGIN + 40,
  2924. tr( "Please open a pattern by double-clicking "
  2925. "on it!" ) );
  2926. }
  2927. p.setClipRect(
  2928. m_whiteKeyWidth,
  2929. PR_TOP_MARGIN,
  2930. width() - m_whiteKeyWidth,
  2931. height() - PR_TOP_MARGIN - m_notesEditHeight - PR_BOTTOM_MARGIN);
  2932. // now draw selection-frame
  2933. int x = ( ( sel_pos_start - m_currentPosition ) * m_ppb ) /
  2934. TimePos::ticksPerBar();
  2935. int w = ( ( ( sel_pos_end - m_currentPosition ) * m_ppb ) /
  2936. TimePos::ticksPerBar() ) - x;
  2937. int y = (int) y_base - sel_key_start * m_keyLineHeight;
  2938. int h = (int) y_base - sel_key_end * m_keyLineHeight - y;
  2939. p.setPen(m_selectedNoteColor);
  2940. p.setBrush( Qt::NoBrush );
  2941. p.drawRect(x + m_whiteKeyWidth, y, w, h);
  2942. // TODO: Get this out of paint event
  2943. int l = ( hasValidPattern() )? (int) m_pattern->length() : 0;
  2944. // reset scroll-range
  2945. if( m_leftRightScroll->maximum() != l )
  2946. {
  2947. m_leftRightScroll->setRange( 0, l );
  2948. m_leftRightScroll->setPageStep( l );
  2949. }
  2950. // set line colors
  2951. QColor editAreaCol = QColor(m_lineColor);
  2952. QColor currentKeyCol = QColor(m_beatLineColor);
  2953. editAreaCol.setAlpha( 64 );
  2954. currentKeyCol.setAlpha( 64 );
  2955. // horizontal line for the key under the cursor
  2956. if(hasValidPattern() && gui->pianoRoll()->hasFocus())
  2957. {
  2958. int key_num = getKey( mapFromGlobal( QCursor::pos() ).y() );
  2959. p.fillRect( 10, keyAreaBottom() + 3 - m_keyLineHeight *
  2960. ( key_num - m_startKey + 1 ), width() - 10, m_keyLineHeight - 7, currentKeyCol );
  2961. }
  2962. // bar to resize note edit area
  2963. p.setClipRect( 0, 0, width(), height() );
  2964. p.fillRect( QRect( 0, keyAreaBottom(),
  2965. width()-PR_RIGHT_MARGIN, NOTE_EDIT_RESIZE_BAR ), editAreaCol );
  2966. if (gui->pianoRoll()->hasFocus())
  2967. {
  2968. const QPixmap * cursor = NULL;
  2969. // draw current edit-mode-icon below the cursor
  2970. switch( m_editMode )
  2971. {
  2972. case ModeDraw:
  2973. if( m_mouseDownRight )
  2974. {
  2975. cursor = s_toolErase;
  2976. }
  2977. else if( m_action == ActionMoveNote )
  2978. {
  2979. cursor = s_toolMove;
  2980. }
  2981. else
  2982. {
  2983. cursor = s_toolDraw;
  2984. }
  2985. break;
  2986. case ModeErase: cursor = s_toolErase; break;
  2987. case ModeSelect: cursor = s_toolSelect; break;
  2988. case ModeEditDetuning: cursor = s_toolOpen; break;
  2989. case ModeEditRazor: cursor = s_toolRazor; break;
  2990. }
  2991. QPoint mousePosition = mapFromGlobal( QCursor::pos() );
  2992. if( cursor != NULL && mousePosition.y() > keyAreaTop() && mousePosition.x() > noteEditLeft())
  2993. {
  2994. p.drawPixmap( mousePosition + QPoint( 8, 8 ), *cursor );
  2995. }
  2996. }
  2997. }
  2998. void PianoRoll::updateScrollbars()
  2999. {
  3000. m_leftRightScroll->setGeometry(
  3001. m_whiteKeyWidth,
  3002. height() - SCROLLBAR_SIZE,
  3003. width() - m_whiteKeyWidth,
  3004. SCROLLBAR_SIZE
  3005. );
  3006. m_topBottomScroll->setGeometry(
  3007. width() - SCROLLBAR_SIZE,
  3008. PR_TOP_MARGIN,
  3009. SCROLLBAR_SIZE,
  3010. height() - PR_TOP_MARGIN - SCROLLBAR_SIZE
  3011. );
  3012. int pianoAreaHeight = keyAreaBottom() - PR_TOP_MARGIN;
  3013. int numKeysVisible = pianoAreaHeight / m_keyLineHeight;
  3014. m_totalKeysToScroll = qMax(0, NumKeys - numKeysVisible);
  3015. m_topBottomScroll->setRange(0, m_totalKeysToScroll);
  3016. if (m_startKey > m_totalKeysToScroll)
  3017. {
  3018. m_startKey = qMax(0, m_totalKeysToScroll);
  3019. }
  3020. m_topBottomScroll->setValue(m_totalKeysToScroll - m_startKey);
  3021. }
  3022. // responsible for moving/resizing scrollbars after window-resizing
  3023. void PianoRoll::resizeEvent(QResizeEvent * re)
  3024. {
  3025. updatePositionLineHeight();
  3026. updateScrollbars();
  3027. Engine::getSong()->getPlayPos(Song::Mode_PlayPattern)
  3028. .m_timeLine->setFixedWidth(width());
  3029. update();
  3030. }
  3031. void PianoRoll::wheelEvent(QWheelEvent * we )
  3032. {
  3033. we->accept();
  3034. // handle wheel events for note edit area - for editing note vol/pan with mousewheel
  3035. if(position(we).x() > noteEditLeft() && position(we).x() < noteEditRight()
  3036. && position(we).y() > noteEditTop() && position(we).y() < noteEditBottom())
  3037. {
  3038. if (!hasValidPattern()) {return;}
  3039. // get values for going through notes
  3040. int pixel_range = 8;
  3041. int x = position(we).x() - m_whiteKeyWidth;
  3042. int ticks_start = ( x - pixel_range / 2 ) *
  3043. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  3044. int ticks_end = ( x + pixel_range / 2 ) *
  3045. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  3046. // When alt is pressed we only edit the note under the cursor
  3047. bool altPressed = we->modifiers() & Qt::AltModifier;
  3048. // go through notes to figure out which one we want to change
  3049. NoteVector nv;
  3050. for ( Note * i : m_pattern->notes() )
  3051. {
  3052. if( i->withinRange( ticks_start, ticks_end ) || ( i->selected() && !altPressed ) )
  3053. {
  3054. nv += i;
  3055. }
  3056. }
  3057. if( nv.size() > 0 )
  3058. {
  3059. const int step = we->angleDelta().y() > 0 ? 1 : -1;
  3060. if( m_noteEditMode == NoteEditVolume )
  3061. {
  3062. for ( Note * n : nv )
  3063. {
  3064. volume_t vol = qBound<int>( MinVolume, n->getVolume() + step, MaxVolume );
  3065. n->setVolume( vol );
  3066. }
  3067. bool allVolumesEqual = std::all_of( nv.begin(), nv.end(),
  3068. [nv](const Note *note)
  3069. {
  3070. return note->getVolume() == nv[0]->getVolume();
  3071. });
  3072. if ( allVolumesEqual )
  3073. {
  3074. // show the volume hover-text only if all notes have the
  3075. // same volume
  3076. showVolTextFloat(nv[0]->getVolume(), position(we), 1000);
  3077. }
  3078. }
  3079. else if( m_noteEditMode == NoteEditPanning )
  3080. {
  3081. for ( Note * n : nv )
  3082. {
  3083. panning_t pan = qBound<int>( PanningLeft, n->getPanning() + step, PanningRight );
  3084. n->setPanning( pan );
  3085. }
  3086. bool allPansEqual = std::all_of( nv.begin(), nv.end(),
  3087. [nv](const Note *note)
  3088. {
  3089. return note->getPanning() == nv[0]->getPanning();
  3090. });
  3091. if ( allPansEqual )
  3092. {
  3093. // show the pan hover-text only if all notes have the same
  3094. // panning
  3095. showPanTextFloat( nv[0]->getPanning(), position( we ), 1000 );
  3096. }
  3097. }
  3098. update();
  3099. }
  3100. }
  3101. // not in note edit area, so handle scrolling/zooming and quantization change
  3102. else
  3103. if( we->modifiers() & Qt::ControlModifier && we->modifiers() & Qt::AltModifier )
  3104. {
  3105. int q = m_quantizeModel.value();
  3106. if((we->angleDelta().x() + we->angleDelta().y()) > 0) // alt + scroll becomes horizontal scroll on KDE
  3107. {
  3108. q--;
  3109. }
  3110. else if((we->angleDelta().x() + we->angleDelta().y()) < 0) // alt + scroll becomes horizontal scroll on KDE
  3111. {
  3112. q++;
  3113. }
  3114. q = qBound( 0, q, m_quantizeModel.size() - 1 );
  3115. m_quantizeModel.setValue( q );
  3116. }
  3117. else if( we->modifiers() & Qt::ControlModifier && we->modifiers() & Qt::ShiftModifier )
  3118. {
  3119. int l = m_noteLenModel.value();
  3120. if(we->angleDelta().y() > 0)
  3121. {
  3122. l--;
  3123. }
  3124. else if(we->angleDelta().y() < 0)
  3125. {
  3126. l++;
  3127. }
  3128. l = qBound( 0, l, m_noteLenModel.size() - 1 );
  3129. m_noteLenModel.setValue( l );
  3130. }
  3131. else if( we->modifiers() & Qt::ControlModifier )
  3132. {
  3133. int z = m_zoomingModel.value();
  3134. if(we->angleDelta().y() > 0)
  3135. {
  3136. z++;
  3137. }
  3138. else if(we->angleDelta().y() < 0)
  3139. {
  3140. z--;
  3141. }
  3142. z = qBound( 0, z, m_zoomingModel.size() - 1 );
  3143. int x = (position(we).x() - m_whiteKeyWidth) * TimePos::ticksPerBar();
  3144. // ticks based on the mouse x-position where the scroll wheel was used
  3145. int ticks = x / m_ppb;
  3146. // what would be the ticks in the new zoom level on the very same mouse x
  3147. int newTicks = x / (DEFAULT_PR_PPB * m_zoomLevels[z]);
  3148. // scroll so the tick "selected" by the mouse x doesn't move on the screen
  3149. m_leftRightScroll->setValue(m_leftRightScroll->value() + ticks - newTicks);
  3150. // update combobox with zooming-factor
  3151. m_zoomingModel.setValue( z );
  3152. }
  3153. // FIXME: Reconsider if determining orientation is necessary in Qt6.
  3154. else if(abs(we->angleDelta().x()) > abs(we->angleDelta().y())) // scrolling is horizontal
  3155. {
  3156. m_leftRightScroll->setValue(m_leftRightScroll->value() -
  3157. we->angleDelta().x() * 2 / 15);
  3158. }
  3159. else if(we->modifiers() & Qt::ShiftModifier)
  3160. {
  3161. m_leftRightScroll->setValue(m_leftRightScroll->value() -
  3162. we->angleDelta().y() * 2 / 15);
  3163. }
  3164. else
  3165. {
  3166. m_topBottomScroll->setValue(m_topBottomScroll->value() -
  3167. we->angleDelta().y() / 30);
  3168. }
  3169. }
  3170. void PianoRoll::focusOutEvent( QFocusEvent * )
  3171. {
  3172. if( hasValidPattern() )
  3173. {
  3174. for( int i = 0; i < NumKeys; ++i )
  3175. {
  3176. m_pattern->instrumentTrack()->pianoModel()->midiEventProcessor()->processInEvent( MidiEvent( MidiNoteOff, -1, i, 0 ) );
  3177. m_pattern->instrumentTrack()->pianoModel()->setKeyState( i, false );
  3178. }
  3179. }
  3180. if (m_editMode == ModeEditRazor) {
  3181. m_editMode = m_razorMode;
  3182. m_action = ActionNone;
  3183. } else {
  3184. m_editMode = m_ctrlMode;
  3185. }
  3186. update();
  3187. }
  3188. void PianoRoll::focusInEvent( QFocusEvent * ev )
  3189. {
  3190. if ( hasValidPattern() )
  3191. {
  3192. // Assign midi device
  3193. m_pattern->instrumentTrack()->autoAssignMidiDevice(true);
  3194. }
  3195. QWidget::focusInEvent(ev);
  3196. }
  3197. int PianoRoll::getKey(int y) const
  3198. {
  3199. // handle case that very top pixel maps to next key above
  3200. if (y - keyAreaTop() <= 1) { y = keyAreaTop() + 2; }
  3201. int key_num = qBound(
  3202. 0,
  3203. // add + 1 to stay within the grid lines
  3204. ((keyAreaBottom() - y + 1) / m_keyLineHeight) + m_startKey,
  3205. NumKeys - 1
  3206. );
  3207. return key_num;
  3208. }
  3209. QList<int> PianoRoll::getAllOctavesForKey( int keyToMirror ) const
  3210. {
  3211. QList<int> keys;
  3212. for (int i=keyToMirror % KeysPerOctave; i < NumKeys; i += KeysPerOctave)
  3213. {
  3214. keys.append(i);
  3215. }
  3216. return keys;
  3217. }
  3218. Song::PlayModes PianoRoll::desiredPlayModeForAccompany() const
  3219. {
  3220. if( m_pattern->getTrack()->trackContainer() ==
  3221. Engine::getBBTrackContainer() )
  3222. {
  3223. return Song::Mode_PlayBB;
  3224. }
  3225. return Song::Mode_PlaySong;
  3226. }
  3227. void PianoRoll::play()
  3228. {
  3229. if( ! hasValidPattern() )
  3230. {
  3231. return;
  3232. }
  3233. if( Engine::getSong()->playMode() != Song::Mode_PlayPattern )
  3234. {
  3235. Engine::getSong()->playPattern( m_pattern );
  3236. }
  3237. else
  3238. {
  3239. Engine::getSong()->togglePause();
  3240. }
  3241. }
  3242. void PianoRoll::record()
  3243. {
  3244. if( Engine::getSong()->isPlaying() )
  3245. {
  3246. stop();
  3247. }
  3248. if( m_recording || ! hasValidPattern() )
  3249. {
  3250. return;
  3251. }
  3252. m_pattern->addJournalCheckPoint();
  3253. m_recording = true;
  3254. Engine::getSong()->playPattern( m_pattern, false );
  3255. }
  3256. void PianoRoll::recordAccompany()
  3257. {
  3258. if( Engine::getSong()->isPlaying() )
  3259. {
  3260. stop();
  3261. }
  3262. if( m_recording || ! hasValidPattern() )
  3263. {
  3264. return;
  3265. }
  3266. m_pattern->addJournalCheckPoint();
  3267. m_recording = true;
  3268. if( m_pattern->getTrack()->trackContainer() == Engine::getSong() )
  3269. {
  3270. Engine::getSong()->playSong();
  3271. }
  3272. else
  3273. {
  3274. Engine::getSong()->playBB();
  3275. }
  3276. }
  3277. bool PianoRoll::toggleStepRecording()
  3278. {
  3279. if(m_stepRecorder.isRecording())
  3280. {
  3281. m_stepRecorder.stop();
  3282. }
  3283. else
  3284. {
  3285. if(hasValidPattern())
  3286. {
  3287. if(Engine::getSong()->isPlaying())
  3288. {
  3289. m_stepRecorder.start(0, newNoteLen());
  3290. }
  3291. else
  3292. {
  3293. m_stepRecorder.start(
  3294. Engine::getSong()->getPlayPos(
  3295. Song::Mode_PlayPattern), newNoteLen());
  3296. }
  3297. }
  3298. }
  3299. return m_stepRecorder.isRecording();;
  3300. }
  3301. void PianoRoll::stop()
  3302. {
  3303. Engine::getSong()->stop();
  3304. m_recording = false;
  3305. m_scrollBack = ( m_timeLine->autoScroll() == TimeLineWidget::AutoScrollEnabled );
  3306. }
  3307. void PianoRoll::startRecordNote(const Note & n )
  3308. {
  3309. if(hasValidPattern())
  3310. {
  3311. if( m_recording &&
  3312. Engine::getSong()->isPlaying() &&
  3313. (Engine::getSong()->playMode() == desiredPlayModeForAccompany() ||
  3314. Engine::getSong()->playMode() == Song::Mode_PlayPattern ))
  3315. {
  3316. TimePos sub;
  3317. if( Engine::getSong()->playMode() == Song::Mode_PlaySong )
  3318. {
  3319. sub = m_pattern->startPosition();
  3320. }
  3321. Note n1( 1, Engine::getSong()->getPlayPos(
  3322. Engine::getSong()->playMode() ) - sub,
  3323. n.key(), n.getVolume(), n.getPanning() );
  3324. if( n1.pos() >= 0 )
  3325. {
  3326. m_recordingNotes << n1;
  3327. }
  3328. }
  3329. else if (m_stepRecorder.isRecording())
  3330. {
  3331. m_stepRecorder.notePressed(n);
  3332. }
  3333. }
  3334. }
  3335. void PianoRoll::finishRecordNote(const Note & n )
  3336. {
  3337. if(hasValidPattern())
  3338. {
  3339. if( m_recording &&
  3340. Engine::getSong()->isPlaying() &&
  3341. ( Engine::getSong()->playMode() ==
  3342. desiredPlayModeForAccompany() ||
  3343. Engine::getSong()->playMode() ==
  3344. Song::Mode_PlayPattern ) )
  3345. {
  3346. for( QList<Note>::Iterator it = m_recordingNotes.begin();
  3347. it != m_recordingNotes.end(); ++it )
  3348. {
  3349. if( it->key() == n.key() )
  3350. {
  3351. Note n1( n.length(), it->pos(),
  3352. it->key(), it->getVolume(),
  3353. it->getPanning() );
  3354. n1.quantizeLength( quantization() );
  3355. m_pattern->addNote( n1 );
  3356. update();
  3357. m_recordingNotes.erase( it );
  3358. break;
  3359. }
  3360. }
  3361. }
  3362. else if (m_stepRecorder.isRecording())
  3363. {
  3364. m_stepRecorder.noteReleased(n);
  3365. }
  3366. }
  3367. }
  3368. void PianoRoll::horScrolled(int new_pos )
  3369. {
  3370. m_currentPosition = new_pos;
  3371. m_stepRecorderWidget.setCurrentPosition(m_currentPosition);
  3372. emit positionChanged( m_currentPosition );
  3373. update();
  3374. }
  3375. void PianoRoll::verScrolled( int new_pos )
  3376. {
  3377. // revert value
  3378. m_startKey = qMax(0, m_totalKeysToScroll - new_pos);
  3379. update();
  3380. }
  3381. void PianoRoll::setEditMode(int mode)
  3382. {
  3383. m_ctrlMode = m_editMode = (EditModes) mode;
  3384. }
  3385. void PianoRoll::selectAll()
  3386. {
  3387. if( ! hasValidPattern() )
  3388. {
  3389. return;
  3390. }
  3391. // if first_time = true, we HAVE to set the vars for select
  3392. bool first_time = true;
  3393. for( const Note *note : m_pattern->notes() )
  3394. {
  3395. int len_ticks = static_cast<int>( note->length() ) > 0 ?
  3396. static_cast<int>( note->length() ) : 1;
  3397. const int key = note->key();
  3398. int pos_ticks = note->pos();
  3399. if( key <= m_selectStartKey || first_time )
  3400. {
  3401. // if we move start-key down, we have to add
  3402. // the difference between old and new start-key
  3403. // to m_selectedKeys, otherwise the selection
  3404. // is just moved down...
  3405. m_selectedKeys += m_selectStartKey
  3406. - ( key - 1 );
  3407. m_selectStartKey = key - 1;
  3408. }
  3409. if( key >= m_selectedKeys + m_selectStartKey ||
  3410. first_time )
  3411. {
  3412. m_selectedKeys = key - m_selectStartKey;
  3413. }
  3414. if( pos_ticks < m_selectStartTick ||
  3415. first_time )
  3416. {
  3417. m_selectStartTick = pos_ticks;
  3418. }
  3419. if( pos_ticks + len_ticks >
  3420. m_selectStartTick + m_selectedTick ||
  3421. first_time )
  3422. {
  3423. m_selectedTick = pos_ticks +
  3424. len_ticks -
  3425. m_selectStartTick;
  3426. }
  3427. first_time = false;
  3428. }
  3429. }
  3430. // returns vector with pointers to all selected notes
  3431. NoteVector PianoRoll::getSelectedNotes() const
  3432. {
  3433. NoteVector selectedNotes;
  3434. if (hasValidPattern())
  3435. {
  3436. for( Note *note : m_pattern->notes() )
  3437. {
  3438. if( note->selected() )
  3439. {
  3440. selectedNotes.push_back( note );
  3441. }
  3442. }
  3443. }
  3444. return selectedNotes;
  3445. }
  3446. // selects all notess associated with m_lastKey
  3447. void PianoRoll::selectNotesOnKey()
  3448. {
  3449. if (hasValidPattern()) {
  3450. for (Note * note : m_pattern->notes()) {
  3451. if (note->key() == m_lastKey) {
  3452. note->setSelected(true);
  3453. }
  3454. }
  3455. }
  3456. }
  3457. void PianoRoll::enterValue( NoteVector* nv )
  3458. {
  3459. if( m_noteEditMode == NoteEditVolume )
  3460. {
  3461. bool ok;
  3462. int new_val;
  3463. new_val = QInputDialog::getInt( this, "Piano roll: note velocity",
  3464. tr( "Please enter a new value between %1 and %2:" ).
  3465. arg( MinVolume ).arg( MaxVolume ),
  3466. (*nv)[0]->getVolume(),
  3467. MinVolume, MaxVolume, 1, &ok );
  3468. if( ok )
  3469. {
  3470. for ( Note * n : *nv )
  3471. {
  3472. n->setVolume( new_val );
  3473. }
  3474. m_lastNoteVolume = new_val;
  3475. }
  3476. }
  3477. else if( m_noteEditMode == NoteEditPanning )
  3478. {
  3479. bool ok;
  3480. int new_val;
  3481. new_val = QInputDialog::getInt( this, "Piano roll: note panning",
  3482. tr( "Please enter a new value between %1 and %2:" ).
  3483. arg( PanningLeft ).arg( PanningRight ),
  3484. (*nv)[0]->getPanning(),
  3485. PanningLeft, PanningRight, 1, &ok );
  3486. if( ok )
  3487. {
  3488. for ( Note * n : *nv )
  3489. {
  3490. n->setPanning( new_val );
  3491. }
  3492. m_lastNotePanning = new_val;
  3493. }
  3494. }
  3495. }
  3496. void PianoRoll::updateYScroll()
  3497. {
  3498. m_topBottomScroll->setGeometry(width() - SCROLLBAR_SIZE, PR_TOP_MARGIN,
  3499. SCROLLBAR_SIZE,
  3500. height() - PR_TOP_MARGIN -
  3501. SCROLLBAR_SIZE);
  3502. int total_pixels = m_octaveHeight * NumOctaves - (height() -
  3503. PR_TOP_MARGIN - PR_BOTTOM_MARGIN -
  3504. m_notesEditHeight);
  3505. m_totalKeysToScroll = qMax(0, total_pixels * KeysPerOctave / m_octaveHeight);
  3506. m_topBottomScroll->setRange(0, m_totalKeysToScroll);
  3507. if(m_startKey > m_totalKeysToScroll)
  3508. {
  3509. m_startKey = qMax(0, m_totalKeysToScroll);
  3510. }
  3511. m_topBottomScroll->setValue(m_totalKeysToScroll - m_startKey);
  3512. }
  3513. void PianoRoll::copyToClipboard( const NoteVector & notes ) const
  3514. {
  3515. // For copyString() and MimeType enum class
  3516. using namespace Clipboard;
  3517. DataFile dataFile( DataFile::ClipboardData );
  3518. QDomElement note_list = dataFile.createElement( "note-list" );
  3519. dataFile.content().appendChild( note_list );
  3520. TimePos start_pos( notes.front()->pos().getBar(), 0 );
  3521. for( const Note *note : notes )
  3522. {
  3523. Note clip_note( *note );
  3524. clip_note.setPos( clip_note.pos( start_pos ) );
  3525. clip_note.saveState( dataFile, note_list );
  3526. }
  3527. copyString( dataFile.toString(), MimeType::Default );
  3528. }
  3529. void PianoRoll::copySelectedNotes()
  3530. {
  3531. NoteVector selected_notes = getSelectedNotes();
  3532. if( ! selected_notes.empty() )
  3533. {
  3534. copyToClipboard( selected_notes );
  3535. }
  3536. }
  3537. void PianoRoll::cutSelectedNotes()
  3538. {
  3539. if( ! hasValidPattern() )
  3540. {
  3541. return;
  3542. }
  3543. NoteVector selected_notes = getSelectedNotes();
  3544. if( ! selected_notes.empty() )
  3545. {
  3546. m_pattern->addJournalCheckPoint();
  3547. copyToClipboard( selected_notes );
  3548. Engine::getSong()->setModified();
  3549. for( Note *note : selected_notes )
  3550. {
  3551. // note (the memory of it) is also deleted by
  3552. // pattern::removeNote(...) so we don't have to do that
  3553. m_pattern->removeNote( note );
  3554. }
  3555. }
  3556. update();
  3557. gui->songEditor()->update();
  3558. }
  3559. void PianoRoll::pasteNotes()
  3560. {
  3561. // For getString() and MimeType enum class
  3562. using namespace Clipboard;
  3563. if( ! hasValidPattern() )
  3564. {
  3565. return;
  3566. }
  3567. QString value = getString( MimeType::Default );
  3568. if( ! value.isEmpty() )
  3569. {
  3570. DataFile dataFile( value.toUtf8() );
  3571. QDomNodeList list = dataFile.elementsByTagName( Note::classNodeName() );
  3572. // remove selection and select the newly pasted notes
  3573. clearSelectedNotes();
  3574. if( ! list.isEmpty() )
  3575. {
  3576. m_pattern->addJournalCheckPoint();
  3577. }
  3578. for( int i = 0; ! list.item( i ).isNull(); ++i )
  3579. {
  3580. // create the note
  3581. Note cur_note;
  3582. cur_note.restoreState( list.item( i ).toElement() );
  3583. cur_note.setPos( cur_note.pos() + Note::quantized( m_timeLine->pos(), quantization() ) );
  3584. // select it
  3585. cur_note.setSelected( true );
  3586. // add to pattern
  3587. m_pattern->addNote( cur_note, false );
  3588. }
  3589. // we only have to do the following lines if we pasted at
  3590. // least one note...
  3591. Engine::getSong()->setModified();
  3592. update();
  3593. gui->songEditor()->update();
  3594. }
  3595. }
  3596. //Return false if no notes are deleted
  3597. bool PianoRoll::deleteSelectedNotes()
  3598. {
  3599. if (!hasValidPattern()) { return false; }
  3600. auto selectedNotes = getSelectedNotes();
  3601. if (selectedNotes.empty()) { return false; }
  3602. m_pattern->addJournalCheckPoint();
  3603. for (Note* note: selectedNotes) { m_pattern->removeNote( note ); }
  3604. Engine::getSong()->setModified();
  3605. update();
  3606. gui->songEditor()->update();
  3607. return true;
  3608. }
  3609. void PianoRoll::autoScroll( const TimePos & t )
  3610. {
  3611. const int w = width() - m_whiteKeyWidth;
  3612. if( t > m_currentPosition + w * TimePos::ticksPerBar() / m_ppb )
  3613. {
  3614. m_leftRightScroll->setValue( t.getBar() * TimePos::ticksPerBar() );
  3615. }
  3616. else if( t < m_currentPosition )
  3617. {
  3618. TimePos t2 = qMax( t - w * TimePos::ticksPerBar() *
  3619. TimePos::ticksPerBar() / m_ppb, (tick_t) 0 );
  3620. m_leftRightScroll->setValue( t2.getBar() * TimePos::ticksPerBar() );
  3621. }
  3622. m_scrollBack = false;
  3623. }
  3624. void PianoRoll::updatePosition( const TimePos & t )
  3625. {
  3626. if( ( Engine::getSong()->isPlaying()
  3627. && Engine::getSong()->playMode() == Song::Mode_PlayPattern
  3628. && m_timeLine->autoScroll() == TimeLineWidget::AutoScrollEnabled
  3629. ) || m_scrollBack )
  3630. {
  3631. autoScroll( t );
  3632. }
  3633. const int pos = m_timeLine->pos() * m_ppb / TimePos::ticksPerBar();
  3634. if (pos >= m_currentPosition && pos <= m_currentPosition + width() - m_whiteKeyWidth)
  3635. {
  3636. m_positionLine->show();
  3637. m_positionLine->move(pos - (m_positionLine->width() - 1) - m_currentPosition + m_whiteKeyWidth, keyAreaTop());
  3638. }
  3639. else
  3640. {
  3641. m_positionLine->hide();
  3642. }
  3643. }
  3644. void PianoRoll::updatePositionLineHeight()
  3645. {
  3646. m_positionLine->setFixedHeight(keyAreaBottom() - keyAreaTop());
  3647. }
  3648. void PianoRoll::updatePositionAccompany( const TimePos & t )
  3649. {
  3650. Song * s = Engine::getSong();
  3651. if( m_recording && hasValidPattern() &&
  3652. s->playMode() != Song::Mode_PlayPattern )
  3653. {
  3654. TimePos pos = t;
  3655. if( s->playMode() != Song::Mode_PlayBB )
  3656. {
  3657. pos -= m_pattern->startPosition();
  3658. }
  3659. if( (int) pos > 0 )
  3660. {
  3661. s->getPlayPos( Song::Mode_PlayPattern ).setTicks( pos );
  3662. autoScroll( pos );
  3663. }
  3664. }
  3665. }
  3666. void PianoRoll::updatePositionStepRecording( const TimePos & t )
  3667. {
  3668. if( m_stepRecorder.isRecording() )
  3669. {
  3670. autoScroll( t );
  3671. }
  3672. }
  3673. void PianoRoll::zoomingChanged()
  3674. {
  3675. m_ppb = m_zoomLevels[m_zoomingModel.value()] * DEFAULT_PR_PPB;
  3676. assert( m_ppb > 0 );
  3677. m_timeLine->setPixelsPerBar( m_ppb );
  3678. m_stepRecorderWidget.setPixelsPerBar( m_ppb );
  3679. m_positionLine->zoomChange(m_zoomLevels[m_zoomingModel.value()]);
  3680. update();
  3681. }
  3682. void PianoRoll::zoomingYChanged()
  3683. {
  3684. m_keyLineHeight = m_zoomYLevels[m_zoomingYModel.value()] * DEFAULT_KEY_LINE_HEIGHT;
  3685. m_octaveHeight = m_keyLineHeight * KeysPerOctave;
  3686. m_whiteKeySmallHeight = qFloor(m_keyLineHeight * 1.5);
  3687. m_whiteKeyBigHeight = m_keyLineHeight * 2;
  3688. m_blackKeyHeight = m_keyLineHeight; //round(m_keyLineHeight * 1.3333);
  3689. updateYScroll();
  3690. update();
  3691. }
  3692. void PianoRoll::quantizeChanged()
  3693. {
  3694. update();
  3695. }
  3696. void PianoRoll::noteLengthChanged()
  3697. {
  3698. m_stepRecorder.setStepsLength(newNoteLen());
  3699. update();
  3700. }
  3701. void PianoRoll::keyChanged()
  3702. {
  3703. markSemiTone(stmaMarkCurrentScale, false);
  3704. }
  3705. int PianoRoll::quantization() const
  3706. {
  3707. if( m_quantizeModel.value() == 0 )
  3708. {
  3709. if( m_noteLenModel.value() > 0 )
  3710. {
  3711. return newNoteLen();
  3712. }
  3713. else
  3714. {
  3715. return DefaultTicksPerBar / 16;
  3716. }
  3717. }
  3718. return DefaultTicksPerBar / Quantizations[m_quantizeModel.value() - 1];
  3719. }
  3720. void PianoRoll::quantizeNotes()
  3721. {
  3722. if( ! hasValidPattern() )
  3723. {
  3724. return;
  3725. }
  3726. m_pattern->addJournalCheckPoint();
  3727. NoteVector notes = getSelectedNotes();
  3728. if( notes.empty() )
  3729. {
  3730. for( Note* n : m_pattern->notes() )
  3731. {
  3732. notes.push_back( n );
  3733. }
  3734. }
  3735. for( Note* n : notes )
  3736. {
  3737. if( n->length() == TimePos( 0 ) )
  3738. {
  3739. continue;
  3740. }
  3741. Note copy(*n);
  3742. m_pattern->removeNote( n );
  3743. copy.quantizePos( quantization() );
  3744. m_pattern->addNote( copy );
  3745. }
  3746. update();
  3747. gui->songEditor()->update();
  3748. Engine::getSong()->setModified();
  3749. }
  3750. void PianoRoll::updateSemiToneMarkerMenu()
  3751. {
  3752. const InstrumentFunctionNoteStacking::ChordTable& chord_table =
  3753. InstrumentFunctionNoteStacking::ChordTable::getInstance();
  3754. const InstrumentFunctionNoteStacking::Chord& scale =
  3755. chord_table.getScaleByName( m_scaleModel.currentText() );
  3756. const InstrumentFunctionNoteStacking::Chord& chord =
  3757. chord_table.getChordByName( m_chordModel.currentText() );
  3758. emit semiToneMarkerMenuScaleSetEnabled( ! scale.isEmpty() );
  3759. emit semiToneMarkerMenuChordSetEnabled( ! chord.isEmpty() );
  3760. }
  3761. TimePos PianoRoll::newNoteLen() const
  3762. {
  3763. if( m_noteLenModel.value() == 0 )
  3764. {
  3765. return m_lenOfNewNotes;
  3766. }
  3767. QString text = m_noteLenModel.currentText();
  3768. return DefaultTicksPerBar / text.right( text.length() - 2 ).toInt();
  3769. }
  3770. bool PianoRoll::mouseOverNote()
  3771. {
  3772. return hasValidPattern() && noteUnderMouse() != NULL;
  3773. }
  3774. Note * PianoRoll::noteUnderMouse()
  3775. {
  3776. QPoint pos = mapFromGlobal( QCursor::pos() );
  3777. if (pos.x() <= m_whiteKeyWidth
  3778. || pos.x() > width() - SCROLLBAR_SIZE
  3779. || pos.y() < PR_TOP_MARGIN
  3780. || pos.y() > keyAreaBottom() )
  3781. {
  3782. return NULL;
  3783. }
  3784. int key_num = getKey( pos.y() );
  3785. int pos_ticks = (pos.x() - m_whiteKeyWidth) *
  3786. TimePos::ticksPerBar() / m_ppb + m_currentPosition;
  3787. // loop through whole note-vector...
  3788. for( Note* const& note : m_pattern->notes() )
  3789. {
  3790. // and check whether the cursor is over an
  3791. // existing note
  3792. if( pos_ticks >= note->pos()
  3793. && pos_ticks <= note->endPos()
  3794. && note->key() == key_num
  3795. && note->length() > 0 )
  3796. {
  3797. return note;
  3798. }
  3799. }
  3800. return NULL;
  3801. }
  3802. PianoRollWindow::PianoRollWindow() :
  3803. Editor(true, true),
  3804. m_editor(new PianoRoll())
  3805. {
  3806. setCentralWidget( m_editor );
  3807. m_playAction->setToolTip(tr( "Play/pause current pattern (Space)" ) );
  3808. m_recordAction->setToolTip(tr( "Record notes from MIDI-device/channel-piano" ) );
  3809. m_recordAccompanyAction->setToolTip( tr( "Record notes from MIDI-device/channel-piano while playing song or BB track" ) );
  3810. m_toggleStepRecordingAction->setToolTip( tr( "Record notes from MIDI-device/channel-piano, one step at the time" ) );
  3811. m_stopAction->setToolTip( tr( "Stop playing of current pattern (Space)" ) );
  3812. DropToolBar *notesActionsToolBar = addDropToolBarToTop( tr( "Edit actions" ) );
  3813. // init edit-buttons at the top
  3814. ActionGroup* editModeGroup = new ActionGroup( this );
  3815. QAction* drawAction = editModeGroup->addAction( embed::getIconPixmap( "edit_draw" ), tr( "Draw mode (Shift+D)" ) );
  3816. QAction* eraseAction = editModeGroup->addAction( embed::getIconPixmap( "edit_erase" ), tr("Erase mode (Shift+E)" ) );
  3817. QAction* selectAction = editModeGroup->addAction( embed::getIconPixmap( "edit_select" ), tr( "Select mode (Shift+S)" ) );
  3818. QAction* pitchBendAction = editModeGroup->addAction( embed::getIconPixmap( "automation" ), tr("Pitch Bend mode (Shift+T)" ) );
  3819. drawAction->setChecked( true );
  3820. drawAction->setShortcut( Qt::SHIFT | Qt::Key_D );
  3821. eraseAction->setShortcut( Qt::SHIFT | Qt::Key_E );
  3822. selectAction->setShortcut( Qt::SHIFT | Qt::Key_S );
  3823. pitchBendAction->setShortcut( Qt::SHIFT | Qt::Key_T );
  3824. connect( editModeGroup, SIGNAL( triggered( int ) ), m_editor, SLOT( setEditMode( int ) ) );
  3825. QAction* quantizeAction = new QAction(embed::getIconPixmap( "quantize" ), tr( "Quantize" ), this );
  3826. connect( quantizeAction, SIGNAL( triggered() ), m_editor, SLOT( quantizeNotes() ) );
  3827. notesActionsToolBar->addAction( drawAction );
  3828. notesActionsToolBar->addAction( eraseAction );
  3829. notesActionsToolBar->addAction( selectAction );
  3830. notesActionsToolBar->addAction( pitchBendAction );
  3831. notesActionsToolBar->addSeparator();
  3832. notesActionsToolBar->addAction( quantizeAction );
  3833. // Copy + paste actions
  3834. DropToolBar *copyPasteActionsToolBar = addDropToolBarToTop( tr( "Copy paste controls" ) );
  3835. QAction* cutAction = new QAction(embed::getIconPixmap( "edit_cut" ),
  3836. tr( "Cut (%1+X)" ).arg(UI_CTRL_KEY), this );
  3837. QAction* copyAction = new QAction(embed::getIconPixmap( "edit_copy" ),
  3838. tr( "Copy (%1+C)" ).arg(UI_CTRL_KEY), this );
  3839. QAction* pasteAction = new QAction(embed::getIconPixmap( "edit_paste" ),
  3840. tr( "Paste (%1+V)" ).arg(UI_CTRL_KEY), this );
  3841. cutAction->setShortcut( Qt::CTRL | Qt::Key_X );
  3842. copyAction->setShortcut( Qt::CTRL | Qt::Key_C );
  3843. pasteAction->setShortcut( Qt::CTRL | Qt::Key_V );
  3844. connect( cutAction, SIGNAL( triggered() ), m_editor, SLOT( cutSelectedNotes() ) );
  3845. connect( copyAction, SIGNAL( triggered() ), m_editor, SLOT( copySelectedNotes() ) );
  3846. connect( pasteAction, SIGNAL( triggered() ), m_editor, SLOT( pasteNotes() ) );
  3847. copyPasteActionsToolBar->addAction( cutAction );
  3848. copyPasteActionsToolBar->addAction( copyAction );
  3849. copyPasteActionsToolBar->addAction( pasteAction );
  3850. DropToolBar *timeLineToolBar = addDropToolBarToTop( tr( "Timeline controls" ) );
  3851. m_editor->m_timeLine->addToolButtons( timeLineToolBar );
  3852. // -- Note modifier tools
  3853. // Toolbar
  3854. QToolButton * noteToolsButton = new QToolButton(m_toolBar);
  3855. noteToolsButton->setIcon(embed::getIconPixmap("tool"));
  3856. noteToolsButton->setPopupMode(QToolButton::InstantPopup);
  3857. // Glue
  3858. QAction * glueAction = new QAction(embed::getIconPixmap("glue"),
  3859. tr("Glue"), noteToolsButton);
  3860. connect(glueAction, SIGNAL(triggered()), m_editor, SLOT(glueNotes()));
  3861. glueAction->setShortcut( Qt::SHIFT | Qt::Key_G );
  3862. // Razor
  3863. QAction * razorAction = new QAction(embed::getIconPixmap("razor"),
  3864. tr("Razor"), noteToolsButton);
  3865. connect(razorAction, &QAction::triggered, m_editor, &PianoRoll::setRazorAction);
  3866. razorAction->setShortcut( Qt::SHIFT | Qt::Key_R );
  3867. noteToolsButton->addAction(glueAction);
  3868. noteToolsButton->addAction(razorAction);
  3869. notesActionsToolBar->addWidget(noteToolsButton);
  3870. addToolBarBreak();
  3871. DropToolBar *zoomAndNotesToolBar = addDropToolBarToTop( tr( "Zoom and note controls" ) );
  3872. QLabel * zoom_lbl = new QLabel( m_toolBar );
  3873. zoom_lbl->setPixmap( embed::getIconPixmap( "zoom_x" ) );
  3874. m_zoomingComboBox = new ComboBox( m_toolBar );
  3875. m_zoomingComboBox->setModel( &m_editor->m_zoomingModel );
  3876. m_zoomingComboBox->setFixedSize( 64, ComboBox::DEFAULT_HEIGHT );
  3877. m_zoomingComboBox->setToolTip( tr( "Horizontal zooming") );
  3878. QLabel * zoom_y_lbl = new QLabel(m_toolBar);
  3879. zoom_y_lbl->setPixmap(embed::getIconPixmap("zoom_y"));
  3880. m_zoomingYComboBox = new ComboBox(m_toolBar);
  3881. m_zoomingYComboBox->setModel(&m_editor->m_zoomingYModel);
  3882. m_zoomingYComboBox->setFixedSize(64, ComboBox::DEFAULT_HEIGHT);
  3883. m_zoomingYComboBox->setToolTip(tr("Vertical zooming"));
  3884. // setup quantize-stuff
  3885. QLabel * quantize_lbl = new QLabel( m_toolBar );
  3886. quantize_lbl->setPixmap( embed::getIconPixmap( "quantize" ) );
  3887. m_quantizeComboBox = new ComboBox( m_toolBar );
  3888. m_quantizeComboBox->setModel( &m_editor->m_quantizeModel );
  3889. m_quantizeComboBox->setFixedSize( 64, ComboBox::DEFAULT_HEIGHT );
  3890. m_quantizeComboBox->setToolTip( tr( "Quantization") );
  3891. // setup note-len-stuff
  3892. QLabel * note_len_lbl = new QLabel( m_toolBar );
  3893. note_len_lbl->setPixmap( embed::getIconPixmap( "note" ) );
  3894. m_noteLenComboBox = new ComboBox( m_toolBar );
  3895. m_noteLenComboBox->setModel( &m_editor->m_noteLenModel );
  3896. m_noteLenComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
  3897. m_noteLenComboBox->setToolTip( tr( "Note length") );
  3898. // setup key-stuff
  3899. m_keyComboBox = new ComboBox(m_toolBar);
  3900. m_keyComboBox->setModel(&m_editor->m_keyModel);
  3901. m_keyComboBox->setFixedSize(72, ComboBox::DEFAULT_HEIGHT);
  3902. m_keyComboBox->setToolTip(tr("Key"));
  3903. // setup scale-stuff
  3904. QLabel * scale_lbl = new QLabel( m_toolBar );
  3905. scale_lbl->setPixmap( embed::getIconPixmap( "scale" ) );
  3906. m_scaleComboBox = new ComboBox( m_toolBar );
  3907. m_scaleComboBox->setModel( &m_editor->m_scaleModel );
  3908. m_scaleComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
  3909. m_scaleComboBox->setToolTip( tr( "Scale") );
  3910. // setup chord-stuff
  3911. QLabel * chord_lbl = new QLabel( m_toolBar );
  3912. chord_lbl->setPixmap( embed::getIconPixmap( "chord" ) );
  3913. m_chordComboBox = new ComboBox( m_toolBar );
  3914. m_chordComboBox->setModel( &m_editor->m_chordModel );
  3915. m_chordComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
  3916. m_chordComboBox->setToolTip( tr( "Chord" ) );
  3917. // -- Clear ghost pattern button
  3918. m_clearGhostButton = new QPushButton( m_toolBar );
  3919. m_clearGhostButton->setIcon( embed::getIconPixmap( "clear_ghost_note" ) );
  3920. m_clearGhostButton->setToolTip( tr( "Clear ghost notes" ) );
  3921. m_clearGhostButton->setEnabled( false );
  3922. connect( m_clearGhostButton, SIGNAL( clicked() ), m_editor, SLOT( clearGhostPattern() ) );
  3923. connect( m_editor, SIGNAL( ghostPatternSet( bool ) ), this, SLOT( ghostPatternSet( bool ) ) );
  3924. // Wrap label icons and comboboxes in a single widget so when
  3925. // the window is resized smaller in width it hides both
  3926. QWidget * zoom_widget = new QWidget();
  3927. QHBoxLayout * zoom_hbox = new QHBoxLayout();
  3928. zoom_hbox->setContentsMargins(0, 0, 0, 0);
  3929. zoom_hbox->addWidget(zoom_lbl);
  3930. zoom_hbox->addWidget(m_zoomingComboBox);
  3931. zoom_widget->setLayout(zoom_hbox);
  3932. zoomAndNotesToolBar->addWidget(zoom_widget);
  3933. QWidget * zoomY_widget = new QWidget();
  3934. QHBoxLayout * zoomY_hbox = new QHBoxLayout();
  3935. zoomY_hbox->setContentsMargins(0, 0, 0, 0);
  3936. zoomY_hbox->addWidget(zoom_y_lbl);
  3937. zoomY_hbox->addWidget(m_zoomingYComboBox);
  3938. zoomY_widget->setLayout(zoomY_hbox);
  3939. zoomAndNotesToolBar->addWidget(zoomY_widget);
  3940. QWidget * quantize_widget = new QWidget();
  3941. QHBoxLayout * quantize_hbox = new QHBoxLayout();
  3942. quantize_hbox->setContentsMargins(0, 0, 0, 0);
  3943. quantize_hbox->addWidget(quantize_lbl);
  3944. quantize_hbox->addWidget(m_quantizeComboBox);
  3945. quantize_widget->setLayout(quantize_hbox);
  3946. zoomAndNotesToolBar->addSeparator();
  3947. zoomAndNotesToolBar->addWidget(quantize_widget);
  3948. QWidget * note_widget = new QWidget();
  3949. QHBoxLayout * note_hbox = new QHBoxLayout();
  3950. note_hbox->setContentsMargins(0, 0, 0, 0);
  3951. note_hbox->addWidget(note_len_lbl);
  3952. note_hbox->addWidget(m_noteLenComboBox);
  3953. note_widget->setLayout(note_hbox);
  3954. zoomAndNotesToolBar->addSeparator();
  3955. zoomAndNotesToolBar->addWidget(note_widget);
  3956. QWidget * scale_widget = new QWidget();
  3957. QHBoxLayout * scale_hbox = new QHBoxLayout();
  3958. scale_hbox->setContentsMargins(0, 0, 0, 0);
  3959. scale_hbox->addWidget(scale_lbl);
  3960. // Add the key selection between scale label and key
  3961. scale_hbox->addWidget(m_keyComboBox);
  3962. scale_hbox->addWidget(m_scaleComboBox);
  3963. scale_widget->setLayout(scale_hbox);
  3964. zoomAndNotesToolBar->addSeparator();
  3965. zoomAndNotesToolBar->addWidget(scale_widget);
  3966. QWidget * chord_widget = new QWidget();
  3967. QHBoxLayout * chord_hbox = new QHBoxLayout();
  3968. chord_hbox->setContentsMargins(0, 0, 0, 0);
  3969. chord_hbox->addWidget(chord_lbl);
  3970. chord_hbox->addWidget(m_chordComboBox);
  3971. chord_widget->setLayout(chord_hbox);
  3972. zoomAndNotesToolBar->addSeparator();
  3973. zoomAndNotesToolBar->addWidget(chord_widget);
  3974. zoomAndNotesToolBar->addSeparator();
  3975. zoomAndNotesToolBar->addWidget( m_clearGhostButton );
  3976. // setup our actual window
  3977. setFocusPolicy( Qt::StrongFocus );
  3978. setFocus();
  3979. setWindowIcon( embed::getIconPixmap( "piano" ) );
  3980. setCurrentPattern( NULL );
  3981. // Connections
  3982. connect( m_editor, SIGNAL( currentPatternChanged() ), this, SIGNAL( currentPatternChanged() ) );
  3983. connect( m_editor, SIGNAL( currentPatternChanged() ), this, SLOT( updateAfterPatternChange() ) );
  3984. }
  3985. const Pattern* PianoRollWindow::currentPattern() const
  3986. {
  3987. return m_editor->currentPattern();
  3988. }
  3989. void PianoRollWindow::setGhostPattern( Pattern* pattern )
  3990. {
  3991. m_editor->setGhostPattern( pattern );
  3992. }
  3993. void PianoRollWindow::setCurrentPattern( Pattern* pattern )
  3994. {
  3995. m_editor->setCurrentPattern( pattern );
  3996. if ( pattern )
  3997. {
  3998. setWindowTitle( tr( "Piano-Roll - %1" ).arg( pattern->name() ) );
  3999. connect( pattern->instrumentTrack(), SIGNAL( nameChanged() ), this, SLOT( updateAfterPatternChange()) );
  4000. connect( pattern, SIGNAL( dataChanged() ), this, SLOT( updateAfterPatternChange() ) );
  4001. }
  4002. else
  4003. {
  4004. setWindowTitle( tr( "Piano-Roll - no pattern" ) );
  4005. }
  4006. }
  4007. bool PianoRollWindow::isRecording() const
  4008. {
  4009. return m_editor->isRecording();
  4010. }
  4011. int PianoRollWindow::quantization() const
  4012. {
  4013. return m_editor->quantization();
  4014. }
  4015. void PianoRollWindow::play()
  4016. {
  4017. m_editor->play();
  4018. }
  4019. void PianoRollWindow::stop()
  4020. {
  4021. m_editor->stop();
  4022. }
  4023. void PianoRollWindow::record()
  4024. {
  4025. stopStepRecording(); //step recording mode is mutually exclusive with other record modes
  4026. m_editor->record();
  4027. }
  4028. void PianoRollWindow::recordAccompany()
  4029. {
  4030. stopStepRecording(); //step recording mode is mutually exclusive with other record modes
  4031. m_editor->recordAccompany();
  4032. }
  4033. void PianoRollWindow::toggleStepRecording()
  4034. {
  4035. if(isRecording())
  4036. {
  4037. // step recording mode is mutually exclusive with other record modes
  4038. // stop them before starting step recording
  4039. stop();
  4040. }
  4041. m_editor->toggleStepRecording();
  4042. updateStepRecordingIcon();
  4043. }
  4044. void PianoRollWindow::stopRecording()
  4045. {
  4046. m_editor->stopRecording();
  4047. }
  4048. void PianoRollWindow::reset()
  4049. {
  4050. m_editor->reset();
  4051. }
  4052. void PianoRollWindow::saveSettings( QDomDocument & doc, QDomElement & de )
  4053. {
  4054. if( !m_editor->ghostNotes().empty() )
  4055. {
  4056. QDomElement ghostNotesRoot = doc.createElement( "ghostnotes" );
  4057. for( Note *note : m_editor->ghostNotes() )
  4058. {
  4059. QDomElement ghostNoteNode = doc.createElement( "ghostnote" );
  4060. ghostNoteNode.setAttribute( "len", note->length() );
  4061. ghostNoteNode.setAttribute( "key", note->key() );
  4062. ghostNoteNode.setAttribute( "pos", note->pos() );
  4063. ghostNotesRoot.appendChild(ghostNoteNode);
  4064. }
  4065. de.appendChild( ghostNotesRoot );
  4066. }
  4067. if (m_editor->m_markedSemiTones.length() > 0)
  4068. {
  4069. QDomElement markedSemiTonesRoot = doc.createElement("markedSemiTones");
  4070. for (int ix = 0; ix < m_editor->m_markedSemiTones.size(); ++ix)
  4071. {
  4072. QDomElement semiToneNode = doc.createElement("semiTone");
  4073. semiToneNode.setAttribute("key", m_editor->m_markedSemiTones.at(ix));
  4074. markedSemiTonesRoot.appendChild(semiToneNode);
  4075. }
  4076. de.appendChild(markedSemiTonesRoot);
  4077. }
  4078. MainWindow::saveWidgetState( this, de );
  4079. }
  4080. void PianoRollWindow::loadSettings( const QDomElement & de )
  4081. {
  4082. m_editor->loadGhostNotes( de.firstChildElement("ghostnotes") );
  4083. m_editor->loadMarkedSemiTones(de.firstChildElement("markedSemiTones"));
  4084. MainWindow::restoreWidgetState( this, de );
  4085. // update margins here because we're later in the startup process
  4086. // We can't earlier because everything is still starting with the
  4087. // WHITE_KEY_WIDTH default
  4088. QMargins qm = m_editor->m_stepRecorderWidget.margins();
  4089. qm.setLeft(m_editor->m_whiteKeyWidth);
  4090. m_editor->m_stepRecorderWidget.setMargins(qm);
  4091. m_editor->m_timeLine->setXOffset(m_editor->m_whiteKeyWidth);
  4092. }
  4093. QSize PianoRollWindow::sizeHint() const
  4094. {
  4095. return { INITIAL_PIANOROLL_WIDTH, INITIAL_PIANOROLL_HEIGHT };
  4096. }
  4097. bool PianoRollWindow::hasFocus() const
  4098. {
  4099. return m_editor->hasFocus();
  4100. }
  4101. void PianoRollWindow::updateAfterPatternChange()
  4102. {
  4103. patternRenamed();
  4104. updateStepRecordingIcon(); //pattern change turn step recording OFF - update icon accordingly
  4105. }
  4106. void PianoRollWindow::patternRenamed()
  4107. {
  4108. if ( currentPattern() )
  4109. {
  4110. setWindowTitle( tr( "Piano-Roll - %1" ).arg( currentPattern()->name() ) );
  4111. }
  4112. else
  4113. {
  4114. setWindowTitle( tr( "Piano-Roll - no pattern" ) );
  4115. }
  4116. }
  4117. void PianoRollWindow::ghostPatternSet( bool state )
  4118. {
  4119. m_clearGhostButton->setEnabled( state );
  4120. }
  4121. void PianoRollWindow::focusInEvent( QFocusEvent * event )
  4122. {
  4123. // when the window is given focus, also give focus to the actual piano roll
  4124. m_editor->setFocus( event->reason() );
  4125. }
  4126. void PianoRollWindow::stopStepRecording()
  4127. {
  4128. if(m_editor->isStepRecording())
  4129. {
  4130. m_editor->toggleStepRecording();
  4131. updateStepRecordingIcon();
  4132. }
  4133. }
  4134. void PianoRollWindow::updateStepRecordingIcon()
  4135. {
  4136. if(m_editor->isStepRecording())
  4137. {
  4138. m_toggleStepRecordingAction->setIcon(embed::getIconPixmap("record_step_on"));
  4139. }
  4140. else
  4141. {
  4142. m_toggleStepRecordingAction->setIcon(embed::getIconPixmap("record_step_off"));
  4143. }
  4144. }