123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995 |
- /*
- * PianoRoll.cpp - implementation of piano roll which is used for actual
- * writing of melodies
- *
- * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
- * Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
- *
- * This file is part of LMMS - https://lmms.io
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program (see COPYING); if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA.
- *
- */
- #include "PianoRoll.h"
- #include <QApplication>
- #include <QClipboard>
- #include <QKeyEvent>
- #include <QLabel>
- #include <QLayout>
- #include <QMdiArea>
- #include <QPainter>
- #include <QPointer>
- #include <QScrollBar>
- #include <QStyleOption>
- #include <QtMath>
- #include <QToolButton>
- #ifndef __USE_XOPEN
- #define __USE_XOPEN
- #endif
- #include <math.h>
- #include <utility>
- #include "AutomationEditor.h"
- #include "ActionGroup.h"
- #include "BBTrackContainer.h"
- #include "Clipboard.h"
- #include "ComboBox.h"
- #include "ConfigManager.h"
- #include "DataFile.h"
- #include "debug.h"
- #include "DeprecationHelper.h"
- #include "DetuningHelper.h"
- #include "embed.h"
- #include "GuiApplication.h"
- #include "gui_templates.h"
- #include "InstrumentTrack.h"
- #include "MainWindow.h"
- #include "Pattern.h"
- #include "SongEditor.h"
- #include "StepRecorderWidget.h"
- #include "TextFloat.h"
- #include "TimeLineWidget.h"
- using std::move;
- typedef AutomationPattern::timeMap timeMap;
- // some constants...
- const int INITIAL_PIANOROLL_WIDTH = 860;
- const int INITIAL_PIANOROLL_HEIGHT = 485;
- const int SCROLLBAR_SIZE = 12;
- const int PIANO_X = 0;
- const int WHITE_KEY_WIDTH = 64;
- const int BLACK_KEY_WIDTH = 41;
- const int DEFAULT_KEY_LINE_HEIGHT = 12;
- const int DEFAULT_CELL_WIDTH = 12;
- const int NOTE_EDIT_RESIZE_BAR = 6;
- const int NOTE_EDIT_MIN_HEIGHT = 50;
- const int KEY_AREA_MIN_HEIGHT = DEFAULT_KEY_LINE_HEIGHT * 10;
- const int PR_BOTTOM_MARGIN = SCROLLBAR_SIZE;
- const int PR_TOP_MARGIN = 18;
- const int PR_RIGHT_MARGIN = SCROLLBAR_SIZE;
- // width of area used for resizing (the grip at the end of a note)
- const int RESIZE_AREA_WIDTH = 9;
- // width of line for setting volume/panning of note
- const int NOTE_EDIT_LINE_WIDTH = 3;
- // key where to start
- const int INITIAL_START_KEY = Key_C + Octave_4 * KeysPerOctave;
- // number of each note to provide in quantization and note lengths
- const int NUM_EVEN_LENGTHS = 6;
- const int NUM_TRIPLET_LENGTHS = 5;
- QPixmap * PianoRoll::s_toolDraw = NULL;
- QPixmap * PianoRoll::s_toolErase = NULL;
- QPixmap * PianoRoll::s_toolSelect = NULL;
- QPixmap * PianoRoll::s_toolMove = NULL;
- QPixmap * PianoRoll::s_toolOpen = NULL;
- QPixmap* PianoRoll::s_toolRazor = nullptr;
- TextFloat * PianoRoll::s_textFloat = NULL;
- static QString s_noteStrings[12] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
- static QString getNoteString( int key )
- {
- return s_noteStrings[key % 12] + QString::number( static_cast<int>( key / KeysPerOctave ) );
- }
- // used for drawing of piano
- PianoRoll::PianoRollKeyTypes PianoRoll::prKeyOrder[] =
- {
- PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY,
- PR_WHITE_KEY_SMALL, PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG,
- PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY, PR_WHITE_KEY_SMALL
- } ;
- const int DEFAULT_PR_PPB = DEFAULT_CELL_WIDTH * DefaultStepsPerBar;
- const QVector<double> PianoRoll::m_zoomLevels =
- {0.125f, 0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 4.0f, 8.0f};
- const QVector<double> PianoRoll::m_zoomYLevels =
- {0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 4.0f};
- PianoRoll::PianoRoll() :
- m_nemStr( QVector<QString>() ),
- m_noteEditMenu( NULL ),
- m_semiToneMarkerMenu( NULL ),
- m_zoomingModel(),
- m_zoomingYModel(),
- m_quantizeModel(),
- m_noteLenModel(),
- m_scaleModel(),
- m_chordModel(),
- m_pattern( NULL ),
- m_currentPosition(),
- m_recording( false ),
- m_currentNote( NULL ),
- m_action( ActionNone ),
- m_noteEditMode( NoteEditVolume ),
- m_moveBoundaryLeft( 0 ),
- m_moveBoundaryTop( 0 ),
- m_moveBoundaryRight( 0 ),
- m_moveBoundaryBottom( 0 ),
- m_mouseDownKey( 0 ),
- m_mouseDownTick( 0 ),
- m_lastMouseX( 0 ),
- m_lastMouseY( 0 ),
- m_notesEditHeight( 100 ),
- m_userSetNotesEditHeight(100),
- m_ppb( DEFAULT_PR_PPB ),
- m_keyLineHeight(DEFAULT_KEY_LINE_HEIGHT),
- m_octaveHeight(m_keyLineHeight * KeysPerOctave),
- m_whiteKeySmallHeight(qFloor(m_keyLineHeight * 1.5)),
- m_whiteKeyBigHeight(m_keyLineHeight * 2),
- m_blackKeyHeight(m_keyLineHeight),
- m_lenOfNewNotes( TimePos( 0, DefaultTicksPerBar/4 ) ),
- m_lastNoteVolume( DefaultVolume ),
- m_lastNotePanning( DefaultPanning ),
- m_minResizeLen( 0 ),
- m_startKey( INITIAL_START_KEY ),
- m_lastKey( 0 ),
- m_editMode( ModeDraw ),
- m_ctrlMode( ModeDraw ),
- m_mouseDownRight( false ),
- m_firstRazorSplit(false),
- m_scrollBack( false ),
- m_stepRecorderWidget(this, DEFAULT_PR_PPB, PR_TOP_MARGIN, PR_BOTTOM_MARGIN + m_notesEditHeight, WHITE_KEY_WIDTH, 0),
- m_stepRecorder(*this, m_stepRecorderWidget),
- m_barLineColor( 0, 0, 0 ),
- m_beatLineColor( 0, 0, 0 ),
- m_lineColor( 0, 0, 0 ),
- m_noteModeColor( 0, 0, 0 ),
- m_noteColor( 0, 0, 0 ),
- m_ghostNoteColor( 0, 0, 0 ),
- m_ghostNoteTextColor( 0, 0, 0 ),
- m_barColor( 0, 0, 0 ),
- m_selectedNoteColor( 0, 0, 0 ),
- m_textColor( 0, 0, 0 ),
- m_textColorLight( 0, 0, 0 ),
- m_textShadow( 0, 0, 0 ),
- m_markedSemitoneColor( 0, 0, 0 ),
- m_razorCutLineColor(0, 0, 0),
- m_noteOpacity( 255 ),
- m_ghostNoteOpacity( 255 ),
- m_noteBorders( true ),
- m_ghostNoteBorders( true ),
- m_backgroundShade( 0, 0, 0 ),
- m_whiteKeyWidth(WHITE_KEY_WIDTH),
- m_blackKeyWidth(BLACK_KEY_WIDTH)
- {
- // gui names of edit modes
- m_nemStr.push_back( tr( "Note Velocity" ) );
- m_nemStr.push_back( tr( "Note Panning" ) );
- m_noteEditMenu = new QMenu( this );
- m_noteEditMenu->clear();
- for( int i = 0; i < m_nemStr.size(); ++i )
- {
- QAction * act = new QAction( m_nemStr.at(i), this );
- connect( act, &QAction::triggered, [this, i](){ changeNoteEditMode(i); } );
- m_noteEditMenu->addAction( act );
- }
- m_semiToneMarkerMenu = new QMenu( this );
- QAction* markSemitoneAction = new QAction( tr("Mark/unmark current semitone"), this );
- QAction* markAllOctaveSemitonesAction = new QAction( tr("Mark/unmark all corresponding octave semitones"), this );
- QAction* markScaleAction = new QAction( tr("Mark current scale"), this );
- QAction* markChordAction = new QAction( tr("Mark current chord"), this );
- QAction* unmarkAllAction = new QAction( tr("Unmark all"), this );
- QAction* copyAllNotesAction = new QAction( tr("Select all notes on this key"), this);
- connect( markSemitoneAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentSemiTone); });
- connect( markAllOctaveSemitonesAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkAllOctaveSemiTones); });
- connect( markScaleAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentScale); });
- connect( markChordAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentChord); });
- connect( unmarkAllAction, &QAction::triggered, [this](){ markSemiTone(stmaUnmarkAll); });
- connect( copyAllNotesAction, &QAction::triggered, [this](){ markSemiTone(stmaCopyAllNotesOnKey); });
- markScaleAction->setEnabled( false );
- markChordAction->setEnabled( false );
- connect( this, SIGNAL(semiToneMarkerMenuScaleSetEnabled(bool)), markScaleAction, SLOT(setEnabled(bool)) );
- connect( this, SIGNAL(semiToneMarkerMenuChordSetEnabled(bool)), markChordAction, SLOT(setEnabled(bool)) );
- m_semiToneMarkerMenu->addAction( markSemitoneAction );
- m_semiToneMarkerMenu->addAction( markAllOctaveSemitonesAction );
- m_semiToneMarkerMenu->addAction( markScaleAction );
- m_semiToneMarkerMenu->addAction( markChordAction );
- m_semiToneMarkerMenu->addAction( unmarkAllAction );
- m_semiToneMarkerMenu->addAction( copyAllNotesAction );
- // init pixmaps
- if( s_toolDraw == NULL )
- {
- s_toolDraw = new QPixmap( embed::getIconPixmap( "edit_draw" ) );
- }
- if( s_toolErase == NULL )
- {
- s_toolErase= new QPixmap( embed::getIconPixmap( "edit_erase" ) );
- }
- if( s_toolSelect == NULL )
- {
- s_toolSelect = new QPixmap( embed::getIconPixmap( "edit_select" ) );
- }
- if( s_toolMove == NULL )
- {
- s_toolMove = new QPixmap( embed::getIconPixmap( "edit_move" ) );
- }
- if( s_toolOpen == NULL )
- {
- s_toolOpen = new QPixmap( embed::getIconPixmap( "automation" ) );
- }
- if (s_toolRazor == nullptr)
- {
- s_toolRazor = new QPixmap(embed::getIconPixmap("razor"));
- }
- // init text-float
- if( s_textFloat == NULL )
- {
- s_textFloat = new TextFloat;
- }
- setAttribute( Qt::WA_OpaquePaintEvent, true );
- // add time-line
- m_timeLine = new TimeLineWidget(m_whiteKeyWidth, 0, m_ppb,
- Engine::getSong()->getPlayPos(
- Song::Mode_PlayPattern ),
- m_currentPosition,
- Song::Mode_PlayPattern, this );
- connect( this, SIGNAL( positionChanged( const TimePos & ) ),
- m_timeLine, SLOT( updatePosition( const TimePos & ) ) );
- connect( m_timeLine, SIGNAL( positionChanged( const TimePos & ) ),
- this, SLOT( updatePosition( const TimePos & ) ) );
- // white position line follows timeline marker
- m_positionLine = new PositionLine(this);
- //update timeline when in step-recording mode
- connect( &m_stepRecorderWidget, SIGNAL( positionChanged( const TimePos & ) ),
- this, SLOT( updatePositionStepRecording( const TimePos & ) ) );
- // update timeline when in record-accompany mode
- connect( Engine::getSong()->getPlayPos( Song::Mode_PlaySong ).m_timeLine,
- SIGNAL( positionChanged( const TimePos & ) ),
- this,
- SLOT( updatePositionAccompany( const TimePos & ) ) );
- // TODO
- /* connect( engine::getSong()->getPlayPos( Song::Mode_PlayBB ).m_timeLine,
- SIGNAL( positionChanged( const TimePos & ) ),
- this,
- SLOT( updatePositionAccompany( const TimePos & ) ) );*/
- removeSelection();
- // init scrollbars
- m_leftRightScroll = new QScrollBar( Qt::Horizontal, this );
- m_leftRightScroll->setSingleStep( 1 );
- connect( m_leftRightScroll, SIGNAL( valueChanged( int ) ), this,
- SLOT( horScrolled( int ) ) );
- m_topBottomScroll = new QScrollBar( Qt::Vertical, this );
- m_topBottomScroll->setSingleStep( 1 );
- m_topBottomScroll->setPageStep( 20 );
- connect( m_topBottomScroll, SIGNAL( valueChanged( int ) ), this,
- SLOT( verScrolled( int ) ) );
- // setup zooming-stuff
- for( float const & zoomLevel : m_zoomLevels )
- {
- m_zoomingModel.addItem( QString( "%1\%" ).arg( zoomLevel * 100 ) );
- }
- m_zoomingModel.setValue( m_zoomingModel.findText( "100%" ) );
- connect( &m_zoomingModel, SIGNAL( dataChanged() ),
- this, SLOT( zoomingChanged() ) );
- // zoom y
- for (float const & zoomLevel : m_zoomYLevels)
- {
- m_zoomingYModel.addItem(QString( "%1\%" ).arg(zoomLevel * 100));
- }
- m_zoomingYModel.setValue(m_zoomingYModel.findText("100%"));
- connect(&m_zoomingYModel, SIGNAL(dataChanged()),
- this, SLOT(zoomingYChanged()));
- // Set up quantization model
- m_quantizeModel.addItem( tr( "Note lock" ) );
- for (auto q : Quantizations) {
- m_quantizeModel.addItem(QString("1/%1").arg(q));
- }
- m_quantizeModel.setValue( m_quantizeModel.findText( "1/16" ) );
- connect( &m_quantizeModel, SIGNAL( dataChanged() ),
- this, SLOT( quantizeChanged() ) );
- // Set up note length model
- m_noteLenModel.addItem( tr( "Last note" ),
- std::make_unique<PixmapLoader>( "edit_draw" ) );
- const QString pixmaps[] = { "whole", "half", "quarter", "eighth",
- "sixteenth", "thirtysecond", "triplethalf",
- "tripletquarter", "tripleteighth",
- "tripletsixteenth", "tripletthirtysecond" } ;
- for( int i = 0; i < NUM_EVEN_LENGTHS; ++i )
- {
- auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i] );
- m_noteLenModel.addItem( "1/" + QString::number( 1 << i ), ::move(loader) );
- }
- for( int i = 0; i < NUM_TRIPLET_LENGTHS; ++i )
- {
- auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i+NUM_EVEN_LENGTHS] );
- m_noteLenModel.addItem( "1/" + QString::number( (1 << i) * 3 ), ::move(loader) );
- }
- m_noteLenModel.setValue( 0 );
- // Note length change can cause a redraw if Q is set to lock
- connect( &m_noteLenModel, SIGNAL( dataChanged() ),
- this, SLOT( noteLengthChanged() ) );
- // Set up key selection dropdown
- m_keyModel.addItem(tr("No key"));
- // Use piano roll note strings for key dropdown
- for (int i = 0; i < 12; i++) { m_keyModel.addItem(s_noteStrings[i]); }
- m_keyModel.setValue(0); // start with "No key"
- connect(&m_keyModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
- // Set up scale model
- const InstrumentFunctionNoteStacking::ChordTable& chord_table =
- InstrumentFunctionNoteStacking::ChordTable::getInstance();
- m_scaleModel.addItem( tr("No scale") );
- for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
- {
- if( chord.isScale() )
- {
- m_scaleModel.addItem( chord.getName() );
- }
- }
- m_scaleModel.setValue( 0 );
- // connect scale change to key change so it auto-highlights with scale as well
- connect(&m_scaleModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
- // change can update m_semiToneMarkerMenu
- connect( &m_scaleModel, SIGNAL( dataChanged() ),
- this, SLOT( updateSemiToneMarkerMenu() ) );
- // Set up chord model
- m_chordModel.addItem( tr("No chord") );
- for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
- {
- if( ! chord.isScale() )
- {
- m_chordModel.addItem( chord.getName() );
- }
- }
- m_chordModel.setValue( 0 );
- // change can update m_semiToneMarkerMenu
- connect( &m_chordModel, SIGNAL( dataChanged() ),
- this, SLOT( updateSemiToneMarkerMenu() ) );
- setFocusPolicy( Qt::StrongFocus );
- setFocus();
- setMouseTracking( true );
- connect( &m_scaleModel, SIGNAL( dataChanged() ),
- this, SLOT( updateSemiToneMarkerMenu() ) );
- connect( Engine::getSong(), SIGNAL( timeSignatureChanged( int, int ) ),
- this, SLOT( update() ) );
- //connection for selecion from timeline
- connect( m_timeLine, SIGNAL( regionSelectedFromPixels( int, int ) ),
- this, SLOT( selectRegionFromPixels( int, int ) ) );
- m_stepRecorder.initialize();
- }
- void PianoRoll::reset()
- {
- m_lastNoteVolume = DefaultVolume;
- m_lastNotePanning = DefaultPanning;
- clearGhostPattern();
- }
- void PianoRoll::showTextFloat(const QString &text, const QPoint &pos, int timeout)
- {
- s_textFloat->setText( text );
- // show the float, offset slightly so as to not obscure anything
- s_textFloat->moveGlobal( this, pos + QPoint(4, 16) );
- if (timeout == -1)
- {
- s_textFloat->show();
- }
- else
- {
- s_textFloat->setVisibilityTimeOut( timeout );
- }
- }
- void PianoRoll::showVolTextFloat(volume_t vol, const QPoint &pos, int timeout)
- {
- //! \todo display velocity for MIDI-based instruments
- // possibly dBFS values too? not sure if it makes sense for note volumes...
- showTextFloat( tr("Velocity: %1%").arg( vol ), pos, timeout );
- }
- void PianoRoll::showPanTextFloat(panning_t pan, const QPoint &pos, int timeout)
- {
- QString text;
- if( pan < 0 )
- {
- text = tr("Panning: %1% left").arg( qAbs( pan ) );
- }
- else if( pan > 0 )
- {
- text = tr("Panning: %1% right").arg( qAbs( pan ) );
- }
- else
- {
- text = tr("Panning: center");
- }
- showTextFloat( text, pos, timeout );
- }
- void PianoRoll::changeNoteEditMode( int i )
- {
- m_noteEditMode = (NoteEditMode) i;
- repaint();
- }
- void PianoRoll::markSemiTone(int i, bool fromMenu)
- {
- const int key = fromMenu
- ? getKey(mapFromGlobal(m_semiToneMarkerMenu->pos()).y())
- : m_keyModel.value() - 1;
- const InstrumentFunctionNoteStacking::Chord * chord = nullptr;
- // if "No key" is selected, key is -1, unmark all semitones
- // or if scale changed from toolbar to "No scale", unmark all semitones
- if (!fromMenu && (key < 0 || m_scaleModel.value() == 0)) { i = stmaUnmarkAll; }
- switch( static_cast<SemiToneMarkerAction>( i ) )
- {
- case stmaUnmarkAll:
- m_markedSemiTones.clear();
- break;
- case stmaMarkCurrentSemiTone:
- {
- QList<int>::iterator it = std::find( m_markedSemiTones.begin(), m_markedSemiTones.end(), key );
- if( it != m_markedSemiTones.end() )
- {
- m_markedSemiTones.erase( it );
- }
- else
- {
- m_markedSemiTones.push_back( key );
- }
- break;
- }
- case stmaMarkAllOctaveSemiTones:
- {
- QList<int> aok = getAllOctavesForKey(key);
- if ( m_markedSemiTones.contains(key) )
- {
- // lets erase all of the ones that match this by octave
- QList<int>::iterator i;
- for (int ix = 0; ix < aok.size(); ++ix)
- {
- i = std::find(m_markedSemiTones.begin(), m_markedSemiTones.end(), aok.at(ix));
- if (i != m_markedSemiTones.end())
- {
- m_markedSemiTones.erase(i);
- }
- }
- }
- else
- {
- // we should add all of the ones that match this by octave
- m_markedSemiTones.append(aok);
- }
- break;
- }
- case stmaMarkCurrentScale:
- chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
- .getScaleByName( m_scaleModel.currentText() );
- case stmaMarkCurrentChord:
- {
- if( ! chord )
- {
- chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
- .getChordByName( m_chordModel.currentText() );
- }
- if( chord->isEmpty() )
- {
- break;
- }
- else if( chord->isScale() )
- {
- m_markedSemiTones.clear();
- }
- const int first = chord->isScale() ? 0 : key;
- const int last = chord->isScale() ? NumKeys : key + chord->last();
- const int cap = ( chord->isScale() || chord->last() == 0 ) ? KeysPerOctave : chord->last();
- for( int i = first; i <= last; i++ )
- {
- if( chord->hasSemiTone( ( i + cap - ( key % cap ) ) % cap ) )
- {
- m_markedSemiTones.push_back( i );
- }
- }
- break;
- }
- case stmaCopyAllNotesOnKey:
- {
- selectNotesOnKey();
- break;
- }
- default:
- ;
- }
- std::sort( m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>() );
- QList<int>::iterator new_end = std::unique( m_markedSemiTones.begin(), m_markedSemiTones.end() );
- m_markedSemiTones.erase( new_end, m_markedSemiTones.end() );
- // until we move the mouse the window won't update, force redraw
- update();
- }
- PianoRoll::~PianoRoll()
- {
- }
- void PianoRoll::setGhostPattern( Pattern* newPattern )
- {
- // Expects a pointer to a pattern or nullptr.
- m_ghostNotes.clear();
- if( newPattern != nullptr )
- {
- for( Note *note : newPattern->notes() )
- {
- Note * new_note = new Note( note->length(), note->pos(), note->key() );
- m_ghostNotes.push_back( new_note );
- }
- emit ghostPatternSet( true );
- }
- }
- void PianoRoll::loadGhostNotes( const QDomElement & de )
- {
- // Load ghost notes from DOM element.
- if( de.isElement() )
- {
- QDomNode node = de.firstChild();
- while( !node.isNull() )
- {
- Note * n = new Note;
- n->restoreState( node.toElement() );
- m_ghostNotes.push_back( n );
- node = node.nextSibling();
- }
- emit ghostPatternSet( true );
- }
- }
- void PianoRoll::clearGhostPattern()
- {
- setGhostPattern( nullptr );
- emit ghostPatternSet( false );
- update();
- }
- void PianoRoll::glueNotes()
- {
- if (hasValidPattern())
- {
- NoteVector selectedNotes = getSelectedNotes();
- if (selectedNotes.empty())
- {
- TextFloat::displayMessage( tr( "Glue notes failed" ),
- tr( "Please select notes to glue first." ),
- embed::getIconPixmap( "glue", 24, 24 ),
- 3000 );
- return;
- }
- // Make undo possible
- m_pattern->addJournalCheckPoint();
- // Sort notes on key and then pos.
- std::sort(selectedNotes.begin(), selectedNotes.end(),
- [](const Note * note, const Note * compareNote) -> bool
- {
- if (note->key() == compareNote->key())
- {
- return note->pos() < compareNote->pos();
- }
- return note->key() < compareNote->key();
- });
- QList<Note *> noteToRemove;
- NoteVector::iterator note = selectedNotes.begin();
- auto nextNote = note+1;
- NoteVector::iterator end = selectedNotes.end();
- while (note != end && nextNote != end)
- {
- // key and position match for glue. The notes are already
- // sorted so we don't need to test that nextNote is the same
- // position or next in sequence.
- if ((*note)->key() == (*nextNote)->key()
- && (*nextNote)->pos() <= (*note)->pos()
- + qMax(TimePos(0), (*note)->length()))
- {
- (*note)->setLength(qMax((*note)->length(),
- TimePos((*nextNote)->endPos() - (*note)->pos())));
- noteToRemove.push_back(*nextNote);
- ++nextNote;
- }
- // key or position doesn't match
- else
- {
- note = nextNote;
- nextNote = note+1;
- }
- }
- // Remove old notes
- for (int i = 0; i < noteToRemove.count(); ++i)
- {
- m_pattern->removeNote(noteToRemove[i]);
- }
- update();
- }
- }
- void PianoRoll::loadMarkedSemiTones(const QDomElement & de)
- {
- // clear marked semitones to prevent leftover marks
- m_markedSemiTones.clear();
- if (de.isElement())
- {
- QDomNode node = de.firstChild();
- while (!node.isNull())
- {
- bool ok;
- int key = node.toElement().attribute(
- QString("key"), QString("-1")).toInt(&ok, 10);
- if (ok && key >= 0)
- {
- m_markedSemiTones.append(key);
- }
- node = node.nextSibling();
- }
- }
- // from markSemiTone, required otherwise marks will not show
- std::sort(m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>());
- QList<int>::iterator new_end = std::unique(m_markedSemiTones.begin(), m_markedSemiTones.end());
- m_markedSemiTones.erase(new_end, m_markedSemiTones.end());
- }
- void PianoRoll::setCurrentPattern( Pattern* newPattern )
- {
- if( hasValidPattern() )
- {
- m_pattern->instrumentTrack()->disconnect( this );
- }
- // force the song-editor to stop playing if it played pattern before
- if( Engine::getSong()->isPlaying() &&
- Engine::getSong()->playMode() == Song::Mode_PlayPattern )
- {
- Engine::getSong()->playPattern( NULL );
- }
- if(m_stepRecorder.isRecording())
- {
- m_stepRecorder.stop();
- }
- // set new data
- m_pattern = newPattern;
- m_currentPosition = 0;
- m_currentNote = NULL;
- m_startKey = INITIAL_START_KEY;
- m_stepRecorder.setCurrentPattern(newPattern);
- if( ! hasValidPattern() )
- {
- //resizeEvent( NULL );
- update();
- emit currentPatternChanged();
- return;
- }
- m_leftRightScroll->setValue( 0 );
- // determine the central key so that we can scroll to it
- int central_key = 0;
- int total_notes = 0;
- for( const Note *note : m_pattern->notes() )
- {
- if( note->length() > 0 )
- {
- central_key += note->key();
- ++total_notes;
- }
- }
- if( total_notes > 0 )
- {
- central_key = central_key / total_notes -
- ( KeysPerOctave * NumOctaves - m_totalKeysToScroll ) / 2;
- m_startKey = qBound( 0, central_key, NumOctaves * KeysPerOctave );
- }
- // resizeEvent() does the rest for us (scrolling, range-checking
- // of start-notes and so on...)
- resizeEvent( NULL );
- // make sure to always get informed about the pattern being destroyed
- connect( m_pattern, SIGNAL( destroyedPattern( Pattern* ) ), this, SLOT( hidePattern( Pattern* ) ) );
- connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOn( const Note& ) ), this, SLOT( startRecordNote( const Note& ) ) );
- connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOff( const Note& ) ), this, SLOT( finishRecordNote( const Note& ) ) );
- connect( m_pattern->instrumentTrack()->pianoModel(), SIGNAL( dataChanged() ), this, SLOT( update() ) );
- update();
- emit currentPatternChanged();
- }
- void PianoRoll::hidePattern( Pattern* pattern )
- {
- if( m_pattern == pattern )
- {
- setCurrentPattern( NULL );
- }
- }
- void PianoRoll::selectRegionFromPixels( int xStart, int xEnd )
- {
- xStart -= m_whiteKeyWidth;
- xEnd -= m_whiteKeyWidth;
- // select an area of notes
- int posTicks = xStart * TimePos::ticksPerBar() / m_ppb +
- m_currentPosition;
- int keyNum = 0;
- m_selectStartTick = posTicks;
- m_selectedTick = 0;
- m_selectStartKey = keyNum;
- m_selectedKeys = 1;
- // change size of selection
- // get tick in which the cursor is posated
- posTicks = xEnd * TimePos::ticksPerBar() / m_ppb +
- m_currentPosition;
- keyNum = 120;
- m_selectedTick = posTicks - m_selectStartTick;
- if( (int) m_selectStartTick + m_selectedTick < 0 )
- {
- m_selectedTick = -static_cast<int>(
- m_selectStartTick );
- }
- m_selectedKeys = keyNum - m_selectStartKey;
- if( keyNum <= m_selectStartKey )
- {
- --m_selectedKeys;
- }
- computeSelectedNotes( false );
- }
- void PianoRoll::drawNoteRect( QPainter & p, int x, int y,
- int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
- const QColor & selCol, const int noteOpc, const bool borders, bool drawNoteName )
- {
- ++x;
- ++y;
- width -= 2;
- if( width <= 0 )
- {
- width = 2;
- }
- // Volume
- float const volumeRange = static_cast<float>(MaxVolume - MinVolume);
- float const volumeSpan = static_cast<float>(n->getVolume() - MinVolume);
- float const volumeRatio = volumeSpan / volumeRange;
- int volVal = qMin( 255, 100 + static_cast<int>( volumeRatio * 155.0f) );
- // Panning
- float const panningRange = static_cast<float>(PanningRight - PanningLeft);
- float const leftPanSpan = static_cast<float>(PanningRight - n->getPanning());
- float const rightPanSpan = static_cast<float>(n->getPanning() - PanningLeft);
- float leftPercent = qMin<float>( 1.0f, leftPanSpan / panningRange * 2.0f );
- float rightPercent = qMin<float>( 1.0f, rightPanSpan / panningRange * 2.0f );
- QColor col = QColor( noteCol );
- QPen pen;
- if( n->selected() )
- {
- col = QColor( selCol );
- }
- const int borderWidth = borders ? 1 : 0;
- const int noteHeight = m_keyLineHeight - 1 - borderWidth;
- int noteWidth = width + 1 - borderWidth;
- // adjust note to make it a bit faded if it has a lower volume
- // in stereo using gradients
- QColor lcol = QColor::fromHsv( col.hue(), col.saturation(),
- static_cast<int>(volVal * leftPercent), noteOpc );
- QColor rcol = QColor::fromHsv( col.hue(), col.saturation(),
- static_cast<int>(volVal * rightPercent), noteOpc );
- QLinearGradient gradient( x, y, x, y + noteHeight );
- gradient.setColorAt( 0, rcol );
- gradient.setColorAt( 1, lcol );
- p.setBrush( gradient );
- if ( borders )
- {
- p.setPen( col );
- }
- else
- {
- p.setPen( Qt::NoPen );
- }
- p.drawRect( x, y, noteWidth, noteHeight );
- // Draw note key text
- if (drawNoteName)
- {
- p.save();
- int const noteTextHeight = static_cast<int>(noteHeight * 0.8);
- if (noteTextHeight > 6)
- {
- QString noteKeyString = getNoteString(n->key());
- QFont noteFont(p.font());
- noteFont.setPixelSize(noteTextHeight);
- QFontMetrics fontMetrics(noteFont);
- QSize textSize = fontMetrics.size(Qt::TextSingleLine, noteKeyString);
- int const distanceToBorder = 2;
- int const xOffset = borderWidth + distanceToBorder;
- // noteTextHeight, textSize are not suitable for determining vertical spacing,
- // capHeight() can be used for this, but requires Qt 5.8.
- // We use boundingRect() with QChar (the QString version returns wrong value).
- QRect const boundingRect = fontMetrics.boundingRect(QChar::fromLatin1('H'));
- int const yOffset = (noteHeight - boundingRect.top() - boundingRect.bottom()) / 2;
- if (textSize.width() < noteWidth - xOffset)
- {
- p.setPen(noteTextColor);
- p.setFont(noteFont);
- QPoint textStart(x + xOffset, y + yOffset);
- p.drawText(textStart, noteKeyString);
- }
- }
- p.restore();
- }
- // draw the note endmark, to hint the user to resize
- p.setBrush( col );
- if( width > 2 )
- {
- const int endmarkWidth = 3 - borderWidth;
- p.drawRect( x + noteWidth - endmarkWidth, y, endmarkWidth, noteHeight );
- }
- }
- void PianoRoll::drawDetuningInfo( QPainter & _p, const Note * _n, int _x,
- int _y ) const
- {
- int middle_y = _y + m_keyLineHeight / 2;
- _p.setPen(m_noteColor);
- _p.setClipRect(
- m_whiteKeyWidth,
- PR_TOP_MARGIN,
- width() - m_whiteKeyWidth,
- keyAreaBottom() - PR_TOP_MARGIN);
- int old_x = 0;
- int old_y = 0;
- timeMap & map = _n->detuning()->automationPattern()->getTimeMap();
- for( timeMap::ConstIterator it = map.begin(); it != map.end(); ++it )
- {
- int pos_ticks = it.key();
- int pos_x = _x + pos_ticks * m_ppb / TimePos::ticksPerBar();
- const float level = it.value();
- int pos_y = middle_y - level * m_keyLineHeight;
- if( old_x != 0 && old_y != 0 )
- {
- switch( _n->detuning()->automationPattern()->progressionType() )
- {
- case AutomationPattern::DiscreteProgression:
- _p.drawLine( old_x, old_y, pos_x, old_y );
- _p.drawLine( pos_x, old_y, pos_x, pos_y );
- break;
- case AutomationPattern::CubicHermiteProgression: /* TODO */
- case AutomationPattern::LinearProgression:
- _p.drawLine( old_x, old_y, pos_x, pos_y );
- break;
- }
- }
- _p.drawLine( pos_x - 1, pos_y, pos_x + 1, pos_y );
- _p.drawLine( pos_x, pos_y - 1, pos_x, pos_y + 1 );
- old_x = pos_x;
- old_y = pos_y;
- }
- }
- void PianoRoll::removeSelection()
- {
- m_selectStartTick = 0;
- m_selectedTick = 0;
- m_selectStartKey = 0;
- m_selectedKeys = 0;
- }
- void PianoRoll::clearSelectedNotes()
- {
- if( m_pattern != NULL )
- {
- for( Note *note : m_pattern->notes() )
- {
- note->setSelected( false );
- }
- }
- }
- void PianoRoll::shiftSemiTone(int amount) //Shift notes by amount semitones
- {
- if (!hasValidPattern()) { return; }
- auto selectedNotes = getSelectedNotes();
- //If no notes are selected, shift all of them, otherwise shift selection
- if (selectedNotes.empty()) { return shiftSemiTone(m_pattern->notes(), amount); }
- else { return shiftSemiTone(selectedNotes, amount); }
- }
- void PianoRoll::shiftSemiTone(NoteVector notes, int amount)
- {
- m_pattern->addJournalCheckPoint();
- for (Note *note : notes) { note->setKey( note->key() + amount ); }
- m_pattern->rearrangeAllNotes();
- m_pattern->dataChanged();
- //We modified the song
- update();
- gui->songEditor()->update();
- }
- void PianoRoll::shiftPos(int amount) //Shift notes pos by amount
- {
- if (!hasValidPattern()) { return; }
- auto selectedNotes = getSelectedNotes();
- //If no notes are selected, shift all of them, otherwise shift selection
- if (selectedNotes.empty()) { return shiftPos(m_pattern->notes(), amount); }
- else { return shiftPos(selectedNotes, amount); }
- }
- void PianoRoll::shiftPos(NoteVector notes, int amount)
- {
- m_pattern->addJournalCheckPoint();
- auto leftMostPos = notes.first()->pos();
- //Limit leftwards shifts to prevent moving left of pattern start
- auto shiftAmount = (leftMostPos > -amount) ? amount : -leftMostPos;
- if (shiftAmount == 0) { return; }
- for (Note *note : notes) { note->setPos( note->pos() + shiftAmount ); }
- m_pattern->rearrangeAllNotes();
- m_pattern->updateLength();
- m_pattern->dataChanged();
- // we modified the song
- update();
- gui->songEditor()->update();
- }
- bool PianoRoll::isSelection() const // are any notes selected?
- {
- for( const Note *note : m_pattern->notes() )
- {
- if( note->selected() )
- {
- return true;
- }
- }
- return false;
- }
- int PianoRoll::selectionCount() const // how many notes are selected?
- {
- return getSelectedNotes().size();
- }
- void PianoRoll::keyPressEvent(QKeyEvent* ke)
- {
- if(m_stepRecorder.isRecording())
- {
- bool handled = m_stepRecorder.keyPressEvent(ke);
- if(handled)
- {
- ke->accept();
- update();
- return;
- }
- }
- if( hasValidPattern() && ke->modifiers() == Qt::NoModifier )
- {
- const int key_num = PianoView::getKeyFromKeyEvent( ke ) + ( DefaultOctave - 1 ) * KeysPerOctave;
- if (!ke->isAutoRepeat() && key_num > -1)
- {
- m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(key_num);
- // if a chord is set, play all chord notes (simulate click on all):
- playChordNotes(key_num);
- ke->accept();
- }
- }
- switch( ke->key() )
- {
- case Qt::Key_Up:
- case Qt::Key_Down:
- {
- int direction = (ke->key() == Qt::Key_Up ? +1 : -1);
- if( ( ke->modifiers() & Qt::ControlModifier ) && m_action == ActionNone )
- {
- // shift selection up an octave
- // if nothing selected, shift _everything_
- if (hasValidPattern())
- {
- shiftSemiTone( 12 * direction );
- }
- }
- else if((ke->modifiers() & Qt::ShiftModifier) && m_action == ActionNone)
- {
- // Move selected notes up by one semitone
- if (hasValidPattern())
- {
- shiftSemiTone( 1 * direction );
- }
- }
- else
- {
- // scroll
- m_topBottomScroll->setValue( m_topBottomScroll->value() -
- cm_scrollAmtVert * direction );
- // if they are moving notes around or resizing,
- // recalculate the note/resize position
- if( m_action == ActionMoveNote ||
- m_action == ActionResizeNote )
- {
- dragNotes( m_lastMouseX, m_lastMouseY,
- ke->modifiers() & Qt::AltModifier,
- ke->modifiers() & Qt::ShiftModifier,
- ke->modifiers() & Qt::ControlModifier );
- }
- }
- ke->accept();
- break;
- }
- case Qt::Key_Right:
- case Qt::Key_Left:
- {
- int direction = (ke->key() == Qt::Key_Right ? +1 : -1);
- if( ke->modifiers() & Qt::ControlModifier && m_action == ActionNone )
- {
- // Move selected notes by one bar to the left
- if (hasValidPattern())
- {
- shiftPos( direction * TimePos::ticksPerBar() );
- }
- }
- else if( ke->modifiers() & Qt::ShiftModifier && m_action == ActionNone)
- {
- // move notes
- if (hasValidPattern())
- {
- bool quantized = ! ( ke->modifiers() & Qt::AltModifier );
- int amt = quantized ? quantization() : 1;
- shiftPos( direction * amt );
- }
- }
- else if( ke->modifiers() & Qt::AltModifier)
- {
- // switch to editing a pattern adjacent to this one in the song editor
- if (hasValidPattern())
- {
- Pattern * p = direction > 0 ? m_pattern->nextPattern()
- : m_pattern->previousPattern();
- if(p != NULL)
- {
- setCurrentPattern(p);
- }
- }
- }
- else
- {
- // scroll
- m_leftRightScroll->setValue( m_leftRightScroll->value() +
- direction * cm_scrollAmtHoriz );
- // if they are moving notes around or resizing,
- // recalculate the note/resize position
- if( m_action == ActionMoveNote ||
- m_action == ActionResizeNote )
- {
- dragNotes( m_lastMouseX, m_lastMouseY,
- ke->modifiers() & Qt::AltModifier,
- ke->modifiers() & Qt::ShiftModifier,
- ke->modifiers() & Qt::ControlModifier );
- }
- }
- ke->accept();
- break;
- }
- case Qt::Key_A:
- if( ke->modifiers() & Qt::ControlModifier )
- {
- ke->accept();
- if (ke->modifiers() & Qt::ShiftModifier)
- {
- // Ctrl + Shift + A = deselect all notes
- clearSelectedNotes();
- }
- else
- {
- // Ctrl + A = select all notes
- selectAll();
- }
- update();
- }
- break;
- case Qt::Key_Escape:
- // On the Razor mode, ESC cancels it
- if (m_editMode == ModeEditRazor)
- {
- cancelRazorAction();
- }
- else
- {
- // Same as Ctrl + Shift + A
- clearSelectedNotes();
- }
- break;
- case Qt::Key_Backspace:
- case Qt::Key_Delete:
- deleteSelectedNotes();
- ke->accept();
- break;
- case Qt::Key_Home:
- m_timeLine->pos().setTicks( 0 );
- m_timeLine->updatePosition();
- ke->accept();
- break;
- case Qt::Key_0:
- case Qt::Key_1:
- case Qt::Key_2:
- case Qt::Key_3:
- case Qt::Key_4:
- case Qt::Key_5:
- case Qt::Key_6:
- case Qt::Key_7:
- case Qt::Key_8:
- case Qt::Key_9:
- {
- int len = 1 + ke->key() - Qt::Key_0;
- if( len == 10 )
- {
- len = 0;
- }
- if( ke->modifiers() & ( Qt::ControlModifier | Qt::KeypadModifier ) )
- {
- m_noteLenModel.setValue( len );
- ke->accept();
- }
- else if( ke->modifiers() & Qt::AltModifier )
- {
- m_quantizeModel.setValue( len );
- ke->accept();
- }
- break;
- }
- case Qt::Key_Control:
- // Ctrl will not enter selection mode if we are
- // in Razor mode, but unquantize it
- if (m_editMode == ModeEditRazor)
- {
- break;
- }
- // Enter selection mode if:
- // -> this window is active
- // -> shift is not pressed
- // (<S-C-drag> is shortcut for sticky note resize)
- if ( !( ke->modifiers() & Qt::ShiftModifier ) && isActiveWindow() )
- {
- m_ctrlMode = m_editMode;
- m_editMode = ModeSelect;
- setCursor( Qt::ArrowCursor );
- ke->accept();
- }
- break;
- default:
- break;
- }
- update();
- }
- void PianoRoll::keyReleaseEvent(QKeyEvent* ke )
- {
- if( hasValidPattern() && ke->modifiers() == Qt::NoModifier )
- {
- const int key_num = PianoView::getKeyFromKeyEvent( ke ) + ( DefaultOctave - 1 ) * KeysPerOctave;
- if (!ke->isAutoRepeat() && key_num > -1)
- {
- m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease(key_num);
- // if a chord is set, simulate click release on all chord notes
- pauseChordNotes(key_num);
- ke->accept();
- }
- }
- switch( ke->key() )
- {
- case Qt::Key_Control:
- if (m_editMode == ModeEditRazor)
- {
- break;
- }
- computeSelectedNotes( ke->modifiers() & Qt::ShiftModifier);
- m_editMode = m_ctrlMode;
- update();
- break;
- case Qt::Key_Shift:
- if (m_editMode == ModeEditRazor && !m_firstRazorSplit)
- {
- cancelRazorAction();
- }
- // update after undo/redo
- case Qt::Key_Z:
- case Qt::Key_R:
- if( hasValidPattern() && ke->modifiers() == Qt::ControlModifier )
- {
- update();
- }
- break;
- }
- update();
- }
- void PianoRoll::leaveEvent(QEvent * e )
- {
- QWidget::leaveEvent( e );
- s_textFloat->hide();
- update(); // cleaning inner mouse-related graphics
- }
- int PianoRoll::noteEditTop() const
- {
- return keyAreaBottom() + NOTE_EDIT_RESIZE_BAR;
- }
- int PianoRoll::noteEditBottom() const
- {
- return height() - PR_BOTTOM_MARGIN;
- }
- int PianoRoll::noteEditRight() const
- {
- return width() - PR_RIGHT_MARGIN;
- }
- int PianoRoll::noteEditLeft() const
- {
- return m_whiteKeyWidth;
- }
- int PianoRoll::keyAreaTop() const
- {
- return PR_TOP_MARGIN;
- }
- int PianoRoll::keyAreaBottom() const
- {
- return height() - PR_BOTTOM_MARGIN - m_notesEditHeight;
- }
- void PianoRoll::mousePressEvent(QMouseEvent * me )
- {
- m_startedWithShift = me->modifiers() & Qt::ShiftModifier;
- if( ! hasValidPattern() )
- {
- return;
- }
- // -- Razor
- if (m_editMode == ModeEditRazor && me->button() == Qt::LeftButton)
- {
- NoteVector n;
- Note* note = noteUnderMouse();
- if (note)
- {
- n.append(note);
- updateRazorPos(me);
- // Call splitNotes for the note
- m_pattern->splitNotes(n, TimePos(m_razorTickPos));
- // Allow cancel razor mode when shift is released (if hold down).
- m_firstRazorSplit = false;
- }
- // Keep in razor mode while SHIFT is hold during cut
- if (!(me->modifiers() & Qt::ShiftModifier))
- {
- cancelRazorAction();
- }
- update();
- return;
- }
- if( m_editMode == ModeEditDetuning && noteUnderMouse() )
- {
- static QPointer<AutomationPattern> detuningPattern = nullptr;
- if (detuningPattern.data() != nullptr)
- {
- detuningPattern->disconnect(this);
- }
- Note* n = noteUnderMouse();
- if (n->detuning() == nullptr)
- {
- n->createDetuning();
- }
- detuningPattern = n->detuning()->automationPattern();
- connect(detuningPattern.data(), SIGNAL(dataChanged()), this, SLOT(update()));
- gui->automationEditor()->open(detuningPattern);
- return;
- }
- // if holding control, go to selection mode unless shift is also pressed
- if( me->modifiers() & Qt::ControlModifier && m_editMode != ModeSelect )
- {
- m_ctrlMode = m_editMode;
- m_editMode = ModeSelect;
- setCursor( Qt::ArrowCursor );
- update();
- }
- // keep track of the point where the user clicked down
- if( me->button() == Qt::LeftButton )
- {
- m_moveStartX = me->x();
- m_moveStartY = me->y();
- }
- if(me->button() == Qt::LeftButton &&
- me->y() > keyAreaBottom() && me->y() < noteEditTop())
- {
- // resizing the note edit area
- m_action = ActionResizeNoteEditArea;
- return;
- }
- if( me->y() > PR_TOP_MARGIN )
- {
- bool edit_note = ( me->y() > noteEditTop() );
- int key_num = getKey( me->y() );
- int x = me->x();
- if (x > m_whiteKeyWidth)
- {
- // set, move or resize note
- x -= m_whiteKeyWidth;
- // get tick in which the user clicked
- int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
- m_currentPosition;
- // get note-vector of current pattern
- const NoteVector & notes = m_pattern->notes();
- // will be our iterator in the following loop
- NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
- // loop through whole note-vector...
- for( int i = 0; i < notes.size(); ++i )
- {
- Note *note = *it;
- TimePos len = note->length();
- if( len < 0 )
- {
- len = 4;
- }
- // and check whether the user clicked on an
- // existing note or an edit-line
- if( pos_ticks >= note->pos() &&
- len > 0 &&
- (
- ( ! edit_note &&
- pos_ticks <= note->pos() + len &&
- note->key() == key_num )
- ||
- ( edit_note &&
- pos_ticks <= note->pos() +
- NOTE_EDIT_LINE_WIDTH * TimePos::ticksPerBar() / m_ppb )
- )
- )
- {
- break;
- }
- --it;
- }
- // first check whether the user clicked in note-edit-
- // area
- if( edit_note )
- {
- m_pattern->addJournalCheckPoint();
- // scribble note edit changes
- mouseMoveEvent( me );
- return;
- }
- // left button??
- else if( me->button() == Qt::LeftButton &&
- m_editMode == ModeDraw )
- {
- // whether this action creates new note(s) or not
- bool is_new_note = false;
- Note * created_new_note = NULL;
- // did it reach end of vector because
- // there's no note??
- if( it == notes.begin()-1 )
- {
- is_new_note = true;
- m_pattern->addJournalCheckPoint();
- // then set new note
- // clear selection and select this new note
- clearSelectedNotes();
- // +32 to quanitize the note correctly when placing notes with
- // the mouse. We do this here instead of in note.quantized
- // because live notes should still be quantized at the half.
- TimePos note_pos( pos_ticks - ( quantization() / 2 ) );
- TimePos note_len( newNoteLen() );
- Note new_note( note_len, note_pos, key_num );
- new_note.setSelected( true );
- new_note.setPanning( m_lastNotePanning );
- new_note.setVolume( m_lastNoteVolume );
- created_new_note = m_pattern->addNote( new_note );
- const InstrumentFunctionNoteStacking::Chord & chord = InstrumentFunctionNoteStacking::ChordTable::getInstance()
- .getChordByName( m_chordModel.currentText() );
- if( ! chord.isEmpty() )
- {
- // if a chord is selected, create following notes in chord
- // or arpeggio mode
- const bool arpeggio = me->modifiers() & Qt::ShiftModifier;
- for( int i = 1; i < chord.size(); i++ )
- {
- if( arpeggio )
- {
- note_pos += note_len;
- }
- Note new_note( note_len, note_pos, key_num + chord[i] );
- new_note.setSelected( true );
- new_note.setPanning( m_lastNotePanning );
- new_note.setVolume( m_lastNoteVolume );
- m_pattern->addNote( new_note );
- }
- }
- // reset it so that it can be used for
- // ops (move, resize) after this
- // code-block
- it = notes.begin();
- while( it != notes.end() && *it != created_new_note )
- {
- ++it;
- }
- }
- Note *current_note = *it;
- m_currentNote = current_note;
- m_lastNotePanning = current_note->getPanning();
- m_lastNoteVolume = current_note->getVolume();
- m_lenOfNewNotes = current_note->length();
- // remember which key and tick we started with
- m_mouseDownKey = m_startKey;
- m_mouseDownTick = m_currentPosition;
- //If clicked on an unselected note, remove selection and select that new note
- if (!m_currentNote->selected())
- {
- clearSelectedNotes();
- m_currentNote->setSelected( true );
- }
- auto selectedNotes = getSelectedNotes();
- m_moveBoundaryLeft = selectedNotes.first()->pos().getTicks();
- m_moveBoundaryRight = selectedNotes.first()->endPos();
- m_moveBoundaryBottom = selectedNotes.first()->key();
- m_moveBoundaryTop = m_moveBoundaryBottom;
- //Figure out the bounding box of all the selected notes
- for (Note *note: selectedNotes)
- {
- // remember note starting positions
- note->setOldKey( note->key() );
- note->setOldPos( note->pos() );
- note->setOldLength( note->length() );
- m_moveBoundaryLeft = qMin(note->pos().getTicks(), (tick_t) m_moveBoundaryLeft);
- m_moveBoundaryRight = qMax((int) note->endPos(), m_moveBoundaryRight);
- m_moveBoundaryBottom = qMin(note->key(), m_moveBoundaryBottom);
- m_moveBoundaryTop = qMax(note->key(), m_moveBoundaryTop);
- }
- // clicked at the "tail" of the note?
- if( pos_ticks * m_ppb / TimePos::ticksPerBar() >
- m_currentNote->endPos() * m_ppb / TimePos::ticksPerBar() - RESIZE_AREA_WIDTH
- && m_currentNote->length() > 0 )
- {
- m_pattern->addJournalCheckPoint();
- // then resize the note
- m_action = ActionResizeNote;
- //Calculate the minimum length we should allow when resizing
- //each note, and let all notes use the smallest one found
- m_minResizeLen = quantization();
- for (Note *note : selectedNotes)
- {
- //Notes from the BB editor can have a negative length, so
- //change their length to the displayed one before resizing
- if (note->oldLength() <= 0) { note->setOldLength(4); }
- //Let the note be sized down by quantized increments, stopping
- //when the next step down would result in a negative length
- int thisMin = note->oldLength() % quantization();
- //The initial value for m_minResizeLen is the minimum length of
- //a note divisible by the current Q. Therefore we ignore notes
- //where thisMin == 0 when checking for a new minimum
- if (thisMin > 0 && thisMin < m_minResizeLen) { m_minResizeLen = thisMin; }
- }
- // set resize-cursor
- setCursor( Qt::SizeHorCursor );
- }
- else
- {
- if( ! created_new_note )
- {
- m_pattern->addJournalCheckPoint();
- }
- // otherwise move it
- m_action = ActionMoveNote;
- // set move-cursor
- setCursor( Qt::SizeAllCursor );
- // if they're holding shift, copy all selected notes
- if( ! is_new_note && me->modifiers() & Qt::ShiftModifier )
- {
- for (Note *note: selectedNotes)
- {
- Note *newNote = m_pattern->addNote(*note, false);
- newNote->setSelected(false);
- }
- if (!selectedNotes.empty())
- {
- // added new notes, so must update engine, song, etc
- Engine::getSong()->setModified();
- update();
- gui->songEditor()->update();
- }
- }
- // play the note
- testPlayNote( m_currentNote );
- }
- Engine::getSong()->setModified();
- }
- else if( ( me->buttons() == Qt::RightButton &&
- m_editMode == ModeDraw ) ||
- m_editMode == ModeErase )
- {
- // erase single note
- m_mouseDownRight = true;
- if( it != notes.begin()-1 )
- {
- m_pattern->addJournalCheckPoint();
- m_pattern->removeNote( *it );
- Engine::getSong()->setModified();
- }
- }
- else if( me->button() == Qt::LeftButton &&
- m_editMode == ModeSelect )
- {
- // select an area of notes
- m_selectStartTick = pos_ticks;
- m_selectedTick = 0;
- m_selectStartKey = key_num;
- m_selectedKeys = 1;
- m_action = ActionSelectNotes;
- // call mousemove to fix glitch where selection
- // appears in wrong spot on mousedown
- mouseMoveEvent( me );
- }
- update();
- }
- else if( me->y() < keyAreaBottom() )
- {
- // reference to last key needed for both
- // right click (used for copy all keys on note)
- // and for playing the key when left-clicked
- m_lastKey = key_num;
- // clicked on keyboard on the left
- if( me->buttons() == Qt::RightButton )
- {
- // right click - tone marker contextual menu
- m_pianoKeySelected = getKey( me->y() );
- m_semiToneMarkerMenu->popup( mapToGlobal( QPoint( me->x(), me->y() ) ) );
- }
- else if( me->buttons() == Qt::LeftButton )
- {
- // left click - play the note
- int v = ((float) x) / ((float) m_whiteKeyWidth) * MidiDefaultVelocity;
- m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(key_num, v);
- // if a chord is set, play the chords notes as well:
- playChordNotes(key_num, v);
- }
- }
- else
- {
- if( me->buttons() == Qt::LeftButton )
- {
- // clicked in the box below the keys to the left of note edit area
- m_noteEditMode = (NoteEditMode)(((int)m_noteEditMode)+1);
- if( m_noteEditMode == NoteEditCount )
- {
- m_noteEditMode = (NoteEditMode) 0;
- }
- repaint();
- }
- else if( me->buttons() == Qt::RightButton )
- {
- // pop menu asking which one they want to edit
- m_noteEditMenu->popup( mapToGlobal( QPoint( me->x(), me->y() ) ) );
- }
- }
- }
- }
- void PianoRoll::mouseDoubleClickEvent(QMouseEvent * me )
- {
- if( ! hasValidPattern() )
- {
- return;
- }
- // if they clicked in the note edit area, enter value for the volume bar
- if( me->x() > noteEditLeft() && me->x() < noteEditRight()
- && me->y() > noteEditTop() && me->y() < noteEditBottom() )
- {
- // get values for going through notes
- int pixel_range = 4;
- int x = me->x() - m_whiteKeyWidth;
- const int ticks_start = ( x-pixel_range/2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- const int ticks_end = ( x+pixel_range/2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- const int ticks_middle = x * TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- // go through notes to figure out which one we want to change
- bool altPressed = me->modifiers() & Qt::AltModifier;
- NoteVector nv;
- for ( Note * i : m_pattern->notes() )
- {
- if( i->withinRange( ticks_start, ticks_end ) || ( i->selected() && !altPressed ) )
- {
- nv += i;
- }
- }
- // make sure we're on a note
- if( nv.size() > 0 )
- {
- const Note * closest = NULL;
- int closest_dist = 9999999;
- // if we caught multiple notes and we're not editing a
- // selection, find the closest...
- if( nv.size() > 1 && !isSelection() )
- {
- for ( const Note * i : nv )
- {
- const int dist = qAbs( i->pos().getTicks() - ticks_middle );
- if( dist < closest_dist ) { closest = i; closest_dist = dist; }
- }
- // ... then remove all notes from the vector that aren't on the same exact time
- NoteVector::Iterator it = nv.begin();
- while( it != nv.end() )
- {
- const Note *note = *it;
- if( note->pos().getTicks() != closest->pos().getTicks() )
- {
- it = nv.erase( it );
- }
- else
- {
- it++;
- }
- }
- }
- enterValue( &nv );
- }
- }
- else
- {
- QWidget::mouseDoubleClickEvent(me);
- }
- }
- void PianoRoll::testPlayNote( Note * n )
- {
- m_lastKey = n->key();
- if( ! n->isPlaying() && ! m_recording && ! m_stepRecorder.isRecording())
- {
- n->setIsPlaying( true );
- const int baseVelocity = m_pattern->instrumentTrack()->midiPort()->baseVelocity();
- m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(n->key(), n->midiVelocity(baseVelocity));
- // if a chord is set, play the chords notes as well:
- playChordNotes(n->key(), n->midiVelocity(baseVelocity));
- MidiEvent event( MidiMetaEvent, -1, n->key(), panningToMidi( n->getPanning() ) );
- event.setMetaEvent( MidiNotePanning );
- m_pattern->instrumentTrack()->processInEvent( event, 0 );
- }
- }
- void PianoRoll::pauseTestNotes( bool pause )
- {
- for (Note *note : m_pattern->notes())
- {
- if( note->isPlaying() )
- {
- if( pause )
- {
- // stop note
- m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( note->key() );
- // if a chord was set, stop the chords notes as well:
- pauseChordNotes(note->key());
- }
- else
- {
- // start note
- note->setIsPlaying( false );
- testPlayNote( note );
- }
- }
- }
- }
- void PianoRoll::playChordNotes(int key, int velocity)
- {
- // if a chord is set, play the chords notes beside the base note.
- Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
- const InstrumentFunctionNoteStacking::Chord & chord =
- InstrumentFunctionNoteStacking::ChordTable::getInstance().getChordByName(
- m_chordModel.currentText());
- if (!chord.isEmpty())
- {
- for (int i = 1; i < chord.size(); ++i)
- {
- pianoModel->handleKeyPress(key + chord[i], velocity);
- }
- }
- }
- void PianoRoll::pauseChordNotes(int key)
- {
- // if a chord was set, stop the chords notes beside the base note.
- Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
- const InstrumentFunctionNoteStacking::Chord & chord =
- InstrumentFunctionNoteStacking::ChordTable::getInstance().getChordByName(
- m_chordModel.currentText());
- if (!chord.isEmpty())
- {
- for (int i = 1; i < chord.size(); ++i)
- {
- pianoModel->handleKeyRelease(key + chord[i]);
- }
- }
- }
- void PianoRoll::setRazorAction()
- {
- if (m_editMode != ModeEditRazor)
- {
- m_firstRazorSplit = true;
- m_razorMode = m_editMode;
- m_editMode = ModeEditRazor;
- m_action = ActionRazor;
- setCursor(Qt::ArrowCursor);
- update();
- }
- }
- void PianoRoll::cancelRazorAction()
- {
- m_editMode = m_razorMode;
- m_action = ActionNone;
- update();
- }
- void PianoRoll::testPlayKey( int key, int velocity, int pan )
- {
- Piano *pianoModel = m_pattern->instrumentTrack()->pianoModel();
- // turn off old key
- pianoModel->handleKeyRelease( m_lastKey );
- // if a chord was set, stop the chords notes as well
- pauseChordNotes(m_lastKey);
- // remember which one we're playing
- m_lastKey = key;
- // play new key
- pianoModel->handleKeyPress( key, velocity );
- // and if a chord is set, play chord notes:
- playChordNotes(key, velocity);
- }
- void PianoRoll::computeSelectedNotes(bool shift)
- {
- if( m_selectStartTick == 0 &&
- m_selectedTick == 0 &&
- m_selectStartKey == 0 &&
- m_selectedKeys == 0 )
- {
- // don't bother, there's no selection
- return;
- }
- // setup selection-vars
- int sel_pos_start = m_selectStartTick;
- int sel_pos_end = m_selectStartTick+m_selectedTick;
- if( sel_pos_start > sel_pos_end )
- {
- qSwap<int>( sel_pos_start, sel_pos_end );
- }
- int sel_key_start = m_selectStartKey - m_startKey + 1;
- int sel_key_end = sel_key_start + m_selectedKeys;
- if( sel_key_start > sel_key_end )
- {
- qSwap<int>( sel_key_start, sel_key_end );
- }
- //int y_base = noteEditTop() - 1;
- if( hasValidPattern() )
- {
- for( Note *note : m_pattern->notes() )
- {
- // make a new selection unless they're holding shift
- if( ! shift )
- {
- note->setSelected( false );
- }
- int len_ticks = note->length();
- if( len_ticks == 0 )
- {
- continue;
- }
- else if( len_ticks < 0 )
- {
- len_ticks = 4;
- }
- const int key = note->key() - m_startKey + 1;
- int pos_ticks = note->pos();
- // if the selection even barely overlaps the note
- if( key > sel_key_start &&
- key <= sel_key_end &&
- pos_ticks + len_ticks > sel_pos_start &&
- pos_ticks < sel_pos_end )
- {
- // remove from selection when holding shift
- bool selected = shift && note->selected();
- note->setSelected( ! selected);
- }
- }
- }
- removeSelection();
- update();
- }
- void PianoRoll::mouseReleaseEvent( QMouseEvent * me )
- {
- bool mustRepaint = false;
- s_textFloat->hide();
- // Quit razor mode if we pressed and released the right mouse button
- if (m_editMode == ModeEditRazor && me->button() == Qt::RightButton)
- {
- cancelRazorAction();
- }
- if( me->button() & Qt::LeftButton )
- {
- mustRepaint = true;
- if( m_action == ActionSelectNotes && m_editMode == ModeSelect )
- {
- // select the notes within the selection rectangle and
- // then destroy the selection rectangle
- computeSelectedNotes(
- me->modifiers() & Qt::ShiftModifier );
- }
- else if( m_action == ActionMoveNote )
- {
- // we moved one or more notes so they have to be
- // moved properly according to new starting-
- // time in the note-array of pattern
- m_pattern->rearrangeAllNotes();
- }
- if( m_action == ActionMoveNote || m_action == ActionResizeNote )
- {
- // if we only moved one note, deselect it so we can
- // edit the notes in the note edit area
- if( selectionCount() == 1 )
- {
- clearSelectedNotes();
- }
- }
- }
- if( me->button() & Qt::RightButton )
- {
- m_mouseDownRight = false;
- mustRepaint = true;
- }
- if( hasValidPattern() )
- {
- // turn off all notes that are playing
- for ( Note *note : m_pattern->notes() )
- {
- if( note->isPlaying() )
- {
- m_pattern->instrumentTrack()->pianoModel()->
- handleKeyRelease( note->key() );
- pauseChordNotes(note->key());
- note->setIsPlaying( false );
- }
- }
- // stop playing keys that we let go of
- m_pattern->instrumentTrack()->pianoModel()->
- handleKeyRelease( m_lastKey );
- pauseChordNotes(m_lastKey);
- }
- m_currentNote = NULL;
- if (m_action != ActionRazor)
- {
- m_action = ActionNone;
- }
- if( m_editMode == ModeDraw )
- {
- setCursor( Qt::ArrowCursor );
- }
- if( mustRepaint )
- {
- repaint();
- }
- }
- void PianoRoll::mouseMoveEvent( QMouseEvent * me )
- {
- if( ! hasValidPattern() )
- {
- update();
- return;
- }
- if( m_action == ActionNone && me->buttons() == 0 )
- {
- // When cursor is between note editing area and volume/panning
- // area show vertical size cursor.
- if( me->y() > keyAreaBottom() && me->y() < noteEditTop() )
- {
- setCursor( Qt::SizeVerCursor );
- return;
- }
- }
- else if( m_action == ActionResizeNoteEditArea )
- {
- // Don't try to show more keys than the full keyboard, bail if trying to
- if (m_pianoKeysVisible == NumKeys && me->y() > m_moveStartY)
- {
- return;
- }
- int newHeight = height() - me->y();
- if (me->y() < KEY_AREA_MIN_HEIGHT)
- {
- newHeight = height() - KEY_AREA_MIN_HEIGHT -
- PR_TOP_MARGIN - PR_BOTTOM_MARGIN; // - NOTE_EDIT_RESIZE_BAR
- }
- // change m_notesEditHeight and then repaint
- m_notesEditHeight = qMax(NOTE_EDIT_MIN_HEIGHT, newHeight);
- m_userSetNotesEditHeight = m_notesEditHeight;
- m_stepRecorderWidget.setBottomMargin(PR_BOTTOM_MARGIN + m_notesEditHeight);
- updateScrollbars();
- updatePositionLineHeight();
- repaint();
- return;
- }
- // Update Razor position if we are on Razor mode
- if (m_editMode == ModeEditRazor)
- {
- updateRazorPos(me);
- }
- if( me->y() > PR_TOP_MARGIN || m_action != ActionNone )
- {
- bool edit_note = ( me->y() > noteEditTop() )
- && m_action != ActionSelectNotes;
- int key_num = getKey( me->y() );
- int x = me->x();
- // see if they clicked on the keyboard on the left
- if (x < m_whiteKeyWidth && m_action == ActionNone
- && ! edit_note && key_num != m_lastKey
- && me->buttons() & Qt::LeftButton )
- {
- // clicked on a key, play the note
- testPlayKey(key_num, ((float) x) / ((float) m_whiteKeyWidth) * MidiDefaultVelocity, 0);
- update();
- return;
- }
- x -= m_whiteKeyWidth;
- if( me->buttons() & Qt::LeftButton
- && m_editMode == ModeDraw
- && (m_action == ActionMoveNote || m_action == ActionResizeNote ) )
- {
- // handle moving notes and resizing them
- bool replay_note = key_num != m_lastKey
- && m_action == ActionMoveNote;
- if( replay_note || ( m_action == ActionMoveNote && ( me->modifiers() & Qt::ShiftModifier ) && ! m_startedWithShift ) )
- {
- pauseTestNotes();
- }
- dragNotes( me->x(), me->y(),
- me->modifiers() & Qt::AltModifier,
- me->modifiers() & Qt::ShiftModifier,
- me->modifiers() & Qt::ControlModifier );
- if( replay_note && m_action == ActionMoveNote && ! ( ( me->modifiers() & Qt::ShiftModifier ) && ! m_startedWithShift ) )
- {
- pauseTestNotes( false );
- }
- }
- else if( m_editMode != ModeErase &&
- ( edit_note || m_action == ActionChangeNoteProperty ) &&
- ( me->buttons() & Qt::LeftButton || me->buttons() & Qt::MiddleButton
- || ( me->buttons() & Qt::RightButton && me->modifiers() & Qt::ShiftModifier ) ) )
- {
- // editing note properties
- // Change notes within a certain pixel range of where
- // the mouse cursor is
- int pixel_range = 14;
- // convert to ticks so that we can check which notes
- // are in the range
- int ticks_start = ( x-pixel_range/2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- int ticks_end = ( x+pixel_range/2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- // get note-vector of current pattern
- const NoteVector & notes = m_pattern->notes();
- // determine what volume/panning to set note to
- // if middle-click, set to defaults
- volume_t vol = DefaultVolume;
- panning_t pan = DefaultPanning;
- if( me->buttons() & Qt::LeftButton )
- {
- vol = qBound<int>( MinVolume,
- MinVolume +
- ( ( (float)noteEditBottom() ) - ( (float)me->y() ) ) /
- ( (float)( noteEditBottom() - noteEditTop() ) ) *
- ( MaxVolume - MinVolume ),
- MaxVolume );
- pan = qBound<int>( PanningLeft,
- PanningLeft +
- ( (float)( noteEditBottom() - me->y() ) ) /
- ( (float)( noteEditBottom() - noteEditTop() ) ) *
- ( (float)( PanningRight - PanningLeft ) ),
- PanningRight);
- }
- if( m_noteEditMode == NoteEditVolume )
- {
- m_lastNoteVolume = vol;
- showVolTextFloat( vol, me->pos() );
- }
- else if( m_noteEditMode == NoteEditPanning )
- {
- m_lastNotePanning = pan;
- showPanTextFloat( pan, me->pos() );
- }
- // When alt is pressed we only edit the note under the cursor
- bool altPressed = me->modifiers() & Qt::AltModifier;
- // We iterate from last note in pattern to the first,
- // chronologically
- NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
- for( int i = 0; i < notes.size(); ++i )
- {
- Note* n = *it;
- bool isUnderPosition = n->withinRange( ticks_start, ticks_end );
- // Play note under the cursor
- if ( isUnderPosition ) { testPlayNote( n ); }
- // If note is:
- // Under the cursor, when there is no selection
- // Selected, and alt is not pressed
- // Under the cursor, selected, and alt is pressed
- if ( ( isUnderPosition && !isSelection() ) ||
- ( n->selected() && !altPressed ) ||
- ( isUnderPosition && n->selected() && altPressed )
- )
- {
- if( m_noteEditMode == NoteEditVolume )
- {
- n->setVolume( vol );
- const int baseVelocity = m_pattern->instrumentTrack()->midiPort()->baseVelocity();
- m_pattern->instrumentTrack()->processInEvent( MidiEvent( MidiKeyPressure, -1, n->key(), n->midiVelocity( baseVelocity ) ) );
- }
- else if( m_noteEditMode == NoteEditPanning )
- {
- n->setPanning( pan );
- MidiEvent evt( MidiMetaEvent, -1, n->key(), panningToMidi( pan ) );
- evt.setMetaEvent( MidiNotePanning );
- m_pattern->instrumentTrack()->processInEvent( evt );
- }
- }
- else if( n->isPlaying() && !isSelection() )
- {
- // mouse not over this note, stop playing it.
- m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( n->key() );
- pauseChordNotes(n->key());
- n->setIsPlaying( false );
- }
- --it;
- }
- // Emit pattern has changed
- m_pattern->dataChanged();
- }
- else if( me->buttons() == Qt::NoButton && m_editMode == ModeDraw )
- {
- // set move- or resize-cursor
- // get tick in which the cursor is posated
- int pos_ticks = ( x * TimePos::ticksPerBar() ) /
- m_ppb + m_currentPosition;
- // get note-vector of current pattern
- const NoteVector & notes = m_pattern->notes();
- // will be our iterator in the following loop
- NoteVector::ConstIterator it = notes.begin()+notes.size()-1;
- // loop through whole note-vector...
- for( int i = 0; i < notes.size(); ++i )
- {
- Note *note = *it;
- // and check whether the cursor is over an
- // existing note
- if( pos_ticks >= note->pos() &&
- pos_ticks <= note->pos() +
- note->length() &&
- note->key() == key_num &&
- note->length() > 0 )
- {
- break;
- }
- --it;
- }
- // did it reach end of vector because there's
- // no note??
- if( it != notes.begin()-1 )
- {
- Note *note = *it;
- // x coordinate of the right edge of the note
- int noteRightX = ( note->pos() + note->length() -
- m_currentPosition) * m_ppb/TimePos::ticksPerBar();
- // cursor at the "tail" of the note?
- bool atTail = note->length() > 0 && x > noteRightX -
- RESIZE_AREA_WIDTH;
- Qt::CursorShape cursorShape = atTail ? Qt::SizeHorCursor :
- Qt::SizeAllCursor;
- setCursor( cursorShape );
- }
- else
- {
- // the cursor is over no note, so restore cursor
- setCursor( Qt::ArrowCursor );
- }
- }
- else if( me->buttons() & Qt::LeftButton &&
- m_editMode == ModeSelect &&
- m_action == ActionSelectNotes )
- {
- // change size of selection
- // get tick in which the cursor is posated
- int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
- m_currentPosition;
- m_selectedTick = pos_ticks - m_selectStartTick;
- if( (int) m_selectStartTick + m_selectedTick < 0 )
- {
- m_selectedTick = -static_cast<int>(
- m_selectStartTick );
- }
- m_selectedKeys = key_num - m_selectStartKey;
- if( key_num <= m_selectStartKey )
- {
- --m_selectedKeys;
- }
- }
- else if( ( m_editMode == ModeDraw && me->buttons() & Qt::RightButton )
- || ( m_editMode == ModeErase && me->buttons() ) )
- {
- // holding down right-click to delete notes or holding down
- // any key if in erase mode
- // get tick in which the user clicked
- int pos_ticks = x * TimePos::ticksPerBar() / m_ppb +
- m_currentPosition;
- // get note-vector of current pattern
- const NoteVector & notes = m_pattern->notes();
- // will be our iterator in the following loop
- NoteVector::ConstIterator it = notes.begin();
- // loop through whole note-vector...
- while( it != notes.end() )
- {
- Note *note = *it;
- TimePos len = note->length();
- if( len < 0 )
- {
- len = 4;
- }
- // and check whether the user clicked on an
- // existing note or an edit-line
- if( pos_ticks >= note->pos() &&
- len > 0 &&
- (
- ( ! edit_note &&
- pos_ticks <= note->pos() + len &&
- note->key() == key_num )
- ||
- ( edit_note &&
- pos_ticks <= note->pos() +
- NOTE_EDIT_LINE_WIDTH *
- TimePos::ticksPerBar() /
- m_ppb )
- )
- )
- {
- // delete this note
- m_pattern->removeNote( note );
- Engine::getSong()->setModified();
- }
- else
- {
- ++it;
- }
- }
- }
- else if (me->buttons() == Qt::NoButton && m_editMode != ModeDraw && m_editMode != ModeEditRazor)
- {
- // Is needed to restore cursor when it previously was set to
- // Qt::SizeVerCursor (between keyAreaBottom and noteEditTop)
- setCursor( Qt::ArrowCursor );
- }
- }
- else
- {
- if( me->buttons() & Qt::LeftButton &&
- m_editMode == ModeSelect &&
- m_action == ActionSelectNotes )
- {
- int x = me->x() - m_whiteKeyWidth;
- if( x < 0 && m_currentPosition > 0 )
- {
- x = 0;
- QCursor::setPos( mapToGlobal( QPoint(
- m_whiteKeyWidth,
- me->y() ) ) );
- if( m_currentPosition >= 4 )
- {
- m_leftRightScroll->setValue(
- m_currentPosition - 4 );
- }
- else
- {
- m_leftRightScroll->setValue( 0 );
- }
- }
- else if (x > width() - m_whiteKeyWidth)
- {
- x = width() - m_whiteKeyWidth;
- QCursor::setPos( mapToGlobal( QPoint( width(),
- me->y() ) ) );
- m_leftRightScroll->setValue( m_currentPosition +
- 4 );
- }
- // get tick in which the cursor is posated
- int pos_ticks = x * TimePos::ticksPerBar()/ m_ppb +
- m_currentPosition;
- m_selectedTick = pos_ticks -
- m_selectStartTick;
- if( (int) m_selectStartTick + m_selectedTick <
- 0 )
- {
- m_selectedTick = -static_cast<int>(
- m_selectStartTick );
- }
- int key_num = getKey( me->y() );
- int visible_keys = ( height() - PR_TOP_MARGIN -
- PR_BOTTOM_MARGIN -
- m_notesEditHeight ) /
- m_keyLineHeight + 2;
- const int s_key = m_startKey - 1;
- if( key_num <= s_key )
- {
- QCursor::setPos( mapToGlobal( QPoint( me->x(),
- keyAreaBottom() ) ) );
- m_topBottomScroll->setValue(
- m_topBottomScroll->value() + 1 );
- key_num = s_key;
- }
- else if( key_num >= s_key + visible_keys )
- {
- QCursor::setPos( mapToGlobal( QPoint( me->x(),
- PR_TOP_MARGIN ) ) );
- m_topBottomScroll->setValue(
- m_topBottomScroll->value() - 1 );
- key_num = s_key + visible_keys;
- }
- m_selectedKeys = key_num - m_selectStartKey;
- if( key_num <= m_selectStartKey )
- {
- --m_selectedKeys;
- }
- }
- setCursor( Qt::ArrowCursor );
- }
- m_lastMouseX = me->x();
- m_lastMouseY = me->y();
- update();
- }
- void PianoRoll::updateRazorPos(QMouseEvent* me)
- {
- // Calculate the TimePos from the mouse
- int mouseViewportPos = me->x() - m_whiteKeyWidth;
- int mouseTickPos = mouseViewportPos / (m_ppb / TimePos::ticksPerBar()) + m_currentPosition;
- // If ctrl is not pressed, quantize the position
- if (!(me->modifiers() & Qt::ControlModifier))
- {
- mouseTickPos = floor(mouseTickPos / quantization()) * quantization();
- }
- m_razorTickPos = mouseTickPos;
- }
- void PianoRoll::dragNotes( int x, int y, bool alt, bool shift, bool ctrl )
- {
- // dragging one or more notes around
- // convert pixels to ticks and keys
- int off_x = x - m_moveStartX;
- int off_ticks = off_x * TimePos::ticksPerBar() / m_ppb;
- int off_key = getKey( y ) - getKey( m_moveStartY );
- // handle scroll changes while dragging
- off_ticks -= m_mouseDownTick - m_currentPosition;
- off_key -= m_mouseDownKey - m_startKey;
- // if they're not holding alt, quantize the offset
- if( ! alt )
- {
- off_ticks = floor( off_ticks / quantization() )
- * quantization();
- }
- // make sure notes won't go outside boundary conditions
- if( m_action == ActionMoveNote && ! ( shift && ! m_startedWithShift ) )
- {
- if( m_moveBoundaryLeft + off_ticks < 0 )
- {
- off_ticks -= (off_ticks + m_moveBoundaryLeft);
- }
- if( m_moveBoundaryTop + off_key > NumKeys )
- {
- off_key -= NumKeys - (m_moveBoundaryTop + off_key);
- }
- if( m_moveBoundaryBottom + off_key < 0 )
- {
- off_key -= (m_moveBoundaryBottom + off_key);
- }
- }
- // get note-vector of current pattern
- const NoteVector & notes = m_pattern->notes();
- if (m_action == ActionMoveNote)
- {
- for (Note *note : getSelectedNotes())
- {
- if (shift && ! m_startedWithShift)
- {
- // quick resize, toggled by holding shift after starting a note move, but not before
- int ticks_new = note->oldLength().getTicks() + off_ticks;
- if( ticks_new <= 0 )
- {
- ticks_new = 1;
- }
- note->setLength( TimePos( ticks_new ) );
- m_lenOfNewNotes = note->length();
- }
- else
- {
- // moving note
- int pos_ticks = note->oldPos().getTicks() + off_ticks;
- int key_num = note->oldKey() + off_key;
- // ticks can't be negative
- pos_ticks = qMax(0, pos_ticks);
- // upper/lower bound checks on key_num
- key_num = qMax(0, key_num);
- key_num = qMin(key_num, NumKeys);
- note->setPos( TimePos( pos_ticks ) );
- note->setKey( key_num );
- }
- }
- }
- else if (m_action == ActionResizeNote)
- {
- // When resizing notes:
- // If shift is not pressed, resize the selected notes but do not rearrange them
- // If shift is pressed we resize and rearrange only the selected notes
- // If shift + ctrl then we also rearrange all posterior notes (sticky)
- // If shift is pressed but only one note is selected, apply sticky
- auto selectedNotes = getSelectedNotes();
- if (shift)
- {
- // Algorithm:
- // Relative to the starting point of the left-most selected note,
- // all selected note start-points and *endpoints* (not length) should be scaled by a calculated factor.
- // This factor is such that the endpoint of the note whose handle is being dragged should lie under the cursor.
- // first, determine the start-point of the left-most selected note:
- int stretchStartTick = -1;
- for (const Note *note : selectedNotes)
- {
- if (stretchStartTick < 0 || note->oldPos().getTicks() < stretchStartTick)
- {
- stretchStartTick = note->oldPos().getTicks();
- }
- }
- // determine the ending tick of the right-most selected note
- const Note *posteriorNote = nullptr;
- for (const Note *note : selectedNotes)
- {
- if (posteriorNote == nullptr ||
- note->oldPos().getTicks() + note->oldLength().getTicks() >
- posteriorNote->oldPos().getTicks() + posteriorNote->oldLength().getTicks())
- {
- posteriorNote = note;
- }
- }
- int posteriorEndTick = posteriorNote->pos().getTicks() + posteriorNote->length().getTicks();
- // end-point of the note whose handle is being dragged:
- int stretchEndTick = m_currentNote->oldPos().getTicks() + m_currentNote->oldLength().getTicks();
- // Calculate factor by which to scale the start-point and end-point of all selected notes
- float scaleFactor = (float)(stretchEndTick - stretchStartTick + off_ticks) / qMax(1, stretchEndTick - stretchStartTick);
- scaleFactor = qMax(0.0f, scaleFactor);
- // process all selected notes & determine how much the endpoint of the right-most note was shifted
- int posteriorDeltaThisFrame = 0;
- for (Note *note : selectedNotes)
- {
- // scale relative start and end positions by scaleFactor
- int newStart = stretchStartTick + scaleFactor *
- (note->oldPos().getTicks() - stretchStartTick);
- int newEnd = stretchStartTick + scaleFactor *
- (note->oldPos().getTicks()+note->oldLength().getTicks() - stretchStartTick);
- // if not holding alt, quantize the offsets
- if (!alt)
- {
- // quantize start time
- int oldStart = note->oldPos().getTicks();
- int startDiff = newStart - oldStart;
- startDiff = floor(startDiff / quantization()) * quantization();
- newStart = oldStart + startDiff;
- // quantize end time
- int oldEnd = oldStart + note->oldLength().getTicks();
- int endDiff = newEnd - oldEnd;
- endDiff = floor(endDiff / quantization()) * quantization();
- newEnd = oldEnd + endDiff;
- }
- int newLength = qMax(1, newEnd-newStart);
- if (note == posteriorNote)
- {
- posteriorDeltaThisFrame = (newStart+newLength) -
- (note->pos().getTicks() + note->length().getTicks());
- }
- note->setLength( TimePos(newLength) );
- note->setPos( TimePos(newStart) );
- m_lenOfNewNotes = note->length();
- }
- if (ctrl || selectionCount() == 1)
- {
- // if holding ctrl or only one note is selected, reposition posterior notes
- for (Note *note : notes)
- {
- if (!note->selected() && note->pos().getTicks() >= posteriorEndTick)
- {
- int newStart = note->pos().getTicks() + posteriorDeltaThisFrame;
- note->setPos( TimePos(newStart) );
- }
- }
- }
- }
- else
- {
- // shift is not pressed; stretch length of selected notes but not their position
- int minLength = alt ? 1 : m_minResizeLen.getTicks();
- for (Note *note : selectedNotes)
- {
- int newLength = qMax(minLength, note->oldLength() + off_ticks);
- note->setLength(TimePos(newLength));
- m_lenOfNewNotes = note->length();
- }
- }
- }
- m_pattern->updateLength();
- m_pattern->dataChanged();
- Engine::getSong()->setModified();
- }
- void PianoRoll::paintEvent(QPaintEvent * pe )
- {
- bool drawNoteNames = ConfigManager::inst()->value( "ui", "printnotelabels").toInt();
- QStyleOption opt;
- opt.initFrom( this );
- QPainter p( this );
- style()->drawPrimitive( QStyle::PE_Widget, &opt, &p, this );
- QBrush bgColor = p.background();
- // fill with bg color
- p.fillRect( 0, 0, width(), height(), bgColor );
- // set font-size to 80% of key line height
- QFont f = p.font();
- f.setPixelSize(m_keyLineHeight * 0.8);
- p.setFont(f); // font size doesn't change without this for some reason
- QFontMetrics fontMetrics(p.font());
- // G4 is one of the widest
- QRect const boundingRect = fontMetrics.boundingRect(QString("G4"));
- // Order of drawing
- // - vertical quantization lines
- // - piano roll + horizontal key lines
- // - alternating bar colors
- // - vertical beat lines
- // - vertical bar lines
- // - marked semitones
- // - note editing
- // - notes
- // - selection frame
- // - highlight hovered note
- // - note edit area resize bar
- // - cursor mode icon
- if (hasValidPattern())
- {
- int pianoAreaHeight, partialKeyVisible, topKey, topNote;
- pianoAreaHeight = keyAreaBottom() - keyAreaTop();
- m_pianoKeysVisible = pianoAreaHeight / m_keyLineHeight;
- partialKeyVisible = pianoAreaHeight % m_keyLineHeight;
- // check if we're below the minimum key area size
- if (m_pianoKeysVisible * m_keyLineHeight < KEY_AREA_MIN_HEIGHT)
- {
- m_pianoKeysVisible = KEY_AREA_MIN_HEIGHT / m_keyLineHeight;
- partialKeyVisible = KEY_AREA_MIN_HEIGHT % m_keyLineHeight;
- // if we have a partial key, just show it
- if (partialKeyVisible > 0)
- {
- m_pianoKeysVisible += 1;
- partialKeyVisible = 0;
- }
- // have to modifiy the notes edit area height instead
- m_notesEditHeight = height() - (m_pianoKeysVisible * m_keyLineHeight)
- - PR_TOP_MARGIN - PR_BOTTOM_MARGIN;
- }
- // check if we're trying to show more keys than available
- else if (m_pianoKeysVisible >= NumKeys)
- {
- m_pianoKeysVisible = NumKeys;
- // have to modify the notes edit area height instead
- m_notesEditHeight = height() - (NumKeys * m_keyLineHeight) -
- PR_TOP_MARGIN - PR_BOTTOM_MARGIN;
- partialKeyVisible = 0;
- }
- topKey = qBound(0, m_startKey + m_pianoKeysVisible - 1, NumKeys - 1);
- topNote = topKey % KeysPerOctave;
- // if not resizing the note edit area, we can change m_notesEditHeight
- if (m_action != ActionResizeNoteEditArea && partialKeyVisible != 0)
- {
- // calculate the height change adding and subtracting the partial key
- int noteAreaPlus = (m_notesEditHeight + partialKeyVisible) - m_userSetNotesEditHeight;
- int noteAreaMinus = m_userSetNotesEditHeight - (m_notesEditHeight - partialKeyVisible);
- // if adding the partial key to height is more distant from the set height
- // we want to subtract the partial key
- if (noteAreaPlus > noteAreaMinus)
- {
- m_notesEditHeight -= partialKeyVisible;
- // since we're adding a partial key, we add one to the number visible
- m_pianoKeysVisible += 1;
- }
- // otherwise we add height
- else { m_notesEditHeight += partialKeyVisible; }
- }
- updatePositionLineHeight();
- int x, q = quantization(), tick;
- // draw vertical quantization lines
- // If we're over 100% zoom, we allow all quantization level grids
- if (m_zoomingModel.value() <= 3)
- {
- // we're under 100% zoom
- // allow quantization grid up to 1/24 for triplets
- if (q % 3 != 0 && q < 8) { q = 8; }
- // allow quantization grid up to 1/32 for normal notes
- else if (q < 6) { q = 6; }
- }
- auto xCoordOfTick = [=](int tick) {
- return m_whiteKeyWidth + (
- (tick - m_currentPosition) * m_ppb / TimePos::ticksPerBar()
- );
- };
- p.setPen(m_lineColor);
- for (tick = m_currentPosition - m_currentPosition % q,
- x = xCoordOfTick(tick);
- x <= width();
- tick += q, x = xCoordOfTick(tick))
- {
- p.drawLine(x, keyAreaTop(), x, noteEditBottom());
- }
- // draw horizontal grid lines and piano notes
- p.setClipRect(0, keyAreaTop(), width(), keyAreaBottom() - keyAreaTop());
- // the first grid line from the top Y position
- int grid_line_y = keyAreaTop() + m_keyLineHeight - 1;
- // lambda function for returning the height of a key
- auto keyHeight = [&](
- const int key
- ) -> int
- {
- switch (prKeyOrder[key % KeysPerOctave])
- {
- case PR_WHITE_KEY_BIG:
- return m_whiteKeyBigHeight;
- case PR_WHITE_KEY_SMALL:
- return m_whiteKeySmallHeight;
- case PR_BLACK_KEY:
- return m_blackKeyHeight;
- }
- return 0; // should never happen
- };
- // lambda function for returning the distance to the top of a key
- auto gridCorrection = [&](
- const int key
- ) -> int
- {
- const int keyCode = key % KeysPerOctave;
- switch (prKeyOrder[keyCode])
- {
- case PR_WHITE_KEY_BIG:
- return m_whiteKeySmallHeight;
- case PR_WHITE_KEY_SMALL:
- // These two keys need to adjust up small height instead of only key line height
- if (keyCode == Key_C || keyCode == Key_F)
- {
- return m_whiteKeySmallHeight;
- }
- case PR_BLACK_KEY:
- return m_blackKeyHeight;
- }
- return 0; // should never happen
- };
- auto keyWidth = [&](
- const int key
- ) -> int
- {
- switch (prKeyOrder[key % KeysPerOctave])
- {
- case PR_WHITE_KEY_SMALL:
- case PR_WHITE_KEY_BIG:
- return m_whiteKeyWidth;
- case PR_BLACK_KEY:
- return m_blackKeyWidth;
- }
- return 0; // should never happen
- };
- // lambda function to draw a key
- auto drawKey = [&](
- const int key,
- const int yb)
- {
- const bool pressed = m_pattern->instrumentTrack()->pianoModel()->isKeyPressed(key);
- const int keyCode = key % KeysPerOctave;
- const int yt = yb - gridCorrection(key);
- const int kh = keyHeight(key);
- const int kw = keyWidth(key);
- // set key colors
- p.setPen(QColor(0, 0, 0));
- switch (prKeyOrder[keyCode])
- {
- case PR_WHITE_KEY_SMALL:
- case PR_WHITE_KEY_BIG:
- p.setBrush(pressed ? m_whiteKeyActiveBackground : m_whiteKeyInactiveBackground);
- break;
- case PR_BLACK_KEY:
- p.setBrush(pressed ? m_blackKeyActiveBackground : m_blackKeyInactiveBackground);
- }
- // draw key
- p.drawRect(PIANO_X, yt, kw, kh);
- // draw note name
- if (keyCode == Key_C || (drawNoteNames && Piano::isWhiteKey(key)))
- {
- // small font sizes have 1 pixel offset instead of 2
- auto zoomOffset = m_zoomYLevels[m_zoomingYModel.value()] > 1.0f ? 2 : 1;
- QString noteString = getNoteString(key);
- QRect textRect(
- m_whiteKeyWidth - boundingRect.width() - 2,
- yb - m_keyLineHeight + zoomOffset,
- boundingRect.width(),
- boundingRect.height()
- );
- p.setPen(pressed ? m_whiteKeyActiveTextShadow : m_whiteKeyInactiveTextShadow);
- p.drawText(textRect.adjusted(0, 1, 1, 0), Qt::AlignRight | Qt::AlignHCenter, noteString);
- p.setPen(pressed ? m_whiteKeyActiveTextColor : m_whiteKeyInactiveTextColor);
- // if (keyCode == Key_C) { p.setPen(textColor()); }
- // else { p.setPen(textColorLight()); }
- p.drawText(textRect, Qt::AlignRight | Qt::AlignHCenter, noteString);
- }
- };
- // lambda for drawing the horizontal grid line
- auto drawHorizontalLine = [&](
- const int key,
- const int y
- )
- {
- if (key % KeysPerOctave == Key_C) { p.setPen(m_beatLineColor); }
- else { p.setPen(m_lineColor); }
- p.drawLine(m_whiteKeyWidth, y, width(), y);
- };
- // correct y offset of the top key
- switch (prKeyOrder[topNote])
- {
- case PR_WHITE_KEY_SMALL:
- case PR_WHITE_KEY_BIG:
- break;
- case PR_BLACK_KEY:
- // draw extra white key
- drawKey(topKey + 1, grid_line_y - m_keyLineHeight);
- }
- // loop through visible keys
- const int lastKey = qMax(0, topKey - m_pianoKeysVisible);
- for (int key = topKey; key > lastKey; --key)
- {
- bool whiteKey = Piano::isWhiteKey(key);
- if (whiteKey)
- {
- drawKey(key, grid_line_y);
- drawHorizontalLine(key, grid_line_y);
- grid_line_y += m_keyLineHeight;
- }
- else
- {
- // draw next white key
- drawKey(key - 1, grid_line_y + m_keyLineHeight);
- drawHorizontalLine(key - 1, grid_line_y + m_keyLineHeight);
- // draw black key over previous and next white key
- drawKey(key, grid_line_y);
- drawHorizontalLine(key, grid_line_y);
- // drew two grid keys so skip ahead properly
- grid_line_y += m_keyLineHeight + m_keyLineHeight;
- // capture double key draw
- --key;
- }
- }
- // don't draw over keys
- p.setClipRect(m_whiteKeyWidth, keyAreaTop(), width(), noteEditBottom() - keyAreaTop());
- // draw alternating shading on bars
- float timeSignature =
- static_cast<float>(Engine::getSong()->getTimeSigModel().getNumerator()) /
- static_cast<float>(Engine::getSong()->getTimeSigModel().getDenominator());
- float zoomFactor = m_zoomLevels[m_zoomingModel.value()];
- //the bars which disappears at the left side by scrolling
- int leftBars = m_currentPosition * zoomFactor / TimePos::ticksPerBar();
- //iterates the visible bars and draw the shading on uneven bars
- for (int x = m_whiteKeyWidth, barCount = leftBars;
- x < width() + m_currentPosition * zoomFactor / timeSignature;
- x += m_ppb, ++barCount)
- {
- if ((barCount + leftBars) % 2 != 0)
- {
- p.fillRect(x - m_currentPosition * zoomFactor / timeSignature,
- PR_TOP_MARGIN,
- m_ppb,
- height() - (PR_BOTTOM_MARGIN + PR_TOP_MARGIN),
- m_backgroundShade);
- }
- }
- // draw vertical beat lines
- int ticksPerBeat = DefaultTicksPerBar /
- Engine::getSong()->getTimeSigModel().getDenominator();
- p.setPen(m_beatLineColor);
- for(tick = m_currentPosition - m_currentPosition % ticksPerBeat,
- x = xCoordOfTick( tick );
- x <= width();
- tick += ticksPerBeat, x = xCoordOfTick(tick))
- {
- p.drawLine(x, PR_TOP_MARGIN, x, noteEditBottom());
- }
- // draw vertical bar lines
- p.setPen(m_barLineColor);
- for(tick = m_currentPosition - m_currentPosition % TimePos::ticksPerBar(),
- x = xCoordOfTick( tick );
- x <= width();
- tick += TimePos::ticksPerBar(), x = xCoordOfTick(tick))
- {
- p.drawLine(x, PR_TOP_MARGIN, x, noteEditBottom());
- }
- // draw marked semitones after the grid
- for(x = 0; x < m_markedSemiTones.size(); ++x)
- {
- const int key_num = m_markedSemiTones.at(x);
- const int y = keyAreaBottom() + 5 - m_keyLineHeight *
- (key_num - m_startKey + 1);
- if(y > keyAreaBottom()) { break; }
- p.fillRect(m_whiteKeyWidth + 1,
- y - m_keyLineHeight / 2,
- width() - 10,
- m_keyLineHeight + 1,
- m_markedSemitoneColor);
- }
- }
- // reset clip
- p.setClipRect(0, 0, width(), height());
- // erase the area below the piano, because there might be keys that
- // should be only half-visible
- p.fillRect( QRect( 0, keyAreaBottom(),
- m_whiteKeyWidth, noteEditBottom() - keyAreaBottom()), bgColor);
- // display note editing info
- //QFont f = p.font();
- f.setBold( false );
- p.setFont( pointSize<10>( f ) );
- p.setPen(m_noteModeColor);
- p.drawText( QRect( 0, keyAreaBottom(),
- m_whiteKeyWidth, noteEditBottom() - keyAreaBottom()),
- Qt::AlignCenter | Qt::TextWordWrap,
- m_nemStr.at( m_noteEditMode ) + ":" );
- // set clipping area, because we are not allowed to paint over
- // keyboard...
- p.setClipRect(
- m_whiteKeyWidth,
- PR_TOP_MARGIN,
- width() - m_whiteKeyWidth,
- height() - PR_TOP_MARGIN - PR_BOTTOM_MARGIN);
- // following code draws all notes in visible area
- // and the note editing stuff (volume, panning, etc)
- // setup selection-vars
- int sel_pos_start = m_selectStartTick;
- int sel_pos_end = m_selectStartTick+m_selectedTick;
- if( sel_pos_start > sel_pos_end )
- {
- qSwap<int>( sel_pos_start, sel_pos_end );
- }
- int sel_key_start = m_selectStartKey - m_startKey + 1;
- int sel_key_end = sel_key_start + m_selectedKeys;
- if( sel_key_start > sel_key_end )
- {
- qSwap<int>( sel_key_start, sel_key_end );
- }
- int y_base = keyAreaBottom() - 1;
- if( hasValidPattern() )
- {
- p.setClipRect(
- m_whiteKeyWidth,
- PR_TOP_MARGIN,
- width() - m_whiteKeyWidth,
- height() - PR_TOP_MARGIN);
- const int topKey = qBound(0, m_startKey + m_pianoKeysVisible - 1, NumKeys - 1);
- const int bottomKey = topKey - m_pianoKeysVisible;
- QPolygonF editHandles;
- // -- Begin ghost pattern
- if( !m_ghostNotes.empty() )
- {
- for( const Note *note : m_ghostNotes )
- {
- int len_ticks = note->length();
- if( len_ticks == 0 )
- {
- continue;
- }
- else if( len_ticks < 0 )
- {
- len_ticks = 4;
- }
- const int key = note->key() - m_startKey + 1;
- int pos_ticks = note->pos();
- int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
- const int x = ( pos_ticks - m_currentPosition ) *
- m_ppb / TimePos::ticksPerBar();
- // skip this note if not in visible area at all
- if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
- {
- continue;
- }
- // is the note in visible area?
- if (note->key() > bottomKey && note->key() <= topKey)
- {
- // we've done and checked all, let's draw the note
- drawNoteRect(
- p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
- note, m_ghostNoteColor, m_ghostNoteTextColor, m_selectedNoteColor,
- m_ghostNoteOpacity, m_ghostNoteBorders, drawNoteNames);
- }
- }
- }
- // -- End ghost pattern
- for( const Note *note : m_pattern->notes() )
- {
- int len_ticks = note->length();
- if( len_ticks == 0 )
- {
- continue;
- }
- else if( len_ticks < 0 )
- {
- len_ticks = 4;
- }
- const int key = note->key() - m_startKey + 1;
- int pos_ticks = note->pos();
- int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
- const int x = ( pos_ticks - m_currentPosition ) *
- m_ppb / TimePos::ticksPerBar();
- // skip this note if not in visible area at all
- if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
- {
- continue;
- }
- // is the note in visible area?
- if (note->key() > bottomKey && note->key() <= topKey)
- {
- // we've done and checked all, let's draw the note
- drawNoteRect(
- p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
- note, m_noteColor, m_noteTextColor, m_selectedNoteColor,
- m_noteOpacity, m_noteBorders, drawNoteNames);
- }
- // draw note editing stuff
- int editHandleTop = 0;
- if( m_noteEditMode == NoteEditVolume )
- {
- QColor color = m_barColor.lighter(30 + (note->getVolume() * 90 / MaxVolume));
- if( note->selected() )
- {
- color = m_selectedNoteColor;
- }
- p.setPen( QPen( color, NOTE_EDIT_LINE_WIDTH ) );
- editHandleTop = noteEditBottom() -
- ( (float)( note->getVolume() - MinVolume ) ) /
- ( (float)( MaxVolume - MinVolume ) ) *
- ( (float)( noteEditBottom() - noteEditTop() ) );
- p.drawLine( QLineF ( noteEditLeft() + x + 0.5, editHandleTop + 0.5,
- noteEditLeft() + x + 0.5, noteEditBottom() + 0.5 ) );
- }
- else if( m_noteEditMode == NoteEditPanning )
- {
- QColor color = m_noteColor;
- if( note->selected() )
- {
- color = m_selectedNoteColor;
- }
- p.setPen( QPen( color, NOTE_EDIT_LINE_WIDTH ) );
- editHandleTop = noteEditBottom() -
- ( (float)( note->getPanning() - PanningLeft ) ) /
- ( (float)( (PanningRight - PanningLeft ) ) ) *
- ( (float)( noteEditBottom() - noteEditTop() ) );
- p.drawLine( QLine( noteEditLeft() + x, noteEditTop() +
- ( (float)( noteEditBottom() - noteEditTop() ) ) / 2.0f,
- noteEditLeft() + x , editHandleTop ) );
- }
- editHandles << QPoint ( x + noteEditLeft(),
- editHandleTop );
- if( note->hasDetuningInfo() )
- {
- drawDetuningInfo(p, note, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight);
- p.setClipRect(
- m_whiteKeyWidth,
- PR_TOP_MARGIN,
- width() - m_whiteKeyWidth,
- height() - PR_TOP_MARGIN);
- }
- }
- // -- Razor tool (draw cut line)
- if (m_action == ActionRazor)
- {
- auto xCoordOfTick = [this](int tick) {
- return m_whiteKeyWidth + (
- (tick - m_currentPosition) * m_ppb / TimePos::ticksPerBar());
- };
- Note* n = noteUnderMouse();
- if (n)
- {
- const int key = n->key() - m_startKey + 1;
- int y = y_base - key * m_keyLineHeight;
- int x = xCoordOfTick(m_razorTickPos);
- if (x > xCoordOfTick(n->pos()) &&
- x < xCoordOfTick(n->pos() + n->length()))
- {
- p.setPen(QPen(m_razorCutLineColor, 1));
- p.drawLine(x, y, x, y + m_keyLineHeight);
- setCursor(Qt::BlankCursor);
- }
- else
- {
- setCursor(Qt::ArrowCursor);
- }
- }
- else
- {
- setCursor(Qt::ArrowCursor);
- }
- }
- // -- End razor tool
- //draw current step recording notes
- for( const Note *note : m_stepRecorder.getCurStepNotes() )
- {
- int len_ticks = note->length();
- if( len_ticks == 0 )
- {
- continue;
- }
- const int key = note->key() - m_startKey + 1;
- int pos_ticks = note->pos();
- int note_width = len_ticks * m_ppb / TimePos::ticksPerBar();
- const int x = ( pos_ticks - m_currentPosition ) *
- m_ppb / TimePos::ticksPerBar();
- // skip this note if not in visible area at all
- if (!(x + note_width >= 0 && x <= width() - m_whiteKeyWidth))
- {
- continue;
- }
- // is the note in visible area?
- if (note->key() > bottomKey && note->key() <= topKey)
- {
- // we've done and checked all, let's draw the note
- drawNoteRect(
- p, x + m_whiteKeyWidth, y_base - key * m_keyLineHeight, note_width,
- note, m_stepRecorder.curStepNoteColor(), m_noteTextColor, m_selectedNoteColor,
- m_noteOpacity, m_noteBorders, drawNoteNames);
- }
- }
- p.setPen(QPen(m_noteColor, NOTE_EDIT_LINE_WIDTH + 2));
- p.drawPoints( editHandles );
- }
- else
- {
- QFont f = p.font();
- f.setBold( true );
- p.setFont( pointSize<14>( f ) );
- p.setPen( QApplication::palette().color( QPalette::Active,
- QPalette::BrightText ) );
- p.drawText(m_whiteKeyWidth + 20, PR_TOP_MARGIN + 40,
- tr( "Please open a pattern by double-clicking "
- "on it!" ) );
- }
- p.setClipRect(
- m_whiteKeyWidth,
- PR_TOP_MARGIN,
- width() - m_whiteKeyWidth,
- height() - PR_TOP_MARGIN - m_notesEditHeight - PR_BOTTOM_MARGIN);
- // now draw selection-frame
- int x = ( ( sel_pos_start - m_currentPosition ) * m_ppb ) /
- TimePos::ticksPerBar();
- int w = ( ( ( sel_pos_end - m_currentPosition ) * m_ppb ) /
- TimePos::ticksPerBar() ) - x;
- int y = (int) y_base - sel_key_start * m_keyLineHeight;
- int h = (int) y_base - sel_key_end * m_keyLineHeight - y;
- p.setPen(m_selectedNoteColor);
- p.setBrush( Qt::NoBrush );
- p.drawRect(x + m_whiteKeyWidth, y, w, h);
- // TODO: Get this out of paint event
- int l = ( hasValidPattern() )? (int) m_pattern->length() : 0;
- // reset scroll-range
- if( m_leftRightScroll->maximum() != l )
- {
- m_leftRightScroll->setRange( 0, l );
- m_leftRightScroll->setPageStep( l );
- }
- // set line colors
- QColor editAreaCol = QColor(m_lineColor);
- QColor currentKeyCol = QColor(m_beatLineColor);
- editAreaCol.setAlpha( 64 );
- currentKeyCol.setAlpha( 64 );
- // horizontal line for the key under the cursor
- if(hasValidPattern() && gui->pianoRoll()->hasFocus())
- {
- int key_num = getKey( mapFromGlobal( QCursor::pos() ).y() );
- p.fillRect( 10, keyAreaBottom() + 3 - m_keyLineHeight *
- ( key_num - m_startKey + 1 ), width() - 10, m_keyLineHeight - 7, currentKeyCol );
- }
- // bar to resize note edit area
- p.setClipRect( 0, 0, width(), height() );
- p.fillRect( QRect( 0, keyAreaBottom(),
- width()-PR_RIGHT_MARGIN, NOTE_EDIT_RESIZE_BAR ), editAreaCol );
- if (gui->pianoRoll()->hasFocus())
- {
- const QPixmap * cursor = NULL;
- // draw current edit-mode-icon below the cursor
- switch( m_editMode )
- {
- case ModeDraw:
- if( m_mouseDownRight )
- {
- cursor = s_toolErase;
- }
- else if( m_action == ActionMoveNote )
- {
- cursor = s_toolMove;
- }
- else
- {
- cursor = s_toolDraw;
- }
- break;
- case ModeErase: cursor = s_toolErase; break;
- case ModeSelect: cursor = s_toolSelect; break;
- case ModeEditDetuning: cursor = s_toolOpen; break;
- case ModeEditRazor: cursor = s_toolRazor; break;
- }
- QPoint mousePosition = mapFromGlobal( QCursor::pos() );
- if( cursor != NULL && mousePosition.y() > keyAreaTop() && mousePosition.x() > noteEditLeft())
- {
- p.drawPixmap( mousePosition + QPoint( 8, 8 ), *cursor );
- }
- }
- }
- void PianoRoll::updateScrollbars()
- {
- m_leftRightScroll->setGeometry(
- m_whiteKeyWidth,
- height() - SCROLLBAR_SIZE,
- width() - m_whiteKeyWidth,
- SCROLLBAR_SIZE
- );
- m_topBottomScroll->setGeometry(
- width() - SCROLLBAR_SIZE,
- PR_TOP_MARGIN,
- SCROLLBAR_SIZE,
- height() - PR_TOP_MARGIN - SCROLLBAR_SIZE
- );
- int pianoAreaHeight = keyAreaBottom() - PR_TOP_MARGIN;
- int numKeysVisible = pianoAreaHeight / m_keyLineHeight;
- m_totalKeysToScroll = qMax(0, NumKeys - numKeysVisible);
- m_topBottomScroll->setRange(0, m_totalKeysToScroll);
- if (m_startKey > m_totalKeysToScroll)
- {
- m_startKey = qMax(0, m_totalKeysToScroll);
- }
- m_topBottomScroll->setValue(m_totalKeysToScroll - m_startKey);
- }
- // responsible for moving/resizing scrollbars after window-resizing
- void PianoRoll::resizeEvent(QResizeEvent * re)
- {
- updatePositionLineHeight();
- updateScrollbars();
- Engine::getSong()->getPlayPos(Song::Mode_PlayPattern)
- .m_timeLine->setFixedWidth(width());
- update();
- }
- void PianoRoll::wheelEvent(QWheelEvent * we )
- {
- we->accept();
- // handle wheel events for note edit area - for editing note vol/pan with mousewheel
- if(position(we).x() > noteEditLeft() && position(we).x() < noteEditRight()
- && position(we).y() > noteEditTop() && position(we).y() < noteEditBottom())
- {
- if (!hasValidPattern()) {return;}
- // get values for going through notes
- int pixel_range = 8;
- int x = position(we).x() - m_whiteKeyWidth;
- int ticks_start = ( x - pixel_range / 2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- int ticks_end = ( x + pixel_range / 2 ) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- // When alt is pressed we only edit the note under the cursor
- bool altPressed = we->modifiers() & Qt::AltModifier;
- // go through notes to figure out which one we want to change
- NoteVector nv;
- for ( Note * i : m_pattern->notes() )
- {
- if( i->withinRange( ticks_start, ticks_end ) || ( i->selected() && !altPressed ) )
- {
- nv += i;
- }
- }
- if( nv.size() > 0 )
- {
- const int step = we->angleDelta().y() > 0 ? 1 : -1;
- if( m_noteEditMode == NoteEditVolume )
- {
- for ( Note * n : nv )
- {
- volume_t vol = qBound<int>( MinVolume, n->getVolume() + step, MaxVolume );
- n->setVolume( vol );
- }
- bool allVolumesEqual = std::all_of( nv.begin(), nv.end(),
- [nv](const Note *note)
- {
- return note->getVolume() == nv[0]->getVolume();
- });
- if ( allVolumesEqual )
- {
- // show the volume hover-text only if all notes have the
- // same volume
- showVolTextFloat(nv[0]->getVolume(), position(we), 1000);
- }
- }
- else if( m_noteEditMode == NoteEditPanning )
- {
- for ( Note * n : nv )
- {
- panning_t pan = qBound<int>( PanningLeft, n->getPanning() + step, PanningRight );
- n->setPanning( pan );
- }
- bool allPansEqual = std::all_of( nv.begin(), nv.end(),
- [nv](const Note *note)
- {
- return note->getPanning() == nv[0]->getPanning();
- });
- if ( allPansEqual )
- {
- // show the pan hover-text only if all notes have the same
- // panning
- showPanTextFloat( nv[0]->getPanning(), position( we ), 1000 );
- }
- }
- update();
- }
- }
- // not in note edit area, so handle scrolling/zooming and quantization change
- else
- if( we->modifiers() & Qt::ControlModifier && we->modifiers() & Qt::AltModifier )
- {
- int q = m_quantizeModel.value();
- if((we->angleDelta().x() + we->angleDelta().y()) > 0) // alt + scroll becomes horizontal scroll on KDE
- {
- q--;
- }
- else if((we->angleDelta().x() + we->angleDelta().y()) < 0) // alt + scroll becomes horizontal scroll on KDE
- {
- q++;
- }
- q = qBound( 0, q, m_quantizeModel.size() - 1 );
- m_quantizeModel.setValue( q );
- }
- else if( we->modifiers() & Qt::ControlModifier && we->modifiers() & Qt::ShiftModifier )
- {
- int l = m_noteLenModel.value();
- if(we->angleDelta().y() > 0)
- {
- l--;
- }
- else if(we->angleDelta().y() < 0)
- {
- l++;
- }
- l = qBound( 0, l, m_noteLenModel.size() - 1 );
- m_noteLenModel.setValue( l );
- }
- else if( we->modifiers() & Qt::ControlModifier )
- {
- int z = m_zoomingModel.value();
- if(we->angleDelta().y() > 0)
- {
- z++;
- }
- else if(we->angleDelta().y() < 0)
- {
- z--;
- }
- z = qBound( 0, z, m_zoomingModel.size() - 1 );
- int x = (position(we).x() - m_whiteKeyWidth) * TimePos::ticksPerBar();
- // ticks based on the mouse x-position where the scroll wheel was used
- int ticks = x / m_ppb;
- // what would be the ticks in the new zoom level on the very same mouse x
- int newTicks = x / (DEFAULT_PR_PPB * m_zoomLevels[z]);
- // scroll so the tick "selected" by the mouse x doesn't move on the screen
- m_leftRightScroll->setValue(m_leftRightScroll->value() + ticks - newTicks);
- // update combobox with zooming-factor
- m_zoomingModel.setValue( z );
- }
- // FIXME: Reconsider if determining orientation is necessary in Qt6.
- else if(abs(we->angleDelta().x()) > abs(we->angleDelta().y())) // scrolling is horizontal
- {
- m_leftRightScroll->setValue(m_leftRightScroll->value() -
- we->angleDelta().x() * 2 / 15);
- }
- else if(we->modifiers() & Qt::ShiftModifier)
- {
- m_leftRightScroll->setValue(m_leftRightScroll->value() -
- we->angleDelta().y() * 2 / 15);
- }
- else
- {
- m_topBottomScroll->setValue(m_topBottomScroll->value() -
- we->angleDelta().y() / 30);
- }
- }
- void PianoRoll::focusOutEvent( QFocusEvent * )
- {
- if( hasValidPattern() )
- {
- for( int i = 0; i < NumKeys; ++i )
- {
- m_pattern->instrumentTrack()->pianoModel()->midiEventProcessor()->processInEvent( MidiEvent( MidiNoteOff, -1, i, 0 ) );
- m_pattern->instrumentTrack()->pianoModel()->setKeyState( i, false );
- }
- }
- if (m_editMode == ModeEditRazor) {
- m_editMode = m_razorMode;
- m_action = ActionNone;
- } else {
- m_editMode = m_ctrlMode;
- }
- update();
- }
- void PianoRoll::focusInEvent( QFocusEvent * ev )
- {
- if ( hasValidPattern() )
- {
- // Assign midi device
- m_pattern->instrumentTrack()->autoAssignMidiDevice(true);
- }
- QWidget::focusInEvent(ev);
- }
- int PianoRoll::getKey(int y) const
- {
- // handle case that very top pixel maps to next key above
- if (y - keyAreaTop() <= 1) { y = keyAreaTop() + 2; }
- int key_num = qBound(
- 0,
- // add + 1 to stay within the grid lines
- ((keyAreaBottom() - y + 1) / m_keyLineHeight) + m_startKey,
- NumKeys - 1
- );
- return key_num;
- }
- QList<int> PianoRoll::getAllOctavesForKey( int keyToMirror ) const
- {
- QList<int> keys;
- for (int i=keyToMirror % KeysPerOctave; i < NumKeys; i += KeysPerOctave)
- {
- keys.append(i);
- }
- return keys;
- }
- Song::PlayModes PianoRoll::desiredPlayModeForAccompany() const
- {
- if( m_pattern->getTrack()->trackContainer() ==
- Engine::getBBTrackContainer() )
- {
- return Song::Mode_PlayBB;
- }
- return Song::Mode_PlaySong;
- }
- void PianoRoll::play()
- {
- if( ! hasValidPattern() )
- {
- return;
- }
- if( Engine::getSong()->playMode() != Song::Mode_PlayPattern )
- {
- Engine::getSong()->playPattern( m_pattern );
- }
- else
- {
- Engine::getSong()->togglePause();
- }
- }
- void PianoRoll::record()
- {
- if( Engine::getSong()->isPlaying() )
- {
- stop();
- }
- if( m_recording || ! hasValidPattern() )
- {
- return;
- }
- m_pattern->addJournalCheckPoint();
- m_recording = true;
- Engine::getSong()->playPattern( m_pattern, false );
- }
- void PianoRoll::recordAccompany()
- {
- if( Engine::getSong()->isPlaying() )
- {
- stop();
- }
- if( m_recording || ! hasValidPattern() )
- {
- return;
- }
- m_pattern->addJournalCheckPoint();
- m_recording = true;
- if( m_pattern->getTrack()->trackContainer() == Engine::getSong() )
- {
- Engine::getSong()->playSong();
- }
- else
- {
- Engine::getSong()->playBB();
- }
- }
- bool PianoRoll::toggleStepRecording()
- {
- if(m_stepRecorder.isRecording())
- {
- m_stepRecorder.stop();
- }
- else
- {
- if(hasValidPattern())
- {
- if(Engine::getSong()->isPlaying())
- {
- m_stepRecorder.start(0, newNoteLen());
- }
- else
- {
- m_stepRecorder.start(
- Engine::getSong()->getPlayPos(
- Song::Mode_PlayPattern), newNoteLen());
- }
- }
- }
- return m_stepRecorder.isRecording();;
- }
- void PianoRoll::stop()
- {
- Engine::getSong()->stop();
- m_recording = false;
- m_scrollBack = ( m_timeLine->autoScroll() == TimeLineWidget::AutoScrollEnabled );
- }
- void PianoRoll::startRecordNote(const Note & n )
- {
- if(hasValidPattern())
- {
- if( m_recording &&
- Engine::getSong()->isPlaying() &&
- (Engine::getSong()->playMode() == desiredPlayModeForAccompany() ||
- Engine::getSong()->playMode() == Song::Mode_PlayPattern ))
- {
- TimePos sub;
- if( Engine::getSong()->playMode() == Song::Mode_PlaySong )
- {
- sub = m_pattern->startPosition();
- }
- Note n1( 1, Engine::getSong()->getPlayPos(
- Engine::getSong()->playMode() ) - sub,
- n.key(), n.getVolume(), n.getPanning() );
- if( n1.pos() >= 0 )
- {
- m_recordingNotes << n1;
- }
- }
- else if (m_stepRecorder.isRecording())
- {
- m_stepRecorder.notePressed(n);
- }
- }
- }
- void PianoRoll::finishRecordNote(const Note & n )
- {
- if(hasValidPattern())
- {
- if( m_recording &&
- Engine::getSong()->isPlaying() &&
- ( Engine::getSong()->playMode() ==
- desiredPlayModeForAccompany() ||
- Engine::getSong()->playMode() ==
- Song::Mode_PlayPattern ) )
- {
- for( QList<Note>::Iterator it = m_recordingNotes.begin();
- it != m_recordingNotes.end(); ++it )
- {
- if( it->key() == n.key() )
- {
- Note n1( n.length(), it->pos(),
- it->key(), it->getVolume(),
- it->getPanning() );
- n1.quantizeLength( quantization() );
- m_pattern->addNote( n1 );
- update();
- m_recordingNotes.erase( it );
- break;
- }
- }
- }
- else if (m_stepRecorder.isRecording())
- {
- m_stepRecorder.noteReleased(n);
- }
- }
- }
- void PianoRoll::horScrolled(int new_pos )
- {
- m_currentPosition = new_pos;
- m_stepRecorderWidget.setCurrentPosition(m_currentPosition);
- emit positionChanged( m_currentPosition );
- update();
- }
- void PianoRoll::verScrolled( int new_pos )
- {
- // revert value
- m_startKey = qMax(0, m_totalKeysToScroll - new_pos);
- update();
- }
- void PianoRoll::setEditMode(int mode)
- {
- m_ctrlMode = m_editMode = (EditModes) mode;
- }
- void PianoRoll::selectAll()
- {
- if( ! hasValidPattern() )
- {
- return;
- }
- // if first_time = true, we HAVE to set the vars for select
- bool first_time = true;
- for( const Note *note : m_pattern->notes() )
- {
- int len_ticks = static_cast<int>( note->length() ) > 0 ?
- static_cast<int>( note->length() ) : 1;
- const int key = note->key();
- int pos_ticks = note->pos();
- if( key <= m_selectStartKey || first_time )
- {
- // if we move start-key down, we have to add
- // the difference between old and new start-key
- // to m_selectedKeys, otherwise the selection
- // is just moved down...
- m_selectedKeys += m_selectStartKey
- - ( key - 1 );
- m_selectStartKey = key - 1;
- }
- if( key >= m_selectedKeys + m_selectStartKey ||
- first_time )
- {
- m_selectedKeys = key - m_selectStartKey;
- }
- if( pos_ticks < m_selectStartTick ||
- first_time )
- {
- m_selectStartTick = pos_ticks;
- }
- if( pos_ticks + len_ticks >
- m_selectStartTick + m_selectedTick ||
- first_time )
- {
- m_selectedTick = pos_ticks +
- len_ticks -
- m_selectStartTick;
- }
- first_time = false;
- }
- }
- // returns vector with pointers to all selected notes
- NoteVector PianoRoll::getSelectedNotes() const
- {
- NoteVector selectedNotes;
- if (hasValidPattern())
- {
- for( Note *note : m_pattern->notes() )
- {
- if( note->selected() )
- {
- selectedNotes.push_back( note );
- }
- }
- }
- return selectedNotes;
- }
- // selects all notess associated with m_lastKey
- void PianoRoll::selectNotesOnKey()
- {
- if (hasValidPattern()) {
- for (Note * note : m_pattern->notes()) {
- if (note->key() == m_lastKey) {
- note->setSelected(true);
- }
- }
- }
- }
- void PianoRoll::enterValue( NoteVector* nv )
- {
- if( m_noteEditMode == NoteEditVolume )
- {
- bool ok;
- int new_val;
- new_val = QInputDialog::getInt( this, "Piano roll: note velocity",
- tr( "Please enter a new value between %1 and %2:" ).
- arg( MinVolume ).arg( MaxVolume ),
- (*nv)[0]->getVolume(),
- MinVolume, MaxVolume, 1, &ok );
- if( ok )
- {
- for ( Note * n : *nv )
- {
- n->setVolume( new_val );
- }
- m_lastNoteVolume = new_val;
- }
- }
- else if( m_noteEditMode == NoteEditPanning )
- {
- bool ok;
- int new_val;
- new_val = QInputDialog::getInt( this, "Piano roll: note panning",
- tr( "Please enter a new value between %1 and %2:" ).
- arg( PanningLeft ).arg( PanningRight ),
- (*nv)[0]->getPanning(),
- PanningLeft, PanningRight, 1, &ok );
- if( ok )
- {
- for ( Note * n : *nv )
- {
- n->setPanning( new_val );
- }
- m_lastNotePanning = new_val;
- }
- }
- }
- void PianoRoll::updateYScroll()
- {
- m_topBottomScroll->setGeometry(width() - SCROLLBAR_SIZE, PR_TOP_MARGIN,
- SCROLLBAR_SIZE,
- height() - PR_TOP_MARGIN -
- SCROLLBAR_SIZE);
- int total_pixels = m_octaveHeight * NumOctaves - (height() -
- PR_TOP_MARGIN - PR_BOTTOM_MARGIN -
- m_notesEditHeight);
- m_totalKeysToScroll = qMax(0, total_pixels * KeysPerOctave / m_octaveHeight);
- m_topBottomScroll->setRange(0, m_totalKeysToScroll);
- if(m_startKey > m_totalKeysToScroll)
- {
- m_startKey = qMax(0, m_totalKeysToScroll);
- }
- m_topBottomScroll->setValue(m_totalKeysToScroll - m_startKey);
- }
- void PianoRoll::copyToClipboard( const NoteVector & notes ) const
- {
- // For copyString() and MimeType enum class
- using namespace Clipboard;
- DataFile dataFile( DataFile::ClipboardData );
- QDomElement note_list = dataFile.createElement( "note-list" );
- dataFile.content().appendChild( note_list );
- TimePos start_pos( notes.front()->pos().getBar(), 0 );
- for( const Note *note : notes )
- {
- Note clip_note( *note );
- clip_note.setPos( clip_note.pos( start_pos ) );
- clip_note.saveState( dataFile, note_list );
- }
- copyString( dataFile.toString(), MimeType::Default );
- }
- void PianoRoll::copySelectedNotes()
- {
- NoteVector selected_notes = getSelectedNotes();
- if( ! selected_notes.empty() )
- {
- copyToClipboard( selected_notes );
- }
- }
- void PianoRoll::cutSelectedNotes()
- {
- if( ! hasValidPattern() )
- {
- return;
- }
- NoteVector selected_notes = getSelectedNotes();
- if( ! selected_notes.empty() )
- {
- m_pattern->addJournalCheckPoint();
- copyToClipboard( selected_notes );
- Engine::getSong()->setModified();
- for( Note *note : selected_notes )
- {
- // note (the memory of it) is also deleted by
- // pattern::removeNote(...) so we don't have to do that
- m_pattern->removeNote( note );
- }
- }
- update();
- gui->songEditor()->update();
- }
- void PianoRoll::pasteNotes()
- {
- // For getString() and MimeType enum class
- using namespace Clipboard;
- if( ! hasValidPattern() )
- {
- return;
- }
- QString value = getString( MimeType::Default );
- if( ! value.isEmpty() )
- {
- DataFile dataFile( value.toUtf8() );
- QDomNodeList list = dataFile.elementsByTagName( Note::classNodeName() );
- // remove selection and select the newly pasted notes
- clearSelectedNotes();
- if( ! list.isEmpty() )
- {
- m_pattern->addJournalCheckPoint();
- }
- for( int i = 0; ! list.item( i ).isNull(); ++i )
- {
- // create the note
- Note cur_note;
- cur_note.restoreState( list.item( i ).toElement() );
- cur_note.setPos( cur_note.pos() + Note::quantized( m_timeLine->pos(), quantization() ) );
- // select it
- cur_note.setSelected( true );
- // add to pattern
- m_pattern->addNote( cur_note, false );
- }
- // we only have to do the following lines if we pasted at
- // least one note...
- Engine::getSong()->setModified();
- update();
- gui->songEditor()->update();
- }
- }
- //Return false if no notes are deleted
- bool PianoRoll::deleteSelectedNotes()
- {
- if (!hasValidPattern()) { return false; }
- auto selectedNotes = getSelectedNotes();
- if (selectedNotes.empty()) { return false; }
- m_pattern->addJournalCheckPoint();
- for (Note* note: selectedNotes) { m_pattern->removeNote( note ); }
- Engine::getSong()->setModified();
- update();
- gui->songEditor()->update();
- return true;
- }
- void PianoRoll::autoScroll( const TimePos & t )
- {
- const int w = width() - m_whiteKeyWidth;
- if( t > m_currentPosition + w * TimePos::ticksPerBar() / m_ppb )
- {
- m_leftRightScroll->setValue( t.getBar() * TimePos::ticksPerBar() );
- }
- else if( t < m_currentPosition )
- {
- TimePos t2 = qMax( t - w * TimePos::ticksPerBar() *
- TimePos::ticksPerBar() / m_ppb, (tick_t) 0 );
- m_leftRightScroll->setValue( t2.getBar() * TimePos::ticksPerBar() );
- }
- m_scrollBack = false;
- }
- void PianoRoll::updatePosition( const TimePos & t )
- {
- if( ( Engine::getSong()->isPlaying()
- && Engine::getSong()->playMode() == Song::Mode_PlayPattern
- && m_timeLine->autoScroll() == TimeLineWidget::AutoScrollEnabled
- ) || m_scrollBack )
- {
- autoScroll( t );
- }
- const int pos = m_timeLine->pos() * m_ppb / TimePos::ticksPerBar();
- if (pos >= m_currentPosition && pos <= m_currentPosition + width() - m_whiteKeyWidth)
- {
- m_positionLine->show();
- m_positionLine->move(pos - (m_positionLine->width() - 1) - m_currentPosition + m_whiteKeyWidth, keyAreaTop());
- }
- else
- {
- m_positionLine->hide();
- }
- }
- void PianoRoll::updatePositionLineHeight()
- {
- m_positionLine->setFixedHeight(keyAreaBottom() - keyAreaTop());
- }
- void PianoRoll::updatePositionAccompany( const TimePos & t )
- {
- Song * s = Engine::getSong();
- if( m_recording && hasValidPattern() &&
- s->playMode() != Song::Mode_PlayPattern )
- {
- TimePos pos = t;
- if( s->playMode() != Song::Mode_PlayBB )
- {
- pos -= m_pattern->startPosition();
- }
- if( (int) pos > 0 )
- {
- s->getPlayPos( Song::Mode_PlayPattern ).setTicks( pos );
- autoScroll( pos );
- }
- }
- }
- void PianoRoll::updatePositionStepRecording( const TimePos & t )
- {
- if( m_stepRecorder.isRecording() )
- {
- autoScroll( t );
- }
- }
- void PianoRoll::zoomingChanged()
- {
- m_ppb = m_zoomLevels[m_zoomingModel.value()] * DEFAULT_PR_PPB;
- assert( m_ppb > 0 );
- m_timeLine->setPixelsPerBar( m_ppb );
- m_stepRecorderWidget.setPixelsPerBar( m_ppb );
- m_positionLine->zoomChange(m_zoomLevels[m_zoomingModel.value()]);
- update();
- }
- void PianoRoll::zoomingYChanged()
- {
- m_keyLineHeight = m_zoomYLevels[m_zoomingYModel.value()] * DEFAULT_KEY_LINE_HEIGHT;
- m_octaveHeight = m_keyLineHeight * KeysPerOctave;
- m_whiteKeySmallHeight = qFloor(m_keyLineHeight * 1.5);
- m_whiteKeyBigHeight = m_keyLineHeight * 2;
- m_blackKeyHeight = m_keyLineHeight; //round(m_keyLineHeight * 1.3333);
- updateYScroll();
- update();
- }
- void PianoRoll::quantizeChanged()
- {
- update();
- }
- void PianoRoll::noteLengthChanged()
- {
- m_stepRecorder.setStepsLength(newNoteLen());
- update();
- }
- void PianoRoll::keyChanged()
- {
- markSemiTone(stmaMarkCurrentScale, false);
- }
- int PianoRoll::quantization() const
- {
- if( m_quantizeModel.value() == 0 )
- {
- if( m_noteLenModel.value() > 0 )
- {
- return newNoteLen();
- }
- else
- {
- return DefaultTicksPerBar / 16;
- }
- }
- return DefaultTicksPerBar / Quantizations[m_quantizeModel.value() - 1];
- }
- void PianoRoll::quantizeNotes()
- {
- if( ! hasValidPattern() )
- {
- return;
- }
- m_pattern->addJournalCheckPoint();
- NoteVector notes = getSelectedNotes();
- if( notes.empty() )
- {
- for( Note* n : m_pattern->notes() )
- {
- notes.push_back( n );
- }
- }
- for( Note* n : notes )
- {
- if( n->length() == TimePos( 0 ) )
- {
- continue;
- }
- Note copy(*n);
- m_pattern->removeNote( n );
- copy.quantizePos( quantization() );
- m_pattern->addNote( copy );
- }
- update();
- gui->songEditor()->update();
- Engine::getSong()->setModified();
- }
- void PianoRoll::updateSemiToneMarkerMenu()
- {
- const InstrumentFunctionNoteStacking::ChordTable& chord_table =
- InstrumentFunctionNoteStacking::ChordTable::getInstance();
- const InstrumentFunctionNoteStacking::Chord& scale =
- chord_table.getScaleByName( m_scaleModel.currentText() );
- const InstrumentFunctionNoteStacking::Chord& chord =
- chord_table.getChordByName( m_chordModel.currentText() );
- emit semiToneMarkerMenuScaleSetEnabled( ! scale.isEmpty() );
- emit semiToneMarkerMenuChordSetEnabled( ! chord.isEmpty() );
- }
- TimePos PianoRoll::newNoteLen() const
- {
- if( m_noteLenModel.value() == 0 )
- {
- return m_lenOfNewNotes;
- }
- QString text = m_noteLenModel.currentText();
- return DefaultTicksPerBar / text.right( text.length() - 2 ).toInt();
- }
- bool PianoRoll::mouseOverNote()
- {
- return hasValidPattern() && noteUnderMouse() != NULL;
- }
- Note * PianoRoll::noteUnderMouse()
- {
- QPoint pos = mapFromGlobal( QCursor::pos() );
- if (pos.x() <= m_whiteKeyWidth
- || pos.x() > width() - SCROLLBAR_SIZE
- || pos.y() < PR_TOP_MARGIN
- || pos.y() > keyAreaBottom() )
- {
- return NULL;
- }
- int key_num = getKey( pos.y() );
- int pos_ticks = (pos.x() - m_whiteKeyWidth) *
- TimePos::ticksPerBar() / m_ppb + m_currentPosition;
- // loop through whole note-vector...
- for( Note* const& note : m_pattern->notes() )
- {
- // and check whether the cursor is over an
- // existing note
- if( pos_ticks >= note->pos()
- && pos_ticks <= note->endPos()
- && note->key() == key_num
- && note->length() > 0 )
- {
- return note;
- }
- }
- return NULL;
- }
- PianoRollWindow::PianoRollWindow() :
- Editor(true, true),
- m_editor(new PianoRoll())
- {
- setCentralWidget( m_editor );
- m_playAction->setToolTip(tr( "Play/pause current pattern (Space)" ) );
- m_recordAction->setToolTip(tr( "Record notes from MIDI-device/channel-piano" ) );
- m_recordAccompanyAction->setToolTip( tr( "Record notes from MIDI-device/channel-piano while playing song or BB track" ) );
- m_toggleStepRecordingAction->setToolTip( tr( "Record notes from MIDI-device/channel-piano, one step at the time" ) );
- m_stopAction->setToolTip( tr( "Stop playing of current pattern (Space)" ) );
- DropToolBar *notesActionsToolBar = addDropToolBarToTop( tr( "Edit actions" ) );
- // init edit-buttons at the top
- ActionGroup* editModeGroup = new ActionGroup( this );
- QAction* drawAction = editModeGroup->addAction( embed::getIconPixmap( "edit_draw" ), tr( "Draw mode (Shift+D)" ) );
- QAction* eraseAction = editModeGroup->addAction( embed::getIconPixmap( "edit_erase" ), tr("Erase mode (Shift+E)" ) );
- QAction* selectAction = editModeGroup->addAction( embed::getIconPixmap( "edit_select" ), tr( "Select mode (Shift+S)" ) );
- QAction* pitchBendAction = editModeGroup->addAction( embed::getIconPixmap( "automation" ), tr("Pitch Bend mode (Shift+T)" ) );
- drawAction->setChecked( true );
- drawAction->setShortcut( Qt::SHIFT | Qt::Key_D );
- eraseAction->setShortcut( Qt::SHIFT | Qt::Key_E );
- selectAction->setShortcut( Qt::SHIFT | Qt::Key_S );
- pitchBendAction->setShortcut( Qt::SHIFT | Qt::Key_T );
- connect( editModeGroup, SIGNAL( triggered( int ) ), m_editor, SLOT( setEditMode( int ) ) );
- QAction* quantizeAction = new QAction(embed::getIconPixmap( "quantize" ), tr( "Quantize" ), this );
- connect( quantizeAction, SIGNAL( triggered() ), m_editor, SLOT( quantizeNotes() ) );
- notesActionsToolBar->addAction( drawAction );
- notesActionsToolBar->addAction( eraseAction );
- notesActionsToolBar->addAction( selectAction );
- notesActionsToolBar->addAction( pitchBendAction );
- notesActionsToolBar->addSeparator();
- notesActionsToolBar->addAction( quantizeAction );
- // Copy + paste actions
- DropToolBar *copyPasteActionsToolBar = addDropToolBarToTop( tr( "Copy paste controls" ) );
- QAction* cutAction = new QAction(embed::getIconPixmap( "edit_cut" ),
- tr( "Cut (%1+X)" ).arg(UI_CTRL_KEY), this );
- QAction* copyAction = new QAction(embed::getIconPixmap( "edit_copy" ),
- tr( "Copy (%1+C)" ).arg(UI_CTRL_KEY), this );
- QAction* pasteAction = new QAction(embed::getIconPixmap( "edit_paste" ),
- tr( "Paste (%1+V)" ).arg(UI_CTRL_KEY), this );
- cutAction->setShortcut( Qt::CTRL | Qt::Key_X );
- copyAction->setShortcut( Qt::CTRL | Qt::Key_C );
- pasteAction->setShortcut( Qt::CTRL | Qt::Key_V );
- connect( cutAction, SIGNAL( triggered() ), m_editor, SLOT( cutSelectedNotes() ) );
- connect( copyAction, SIGNAL( triggered() ), m_editor, SLOT( copySelectedNotes() ) );
- connect( pasteAction, SIGNAL( triggered() ), m_editor, SLOT( pasteNotes() ) );
- copyPasteActionsToolBar->addAction( cutAction );
- copyPasteActionsToolBar->addAction( copyAction );
- copyPasteActionsToolBar->addAction( pasteAction );
- DropToolBar *timeLineToolBar = addDropToolBarToTop( tr( "Timeline controls" ) );
- m_editor->m_timeLine->addToolButtons( timeLineToolBar );
- // -- Note modifier tools
- // Toolbar
- QToolButton * noteToolsButton = new QToolButton(m_toolBar);
- noteToolsButton->setIcon(embed::getIconPixmap("tool"));
- noteToolsButton->setPopupMode(QToolButton::InstantPopup);
- // Glue
- QAction * glueAction = new QAction(embed::getIconPixmap("glue"),
- tr("Glue"), noteToolsButton);
- connect(glueAction, SIGNAL(triggered()), m_editor, SLOT(glueNotes()));
- glueAction->setShortcut( Qt::SHIFT | Qt::Key_G );
- // Razor
- QAction * razorAction = new QAction(embed::getIconPixmap("razor"),
- tr("Razor"), noteToolsButton);
- connect(razorAction, &QAction::triggered, m_editor, &PianoRoll::setRazorAction);
- razorAction->setShortcut( Qt::SHIFT | Qt::Key_R );
- noteToolsButton->addAction(glueAction);
- noteToolsButton->addAction(razorAction);
- notesActionsToolBar->addWidget(noteToolsButton);
- addToolBarBreak();
- DropToolBar *zoomAndNotesToolBar = addDropToolBarToTop( tr( "Zoom and note controls" ) );
- QLabel * zoom_lbl = new QLabel( m_toolBar );
- zoom_lbl->setPixmap( embed::getIconPixmap( "zoom_x" ) );
- m_zoomingComboBox = new ComboBox( m_toolBar );
- m_zoomingComboBox->setModel( &m_editor->m_zoomingModel );
- m_zoomingComboBox->setFixedSize( 64, ComboBox::DEFAULT_HEIGHT );
- m_zoomingComboBox->setToolTip( tr( "Horizontal zooming") );
- QLabel * zoom_y_lbl = new QLabel(m_toolBar);
- zoom_y_lbl->setPixmap(embed::getIconPixmap("zoom_y"));
- m_zoomingYComboBox = new ComboBox(m_toolBar);
- m_zoomingYComboBox->setModel(&m_editor->m_zoomingYModel);
- m_zoomingYComboBox->setFixedSize(64, ComboBox::DEFAULT_HEIGHT);
- m_zoomingYComboBox->setToolTip(tr("Vertical zooming"));
- // setup quantize-stuff
- QLabel * quantize_lbl = new QLabel( m_toolBar );
- quantize_lbl->setPixmap( embed::getIconPixmap( "quantize" ) );
- m_quantizeComboBox = new ComboBox( m_toolBar );
- m_quantizeComboBox->setModel( &m_editor->m_quantizeModel );
- m_quantizeComboBox->setFixedSize( 64, ComboBox::DEFAULT_HEIGHT );
- m_quantizeComboBox->setToolTip( tr( "Quantization") );
- // setup note-len-stuff
- QLabel * note_len_lbl = new QLabel( m_toolBar );
- note_len_lbl->setPixmap( embed::getIconPixmap( "note" ) );
- m_noteLenComboBox = new ComboBox( m_toolBar );
- m_noteLenComboBox->setModel( &m_editor->m_noteLenModel );
- m_noteLenComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
- m_noteLenComboBox->setToolTip( tr( "Note length") );
- // setup key-stuff
- m_keyComboBox = new ComboBox(m_toolBar);
- m_keyComboBox->setModel(&m_editor->m_keyModel);
- m_keyComboBox->setFixedSize(72, ComboBox::DEFAULT_HEIGHT);
- m_keyComboBox->setToolTip(tr("Key"));
- // setup scale-stuff
- QLabel * scale_lbl = new QLabel( m_toolBar );
- scale_lbl->setPixmap( embed::getIconPixmap( "scale" ) );
- m_scaleComboBox = new ComboBox( m_toolBar );
- m_scaleComboBox->setModel( &m_editor->m_scaleModel );
- m_scaleComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
- m_scaleComboBox->setToolTip( tr( "Scale") );
- // setup chord-stuff
- QLabel * chord_lbl = new QLabel( m_toolBar );
- chord_lbl->setPixmap( embed::getIconPixmap( "chord" ) );
- m_chordComboBox = new ComboBox( m_toolBar );
- m_chordComboBox->setModel( &m_editor->m_chordModel );
- m_chordComboBox->setFixedSize( 105, ComboBox::DEFAULT_HEIGHT );
- m_chordComboBox->setToolTip( tr( "Chord" ) );
- // -- Clear ghost pattern button
- m_clearGhostButton = new QPushButton( m_toolBar );
- m_clearGhostButton->setIcon( embed::getIconPixmap( "clear_ghost_note" ) );
- m_clearGhostButton->setToolTip( tr( "Clear ghost notes" ) );
- m_clearGhostButton->setEnabled( false );
- connect( m_clearGhostButton, SIGNAL( clicked() ), m_editor, SLOT( clearGhostPattern() ) );
- connect( m_editor, SIGNAL( ghostPatternSet( bool ) ), this, SLOT( ghostPatternSet( bool ) ) );
- // Wrap label icons and comboboxes in a single widget so when
- // the window is resized smaller in width it hides both
- QWidget * zoom_widget = new QWidget();
- QHBoxLayout * zoom_hbox = new QHBoxLayout();
- zoom_hbox->setContentsMargins(0, 0, 0, 0);
- zoom_hbox->addWidget(zoom_lbl);
- zoom_hbox->addWidget(m_zoomingComboBox);
- zoom_widget->setLayout(zoom_hbox);
- zoomAndNotesToolBar->addWidget(zoom_widget);
- QWidget * zoomY_widget = new QWidget();
- QHBoxLayout * zoomY_hbox = new QHBoxLayout();
- zoomY_hbox->setContentsMargins(0, 0, 0, 0);
- zoomY_hbox->addWidget(zoom_y_lbl);
- zoomY_hbox->addWidget(m_zoomingYComboBox);
- zoomY_widget->setLayout(zoomY_hbox);
- zoomAndNotesToolBar->addWidget(zoomY_widget);
- QWidget * quantize_widget = new QWidget();
- QHBoxLayout * quantize_hbox = new QHBoxLayout();
- quantize_hbox->setContentsMargins(0, 0, 0, 0);
- quantize_hbox->addWidget(quantize_lbl);
- quantize_hbox->addWidget(m_quantizeComboBox);
- quantize_widget->setLayout(quantize_hbox);
- zoomAndNotesToolBar->addSeparator();
- zoomAndNotesToolBar->addWidget(quantize_widget);
- QWidget * note_widget = new QWidget();
- QHBoxLayout * note_hbox = new QHBoxLayout();
- note_hbox->setContentsMargins(0, 0, 0, 0);
- note_hbox->addWidget(note_len_lbl);
- note_hbox->addWidget(m_noteLenComboBox);
- note_widget->setLayout(note_hbox);
- zoomAndNotesToolBar->addSeparator();
- zoomAndNotesToolBar->addWidget(note_widget);
- QWidget * scale_widget = new QWidget();
- QHBoxLayout * scale_hbox = new QHBoxLayout();
- scale_hbox->setContentsMargins(0, 0, 0, 0);
- scale_hbox->addWidget(scale_lbl);
- // Add the key selection between scale label and key
- scale_hbox->addWidget(m_keyComboBox);
- scale_hbox->addWidget(m_scaleComboBox);
- scale_widget->setLayout(scale_hbox);
- zoomAndNotesToolBar->addSeparator();
- zoomAndNotesToolBar->addWidget(scale_widget);
- QWidget * chord_widget = new QWidget();
- QHBoxLayout * chord_hbox = new QHBoxLayout();
- chord_hbox->setContentsMargins(0, 0, 0, 0);
- chord_hbox->addWidget(chord_lbl);
- chord_hbox->addWidget(m_chordComboBox);
- chord_widget->setLayout(chord_hbox);
- zoomAndNotesToolBar->addSeparator();
- zoomAndNotesToolBar->addWidget(chord_widget);
- zoomAndNotesToolBar->addSeparator();
- zoomAndNotesToolBar->addWidget( m_clearGhostButton );
- // setup our actual window
- setFocusPolicy( Qt::StrongFocus );
- setFocus();
- setWindowIcon( embed::getIconPixmap( "piano" ) );
- setCurrentPattern( NULL );
- // Connections
- connect( m_editor, SIGNAL( currentPatternChanged() ), this, SIGNAL( currentPatternChanged() ) );
- connect( m_editor, SIGNAL( currentPatternChanged() ), this, SLOT( updateAfterPatternChange() ) );
- }
- const Pattern* PianoRollWindow::currentPattern() const
- {
- return m_editor->currentPattern();
- }
- void PianoRollWindow::setGhostPattern( Pattern* pattern )
- {
- m_editor->setGhostPattern( pattern );
- }
- void PianoRollWindow::setCurrentPattern( Pattern* pattern )
- {
- m_editor->setCurrentPattern( pattern );
- if ( pattern )
- {
- setWindowTitle( tr( "Piano-Roll - %1" ).arg( pattern->name() ) );
- connect( pattern->instrumentTrack(), SIGNAL( nameChanged() ), this, SLOT( updateAfterPatternChange()) );
- connect( pattern, SIGNAL( dataChanged() ), this, SLOT( updateAfterPatternChange() ) );
- }
- else
- {
- setWindowTitle( tr( "Piano-Roll - no pattern" ) );
- }
- }
- bool PianoRollWindow::isRecording() const
- {
- return m_editor->isRecording();
- }
- int PianoRollWindow::quantization() const
- {
- return m_editor->quantization();
- }
- void PianoRollWindow::play()
- {
- m_editor->play();
- }
- void PianoRollWindow::stop()
- {
- m_editor->stop();
- }
- void PianoRollWindow::record()
- {
- stopStepRecording(); //step recording mode is mutually exclusive with other record modes
- m_editor->record();
- }
- void PianoRollWindow::recordAccompany()
- {
- stopStepRecording(); //step recording mode is mutually exclusive with other record modes
- m_editor->recordAccompany();
- }
- void PianoRollWindow::toggleStepRecording()
- {
- if(isRecording())
- {
- // step recording mode is mutually exclusive with other record modes
- // stop them before starting step recording
- stop();
- }
- m_editor->toggleStepRecording();
- updateStepRecordingIcon();
- }
- void PianoRollWindow::stopRecording()
- {
- m_editor->stopRecording();
- }
- void PianoRollWindow::reset()
- {
- m_editor->reset();
- }
- void PianoRollWindow::saveSettings( QDomDocument & doc, QDomElement & de )
- {
- if( !m_editor->ghostNotes().empty() )
- {
- QDomElement ghostNotesRoot = doc.createElement( "ghostnotes" );
- for( Note *note : m_editor->ghostNotes() )
- {
- QDomElement ghostNoteNode = doc.createElement( "ghostnote" );
- ghostNoteNode.setAttribute( "len", note->length() );
- ghostNoteNode.setAttribute( "key", note->key() );
- ghostNoteNode.setAttribute( "pos", note->pos() );
- ghostNotesRoot.appendChild(ghostNoteNode);
- }
- de.appendChild( ghostNotesRoot );
- }
- if (m_editor->m_markedSemiTones.length() > 0)
- {
- QDomElement markedSemiTonesRoot = doc.createElement("markedSemiTones");
- for (int ix = 0; ix < m_editor->m_markedSemiTones.size(); ++ix)
- {
- QDomElement semiToneNode = doc.createElement("semiTone");
- semiToneNode.setAttribute("key", m_editor->m_markedSemiTones.at(ix));
- markedSemiTonesRoot.appendChild(semiToneNode);
- }
- de.appendChild(markedSemiTonesRoot);
- }
- MainWindow::saveWidgetState( this, de );
- }
- void PianoRollWindow::loadSettings( const QDomElement & de )
- {
- m_editor->loadGhostNotes( de.firstChildElement("ghostnotes") );
- m_editor->loadMarkedSemiTones(de.firstChildElement("markedSemiTones"));
- MainWindow::restoreWidgetState( this, de );
- // update margins here because we're later in the startup process
- // We can't earlier because everything is still starting with the
- // WHITE_KEY_WIDTH default
- QMargins qm = m_editor->m_stepRecorderWidget.margins();
- qm.setLeft(m_editor->m_whiteKeyWidth);
- m_editor->m_stepRecorderWidget.setMargins(qm);
- m_editor->m_timeLine->setXOffset(m_editor->m_whiteKeyWidth);
- }
- QSize PianoRollWindow::sizeHint() const
- {
- return { INITIAL_PIANOROLL_WIDTH, INITIAL_PIANOROLL_HEIGHT };
- }
- bool PianoRollWindow::hasFocus() const
- {
- return m_editor->hasFocus();
- }
- void PianoRollWindow::updateAfterPatternChange()
- {
- patternRenamed();
- updateStepRecordingIcon(); //pattern change turn step recording OFF - update icon accordingly
- }
- void PianoRollWindow::patternRenamed()
- {
- if ( currentPattern() )
- {
- setWindowTitle( tr( "Piano-Roll - %1" ).arg( currentPattern()->name() ) );
- }
- else
- {
- setWindowTitle( tr( "Piano-Roll - no pattern" ) );
- }
- }
- void PianoRollWindow::ghostPatternSet( bool state )
- {
- m_clearGhostButton->setEnabled( state );
- }
- void PianoRollWindow::focusInEvent( QFocusEvent * event )
- {
- // when the window is given focus, also give focus to the actual piano roll
- m_editor->setFocus( event->reason() );
- }
- void PianoRollWindow::stopStepRecording()
- {
- if(m_editor->isStepRecording())
- {
- m_editor->toggleStepRecording();
- updateStepRecordingIcon();
- }
- }
- void PianoRollWindow::updateStepRecordingIcon()
- {
- if(m_editor->isStepRecording())
- {
- m_toggleStepRecordingAction->setIcon(embed::getIconPixmap("record_step_on"));
- }
- else
- {
- m_toggleStepRecordingAction->setIcon(embed::getIconPixmap("record_step_off"));
- }
- }
|