Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

typica.w 727KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712
  1. % Instructions for generating source code and documentation
  2. % Step 1. Convert metapost diagrams into PDF documents
  3. % $ mptopdf pipes.mp ; epstopdf pipes.ps
  4. % $ mptopdf roast.mp ; epstopdf roast.ps
  5. % $ mptopdf search.mp ; epstopdf search.ps
  6. % Step 2. Weave and typeset
  7. % $ cweave typica
  8. % $ pdftex typica
  9. % Step 3. Tangle and moc
  10. % $ ctangle typica ; mv typica.c typica.cpp
  11. % $ moc typica.cpp > moc_typica.cpp
  12. %
  13. % If you have trouble compiling, check to make sure the required headers are in
  14. % your header search path and check to make sure the required libraries are
  15. % linked. If using qmake to generate a project file, remember to add the
  16. % following lines to your .pro file:
  17. % QT += xml
  18. % QT += script
  19. % Document style instructions
  20. \input graphicx.tex
  21. \mark{\noexpand\nullsec0{A Note on Notation}}
  22. \def\pn{Typica}
  23. \def\filebase{typica}
  24. \def\version{1.9.1 \number\year-\number\month-\number\day}
  25. \def\years{2007--2018}
  26. \def\title{\pn{} (Version \version)}
  27. \newskip\dangerskipb
  28. \newskip\dangerskip
  29. \dangerskip=20pt
  30. \dangerskipb=42pt
  31. \def\hang{\hangindent\dangerskip}
  32. \def\hangb{\hangindent\dangerskipb}
  33. \font\manual=manfnt at 12pt
  34. \def\danbend{{\manual\char127}}
  35. \def\datanger{\medbreak\begingroup\clubpenalty=10000
  36. \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
  37. \hbox to0pt{\hskip-3.5pc\danbend\hfill}}
  38. \outer\def\danger{\datanger}%
  39. %
  40. \def\datangerb{\medbreak\begingroup\clubpenalty=10000
  41. \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
  42. \hbox to0pt{\hskip-3.5pc\danbend\hfill}}
  43. \outer\def\dangerb{\datangerb}
  44. \def\endanger{\medskip}
  45. \def\nullsec{\S1}
  46. \def\lheader{\mainfont\the\pageno\kern1pc(\topsecno)\eightrm
  47. \qquad\grouptitle\hfill\title}
  48. \def\rheader{\eightrm\title\hfill\grouptitle\qquad\mainfont
  49. (\topsecno)\kern1pc\the\pageno}
  50. \def\botofcontents{\vfill
  51. \noindent Copyright \copyright\ \years~Neal Evan Wilson
  52. \bigskip\noindent Permission is hereby granted, free of charge, to any
  53. person obtaining a copy of this software and associated documentation files
  54. (the ``Software''), to deal in the Software without restriction, including
  55. without limitation the rights to use, copy, modify, merge, publish,
  56. distribute, sublicense, and/or sell copies of the Software, and to permit
  57. persons to whom the Software is furnished to do so, subject to the following
  58. conditions:\medskip
  59. The above copyright notice and this permission notice shall be included in
  60. all copies or substantial portions of the Software.\medskip
  61. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  62. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  63. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  64. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  65. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  66. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  67. IN THE SOFTWARE.
  68. \bigskip\noindent Parts of \pn{} are from QextSerialPort which is used under the
  69. MIT license as follows:
  70. \bigskip\noindent Copyright \copyright\ 2000--2003 Wayne Roth
  71. \noindent Copyright \copyright\ 2004--2007 Stefan Sander
  72. \noindent Copyright \copyright\ 2007 Michal Policht
  73. \noindent Copyright \copyright\ 2008 Brandon Fosdick
  74. \noindent Copyright \copyright\ 2009--2010 Liam Staskawicz
  75. \noindent Copyright \copyright\ 2011 Debao Zhang
  76. \bigskip\noindent Web: http://code.google.com/p/qextserialport/
  77. \bigskip\noindent Permission is hereby granted, free of charge, to any person obtaining
  78. a copy of this software and associated documentation files (the
  79. ``Software''), to deal in the Software without restriction, including
  80. without limitation the rights to use, copy, modify, merge, publish,
  81. distribute, sublicense, and/or sell copies of the Software, and to
  82. permit persons to whom the Software is furnished to do so, subject to
  83. the following conditions:
  84. The above copyright notice and this permission notice shall be
  85. included in all copies or substantial portions of the Software.
  86. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  87. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  88. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  89. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  90. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  91. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  92. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  93. }
  94. \let\K=\leftarrow
  95. \def\CPLUSPLUS/{{%
  96. \mc C{\hbox{\kern.5pt\raise1pt\hbox{\sevenrm+\kern-1pt+}\kern.5pt}}
  97. \spacefactor1000}}
  98. \def\PP{\uparrow}
  99. \def\MM{\downarrow}
  100. \newbox\DCBox
  101. \setbox\DCBox=\hbox{$\in$}
  102. \def\DC{\copy\DCBox}
  103. \newbox\MODbox \setbox\MODbox=\hbox{\eightrm MOD}
  104. \def\MOD{\mathbin{\copy\MODbox}}
  105. % Title page
  106. \font\authorfont=cmr12
  107. \null\vfill
  108. \centerline{\titlefont \pn}
  109. \vskip 18pt\centerline{(Version \version)}
  110. \vskip 24pt\centerline{\authorfont Neal Evan Wilson}
  111. \vfill
  112. \titletrue\eject\hbox to 0pt{}
  113. \pageno=0 \titletrue\eject
  114. \secpagedepth=1
  115. % Convenience macros
  116. \def\newline{\vskip\baselineskip}
  117. \def\cweb{\.{CWEB}}
  118. \def\web{\.{WEB}}
  119. \newcount\footnotenumber
  120. \def\nfnote{\global\advance\footnotenumber by 1
  121. \footnote{$^{\the\footnotenumber}$}}
  122. % Listing macro from The TeXBook. See page 381 for an explaination.
  123. \def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
  124. \newcount\lineno
  125. \def\setupverbatim{\tt \lineno=0
  126. \def\par{\leavevmode\endgraf} \catcode`\`=\active
  127. \obeylines \uncatcodespecials \obeyspaces
  128. \everypar{\advance\lineno by1 \llap{\sevenrm\the\lineno\ \ }}}
  129. {\obeyspaces\global\let =\ }
  130. \def\listing#1{\par\begingroup\setupverbatim\input#1 \endgroup}
  131. % Javascript chunk handling
  132. \def\jsfile#1#2{\Y\B\4\X\secno:\.{#1}\X${}\E{}\6$\par
  133. \listing{#2}}
  134. % Type formatting
  135. @s QTime int
  136. @s QMetaType int
  137. @s DAQ int
  138. @s Channel int
  139. @s QString int
  140. @s QObject int
  141. @s QThread int
  142. @s DAQImplementation int
  143. @s QVector int
  144. @s TaskHandle int
  145. @s qint32 int
  146. @s int32 int
  147. @s QMessageBox int
  148. @s QLCDNumber int
  149. @s QWidget int
  150. @s AnnotationButton int
  151. @s AnnotationSpinBox int
  152. @s QPushButton int
  153. @s QTimer int
  154. @s QAction int
  155. @s QApplication int
  156. @s PackLayout int
  157. @s QLayout int
  158. @s QLayoutItem int
  159. @s QRect int
  160. @s QList int
  161. @s QSize int
  162. @s QGraphicsScene int
  163. @s SceneButton int
  164. @s QGraphicsSceneMouseEvent int
  165. @s QPoint int
  166. @s true const
  167. @s false const
  168. @s QGraphicsView int
  169. @s QGraphicsTextItem int
  170. @s QFrame int
  171. @s QPaintDevice int
  172. @s QColor int
  173. @s QBrush int
  174. @s QHash int
  175. @s QPointF int
  176. @s QGraphicsLineItem int
  177. @s MeasurementModel int
  178. @s QTableView int
  179. @s QVariant int
  180. @s QAbstractItemView int
  181. @s QAbstractItemModel int
  182. @s QStringList int
  183. @s QModelIndex int
  184. @s MeasurementList int
  185. @s QVariantList int
  186. @s QSplitter int
  187. @s QHBoxLayout int
  188. @s QMainWindow int
  189. @s QCoreApplication int
  190. @s QSettings int
  191. @s QMenu int
  192. @s QCloseEvent int
  193. @s LogEditWindow int
  194. @s QFile int
  195. @s QFileInfo int
  196. @s QDir int
  197. @s QXmlStreamWriter int
  198. @s QXmlStreamReader int
  199. @s QIODevice int
  200. @s QLabel int
  201. @s QTimeEdit int
  202. @s QSpinBox int
  203. @s QDoubleSpinBox int
  204. @s ThermocoupleType int
  205. @s TemperatureUnits int
  206. @s Qt int
  207. @s emit throw
  208. @s TemperatureDisplay int
  209. @s ZeroEmitter int
  210. @s MeasurementAdapter int
  211. @s GraphView int
  212. @s ZoomLog int
  213. @s TimerDisplay int
  214. @s QBoxLayout int
  215. @s WidgetDecorator int
  216. @s XMLInput int
  217. @s XMLOutput int
  218. @s CSVOutput int
  219. @s QTextStream int
  220. @s QTranslator int
  221. @s QLocale int
  222. @s Application int
  223. @s QScriptContext int
  224. @s QScriptEngine int
  225. @s QScriptEngineDebugger int
  226. @s QScriptValue int
  227. @s FakeDAQ int
  228. @s QMenuBar int
  229. @s QKeySequence int
  230. @s QFileDialog int
  231. @s Measurement int
  232. @s Date int
  233. @s QLibrary int
  234. @s daqfp int
  235. @s QResizeEvent int
  236. @s QVBoxLayout int
  237. @s QByteArray int
  238. @s QSqlDatabase int
  239. @s QComboBox int
  240. @s QXmlStreamAttribute int
  241. @s QSqlQuery int
  242. @s QLineEdit int
  243. @s QDoubleValidator int
  244. @s QIntValidator int
  245. @s QTextEdit int
  246. @s QStandardItemModel int
  247. @s QValidator int
  248. @s QMap int
  249. @s QDomElement int
  250. @s QDomNodeList int
  251. @s QDomNode int
  252. @s QStack int
  253. @s QDomDocument int
  254. @s QDomNamedNodeMap int
  255. @s QFormLayout int
  256. @s QAbstractButton int
  257. @s QAbstractScrollArea int
  258. @s SqlComboBox int
  259. @s QUuid int
  260. @s SqlComboBoxDelegate int
  261. @s QItemDelegate int
  262. @s SqlConnectionSetup int
  263. @s QDialog int
  264. @s QCheckBox int
  265. @s SaltModel int
  266. @s QStyleOptionViewItem int
  267. @s QBuffer int
  268. @s QDateEdit int
  269. @s QCalendarWidget int
  270. @s QDate int
  271. @s QFocusEvent int
  272. @s QGridLayout int
  273. @s QScrollArea int
  274. @s QSqlQueryModel int
  275. @s QSqlRecord int
  276. @s QSqlResult int
  277. @s SqlQueryConnection int
  278. @s QFont int
  279. @s SqlQueryView int
  280. @s QTextDocument int
  281. @s QTextCursor int
  282. @s QTextFrame int
  283. @s ReportTable int
  284. @s QTextTable int
  285. @s QTextTableFormat int
  286. @s QTextFrameFormat int
  287. @s QTextTableCell int
  288. @s QPrinter int
  289. @s QPrintDialog int
  290. @s QSqlError int
  291. @s FormArray int
  292. @s QRegExp int
  293. @s QRegExpValidator int
  294. @s QDomDocumentFragment int
  295. @s QStackedLayout int
  296. @s QMouseEvent int
  297. @s QGraphicsPolygonItem int
  298. @s QPolygonF int
  299. @s QGraphicsPathItem int
  300. @s QPainterPath int
  301. @s QXmlQuery int
  302. @s QGraphicsItem int
  303. @s QWebView int
  304. @s QUrl int
  305. @s QShowEvent int
  306. @s QDateTimeEdit int
  307. @s ThresholdDetector int
  308. @s EdgeDirection int
  309. @s DeviceTreeModelNode int
  310. @s QMetaObject int
  311. @s QTreeView int
  312. @s QToolButton int
  313. @s QextPortInfo int
  314. @s QextSerialEnumerator int
  315. @s QMetaEnum int
  316. @s quint16 int
  317. @s QextSerialPort int
  318. @s QGroupBox int
  319. @s QVariantMap int
  320. @s QIcon int
  321. @s QFileInfoList int
  322. @s QMetaMethod int
  323. @f error normal
  324. @f line normal
  325. @f signals public
  326. @f slots int
  327. @f qRegisterMetaType make_pair
  328. @f READ TeX
  329. @f WRITE TeX
  330. @f tr TeX
  331. @f this TeX
  332. @f foreach while
  333. @f qobject_cast make_pair
  334. @f t1 TeX
  335. @f t2 TeX
  336. @f AppInstance TeX
  337. @f getself make_pair
  338. @f TYPE TeX
  339. @f argument make_pair
  340. @f toScriptValue make_pair
  341. @f arg1 TeX
  342. @f arg2 TeX
  343. @f arg3 TeX
  344. @f arg4 TeX
  345. @f findChild make_pair
  346. @f qscriptvalue_cast make_pair
  347. \def\READ{\kern4pt{\tt READ}\kern4pt}
  348. \def\WRITE{\kern4pt{\tt WRITE}\kern4pt}
  349. \def\tr{\delta}
  350. \def\this{\forall}
  351. \def\t#1{t_{#1}}
  352. \def\AppInstance{\.{AppInstance}}
  353. \def\TYPE{\cal T\kern1pt}
  354. \def\arg#1{arg_{#1}}
  355. % Document
  356. @** A Note on Notation.
  357. \noindent As noted by Falkoff and Iverson\nfnote{A.~D.~Falkoff and
  358. K.~E.~Iverson, ``The Design of APL'' (1973)}~there is little need to limit the
  359. typography used to represent a computer program in print. The printed code of
  360. \pn{} uses a number of notations that I have found useful in making clear the
  361. intent of the code. For example, a common mistake in \CPLUSPLUS/ \kern-0.5em
  362. code is the confusion of assignment ({\tt =}) with a test for equality
  363. ({\tt ==}). The \web{} convention of using |=| for assignment and |==| to test
  364. for equality makes such errors obvious at a glance. A list of special symbols
  365. and the equivalent \CPLUSPLUS/text is provided in Table \secno. Most of these
  366. symbols should be familiar\nfnote{The {\tt NULL} symbol is a break with the
  367. conventions of most Qt applications. According the the \CPLUSPLUS/standard, |0|
  368. is both an integer constant and a null pointer constant. Most programs using Qt
  369. use |0| in place of any name for the null pointer, however conceptually these
  370. are two very different things. The notation chosen here was used by Knuth for
  371. similar purposes and seems to have worked well there.}.
  372. \medskip
  373. \settabs 9 \columns
  374. \+&&&{\tt =}&|=|&Assignment\cr
  375. \+&&&{\tt --}&|--|&Decrement\cr
  376. \+&&&{\tt ==}&|==|&Equality Test\cr
  377. \+&&&{\tt >=}&|>=|&Greater or Equal Test\cr
  378. \+&&&{\tt ++}&|++|&Increment\cr
  379. \+&&&{\tt !=}&|!=|&Inequality Test\cr
  380. \+&&&{\tt <=}&|<=|&Less or Equal Test\cr
  381. \+&&&{\tt \char'046\char'046}&$\land$&Logical AND\cr
  382. \+&&&{\tt \char'174\char'174}&$\lor$&Logical OR\cr
  383. \+&&&{\tt ::}&|::|&Member of\cr
  384. \+&&&{\tt !}&|!|&Negation\cr
  385. \+&&&{\tt NULL}&|NULL|&Null Pointer\cr
  386. \+&&&{\tt this}&|this|&Object\cr
  387. \+&&&{\tt \%}&|%|&Remainder\cr
  388. \+&&&{\tt tr()}&|tr()|&Translate\cr
  389. \smallskip
  390. \centerline{Table \secno: Special Characters In \pn}
  391. \medskip
  392. Reserved words are set in bold face. As some of these reserved words are also
  393. the names of types, type names that are not specified in \CPLUSPLUS/are also
  394. set in bold face. Type placeholders in template definitions, however, are set in
  395. caligraphic capitals to emphasize that it will be replaced with a real type at
  396. compile time. Variables and class members are set in italics, character strings
  397. are set in a typewriter style with visible spaces. Macro names are also set in
  398. typewriter style. Numeric constants and plain text comments are set in an
  399. upright roman style. Comments containing \CEE/ or mathematics are styled as
  400. such. Code that will be interpreted by the ECMA-262 host environment has no
  401. pretty printing.
  402. \danger With apologies to prof.~Knuth\nfnote{This symbol was introduced in
  403. {\underbar{Computers~\char'046~Typesetting}}@q'@> (Knuth, 1984) to point out material
  404. that is for ``wizards only.'' It seems to be as appropriate a symbol as any to
  405. point out the darker corners of this program.}, code that is known to be
  406. potentially buggy is flagged with a dangerous bend sign. Some of this code is
  407. buggy due to issues with the code \pn{} depends on, others are things that
  408. should be fixed in \pn{}. Of course, there may also be bugs that have not yet
  409. been noticed or have not been attached to a particular block of code.\endanger
  410. A basic familiarity with literate programming techniques (particularly the
  411. conventions of \cweb{}), Qt, and \CPLUSPLUS/is recommended before altering the
  412. program, but an effort has been made to keep the program understandable for
  413. those who are only interested in studying it.
  414. @** Introduction.
  415. \noindent A common tool in the craft of coffee roasting is the data logger.
  416. Perhaps the most commonly used of these fall into the category of manual data
  417. loggers which require the roaster to use paper and a writing utensil,
  418. periodically recording measurements and noting control changes and observations
  419. of interest as needed.
  420. While there are many benefits to recording roast data\nfnote{Torrey Lee, Stephan
  421. Diedrich, Carl Staub, and Jack Newall, ``How to Obtain Excellence with Drum
  422. Roasters'' (2002) {\it Specialty Coffee Association of America 14$^{th}$ Annual
  423. Conference and Exhibition}}, there are a number of limitations to the manual
  424. approach; maintaining the records in a useful order is time consuming and error
  425. prone, it is difficult to work with aggregates of such records, and the
  426. attention required to log the data by hand can distract from the roasting. Using
  427. a computer with automatic data logging software designed with coffee roasting in
  428. mind can reduce or eliminate these deficiencies. \pn{} is one such program.
  429. The file {\tt \filebase.w} contains both \CPLUSPLUS/source code and the
  430. documentation for that code. This file is intended to be processed by
  431. \cweb\nfnote{Donald E. Knuth and Silvio Levy, ``The \cweb{} System of Structured
  432. Documentation'' (1994)}~to produce source code for your compiler and plain
  433. \TeX{}\nfnote{\TeX{} (pronounced $\tau\epsilon\chi$) is a trademark of the
  434. American Mathematical Society.} documentation that can be used to generate a PDF
  435. document for gorgeous printable documentation. These generated files may have
  436. been distributed with your copy of {\tt \filebase.w} for convenience.
  437. Changes to the program can be made in three ways. \cweb{} provides a patching
  438. mechanism which can be used to experiment with the code without risk of
  439. clobbering it. Instructions for the construction of such a change file can be
  440. found in the \cweb{} documentation. Adding the name of the change file to the
  441. invocation of {\tt ctangle} and {\tt cweave} will incorporate that change
  442. seamlessly in both source and documentation files. A section is provided at the
  443. end of this program for use with this mechanism in the case that new sections
  444. must be added. Another way to create persistent modifications is to alter
  445. {\tt \filebase.w} however this may make it more difficult to use changes with
  446. future versions of the software. Changes should not be made to
  447. {\tt \filebase.cpp} if these changes are expected to persist. Finally, it is
  448. possible to make many changes to how the program looks and behaves by creating a
  449. new configuration document for the program to load. Modifications made in this
  450. way do not even require recompiling the software. Examples that can serve as a
  451. starting point for such customizations are provided with \pn{}.
  452. \pn{} is a work in progress. There are several portions of the documentation
  453. that contain suggestions for future improvement. These notes provide clues for
  454. my future development plans. Naturally, if you have needs which are not quite
  455. addressed by this program, you should feel free to modify the code to suit your
  456. needs. Hopefully this will be easy to do.
  457. In the spirit of Benjamin Franklin\nfnote{``\dots as we enjoy great advantages
  458. from the inventions of others, we should be glad of an opportunity to serve
  459. others by any invention of ours; and this we should do freely and
  460. generously.''
  461. --- Benjamin Franklin, \underbar{The Private Life of the Late
  462. Benjamin Franklin, LL.D.~Originally
  463. Written By Himself, And Now}\par\noindent
  464. \underbar{Translated From The French} (1793)}, \pn{} is shared
  465. with minimal restriction (see the license after the table of contents for legal
  466. requirements). Libraries used by \pn{} may have other restrictions. Before
  467. undertaking to distribute a binary created from this code, you may want to
  468. either determine your rights with regard to these libraries or modify the
  469. program to remove them.
  470. As CWEB generates files with the wrong extension, we leave the default
  471. generated file empty.
  472. @c
  473. /* Nothing to see here. */
  474. @ The following is an overview of the structure of \pn:
  475. @(typica.cpp@>=
  476. #define PROGRAM_NAME "Typica"
  477. @<Header files to include@>@/
  478. @<Additional type definitions@>@/
  479. @<Additional function prototypes@>@/
  480. @<Class declarations@>@/
  481. @<Additional functions@>@/
  482. @<Function prototypes for scripting@>@/
  483. @<Logging function prototype@>@/
  484. @<Class implementations@>@/
  485. @<Functions for scripting@>@/
  486. @<Logging function implementation@>@/
  487. @<The main program@>
  488. #include "moc_typica.cpp"
  489. @ \pn{} is made of a number of distinct classes.
  490. @<Class implementations@>=
  491. @<NodeInserter implementation@>@/
  492. @<Measurement implementation@>@/
  493. @<DAQ Implementation@>@/
  494. @<DataqSdkDevice implementation@>@/
  495. @<FakeDAQ Implementation@>@/
  496. @<Channel Implementation@>@/
  497. @<TemperatureDisplay Implementation@>@/
  498. @<MeasurementTimeOffset Implementation@>@/
  499. @<ZeroEmitter Implementation@>@/
  500. @<MeasurementAdapter Implementation@>@/
  501. @<GraphView Implementation@>@/
  502. @<ZoomLog Implementation@>@/
  503. @<MeasurementModel Implementation@>@/
  504. @<AnnotationButton Implementation@>@/
  505. @<AnnotationSpinBox Implementation@>@/
  506. @<TimerDisplay Implementation@>@/
  507. @<PackLayout Implementation@>@/
  508. @<SceneButton Implementation@>@/
  509. @<WidgetDecorator Implementation@>@/
  510. @<LogEditWindow Implementation@>@/
  511. @<XMLOutput Implementation@>@/
  512. @<XMLInput Implementation@>@/
  513. @<CSVOutput Implementation@>@/
  514. @<SaltModel Implementation@>@/
  515. @<SqlComboBox Implementation@>@/
  516. @<SqlComboBoxDelegate Implementation@>@/
  517. @<Application Implementation@>@/
  518. @<SqlConnectionSetup implementation@>@/
  519. @<SqlQueryView implementation@>@/
  520. @<SqlQueryConnection implementation@>@/
  521. @<ReportTable implementation@>@/
  522. @<FormArray implementation@>@/
  523. @<ScaleControl implementation@>@/
  524. @<IntensityControl implementation@>@/
  525. @<ThresholdDetector Implementation@>@/
  526. @<PortSelector implementation@>@/
  527. @<BaudSelector implementation@>@/
  528. @<ParitySelector implementation@>@/
  529. @<FlowSelector implementation@>@/
  530. @<StopSelector implementation@>@/
  531. @<ModbusConfigurator implementation@>@/
  532. @<ShortHexSpinBox implementation@>@/
  533. @<ModbusRTUDevice implementation@>@/
  534. @<DeviceTreeModelNode implementation@>@/
  535. @<DeviceTreeModel implementation@>@/
  536. @<BasicDeviceConfigurationWidget implementation@>@/
  537. @<DeviceConfigurationWindow implementation@>@/
  538. @<Ni9211TcConfWidget implementation@>@/
  539. @<NiDaqMxBase9211ConfWidget implementation@>@/
  540. @<NiDaqMxBaseDriverConfWidget implementation@>@/
  541. @<ReportAction implementation@>@/
  542. @<NumericDelegate implementation@>@/
  543. @<NiDaqMxDriverConfWidget implementation@>@/
  544. @<NiDaqMx9211ConfWidget implementation@>@/
  545. @<NiDaqMxTc01ConfWidget implementation@>@/
  546. @<ModbusRtuPortConfWidget implementation@>@/
  547. @<ModbusRtuDeviceConfWidget implementation@>@/
  548. @<ModbusRtuDeviceTPvConfWidget implementation@>@/
  549. @<ModbusRtuDeviceTSvConfWidget implementation@>@/
  550. @<RoasterConfWidget implementation@>@/
  551. @<AnnotationButtonConfWidget implementation@>@/
  552. @<NoteSpinConfWidget implementation@>@/
  553. @<LinearCalibrator Implementation@>@/
  554. @<LinearSplineInterpolator Implementation@>@/
  555. @<LinearSplineInterpolationConfWidget implementation@>@/
  556. @<TranslationConfWidget implementation@>@/
  557. @<FreeAnnotationConfWidget implementation@>@/
  558. @<RateOfChange implementation@>@/
  559. @<SettingsWindow implementation@>@/
  560. @<GraphSettingsWidget implementation@>@/
  561. @<DataqSdkDeviceConfWidget implementation@>@/
  562. @<SerialScaleConfWidget implementation@>@/
  563. @<ValueAnnotation implementation@>@/
  564. @<ValueAnnotationConfWidget implementation@>@/
  565. @<ModbusNG implementation@>@/
  566. @<ThresholdAnnotationConfWidget implementation@>@/
  567. @<Annotator implementation@>@/
  568. @ A few headers are required for various parts of \pn{}. These allow the use of
  569. various Qt modules.
  570. @<Header files to include@>=
  571. #include <QtCore>
  572. #include <QtGui>
  573. #include <QtScript>
  574. #include <QtScriptTools>
  575. #include <QtXml>
  576. #include <QtSql>
  577. #include <QtDebug>
  578. #include <QtXmlPatterns>
  579. #include <QtWebKit>
  580. #include <QtSvg>
  581. #include <QtNetwork>
  582. @ New code is being written in separate files in a long term effort to improve
  583. organization of the code. The result of this is that some additional headers
  584. are required here.
  585. @<Header files to include@>=
  586. #include "helpmenu.h"
  587. @** The Scripting Engine.
  588. \noindent The main enhancement of \pn{} version 1.1 is the introduction of a
  589. scriptable environment. This change allows people to easily customize \pn{}
  590. without having to alter the program code. Instead, the user interface and
  591. program data flow is set up with a small script that runs in an ECMA-262 host
  592. environment\nfnote{Standard ECMA-262, 3$^{\rm{rd}}$ Edition\par\hbox{\indent%
  593. \pdfURL{%
  594. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf}%
  595. {http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf}}}
  596. which requires
  597. significantly less expertise to modify than \pn{} itself. Such a scripting
  598. environment will be familiar to anybody with experience using JavaScript on web
  599. pages or ActionScript in Flash. \pn{}'@q'@>s configuration system was later updated
  600. to support running several script fragments found in an XML configuration
  601. document.
  602. Most of the application classes are available from the scripting environment.
  603. The functions that make this possible are presented along with the classes. A
  604. selection of classes provided by Qt are also available. These are presented
  605. here.
  606. This chunk provides two |QScriptValue| objects which are used in other sections
  607. appended to this chunk.
  608. @<Set up the scripting engine@>=
  609. QScriptEngine *engine = new QScriptEngine;
  610. QScriptValue constructor;
  611. QScriptValue value;
  612. @ A common task when working with objects created from a script is finding the
  613. object a method is called on from the current script context. The code for this
  614. is simple, but lengthy. This is shortened with the use of a template function
  615. that obtains the object in question and casts it to the appropriate type. If an
  616. incorrect type is specified, a null pointer or similarly invalid value will be
  617. returned.
  618. @<Functions for scripting@>=
  619. template<class TYPE> TYPE@, getself(QScriptContext *context)
  620. {
  621. TYPE@, self = qobject_cast<TYPE>(context->thisObject().toQObject());
  622. return self;
  623. }
  624. template<> QTime getself(QScriptContext *context)
  625. {
  626. QTime self = context->thisObject().toVariant().toTime();
  627. return self;
  628. }
  629. template<> QModelIndex getself(QScriptContext *context)
  630. {
  631. QModelIndex self = context->thisObject().toVariant().value<QModelIndex>();
  632. return self;
  633. }
  634. template<> QByteArray getself(QScriptContext *context)
  635. {
  636. QByteArray self = context->thisObject().toVariant().toByteArray();
  637. return self;
  638. }
  639. template<> SqlQueryConnection* getself(QScriptContext *context)
  640. {
  641. SqlQueryConnection *self =@|
  642. (SqlQueryConnection *)qscriptvalue_cast<void *>(context->thisObject());
  643. return self;
  644. }
  645. template<> QXmlQuery* getself(QScriptContext *context)
  646. {
  647. QXmlQuery *self =
  648. (QXmlQuery *)qscriptvalue_cast<void *>(context->thisObject());
  649. return self;
  650. }
  651. template<> QXmlStreamWriter* getself(QScriptContext *context)
  652. {
  653. QXmlStreamWriter *self = @|
  654. (QXmlStreamWriter *)qscriptvalue_cast<void *>(context->thisObject());
  655. return self;
  656. }
  657. template<> QXmlStreamReader* getself(QScriptContext *context)
  658. {
  659. QXmlStreamReader *self = @|
  660. (QXmlStreamReader *)qscriptvalue_cast<void *>(context->thisObject());
  661. return self;
  662. }
  663. @ Another common task is obtaining the arguments of a method call from the
  664. script context and casting these arguments to the proper type. This is once
  665. again done with templates.
  666. @<Functions for scripting@>=
  667. template<class TYPE> TYPE@, argument(int arg, QScriptContext *context)
  668. {
  669. TYPE@, argument = qobject_cast<TYPE>(context->argument(arg).toQObject());
  670. return argument;
  671. }
  672. template<> QString argument(int arg, QScriptContext *context)
  673. {
  674. return context->argument(arg).toString();
  675. }
  676. template<> QVariant argument(int arg, QScriptContext *context)
  677. {
  678. return context->argument(arg).toVariant();
  679. }
  680. template<> int argument(int arg, QScriptContext *context)
  681. {
  682. return context->argument(arg).toInt32();
  683. }
  684. template<> SqlQueryConnection* argument(int arg, QScriptContext *context)
  685. {
  686. return (SqlQueryConnection *)
  687. qscriptvalue_cast<void *>(context->argument(arg));
  688. }
  689. template<> QModelIndex argument(int arg, QScriptContext *context)
  690. {
  691. return qscriptvalue_cast<QModelIndex>(context->argument(arg));
  692. }
  693. template<> double argument(int arg, QScriptContext *context)
  694. {
  695. return (double)(context->argument(arg).toNumber());
  696. }
  697. template<> Units::Unit argument(int arg, QScriptContext *context)
  698. {
  699. return (Units::Unit)(context->argument(arg).toInt32());
  700. }
  701. template<> QByteArray argument(int arg, QScriptContext *context)
  702. {
  703. return qscriptvalue_cast<QByteArray>(context->argument(arg));
  704. }
  705. template<> bool argument(int arg, QScriptContext *context)
  706. {
  707. return context->argument(arg).toBool();
  708. }
  709. @ The scripting engine is informed of a number of classes defined elsewhere in
  710. the program. Code related to scripting these classes is grouped with the code
  711. implementing the classes. Additionally, there are several classes from Qt which
  712. are also made scriptable. These are detailed in the following sections.
  713. @* Exposing an Object Hierarchy to the Host Environment.
  714. \noindent While QtScript does a generally acceptable job of exposing
  715. information about objects that are available through the meta-object system,
  716. some methods require special handling in order to make them fully available to
  717. the host environment. Several functions are provided which provide a
  718. |QScriptValue| with these additional properties. The functions providing these
  719. properties also call other functions providing the properties of any base
  720. classes. In this way, any additional functionality provided to the host
  721. environment for a base class is also provided for any class that inherits that
  722. base class.
  723. For example, a |QBoxLayout| created in a script will have properties from
  724. |QLayout| which in turn brings in properties from |QObject| and |QLayoutItem|.
  725. A |QMainWindow| would bring in properties from |QWidget| which would bring in
  726. properties from |QObject|.
  727. Neither all methods nor all Qt classes are available from the host environment.
  728. When adding functionality to the host environment, there is a priority on
  729. classes and methods that are useful for \pn{}'@q'@>s intended purpose.
  730. @* Base Classes.
  731. \noindent There are a few classes that are base classes of classes exposed to
  732. the scripting engine. There is no need for the host environment to allow the
  733. creation of these base classes and there may not be methods that must be added
  734. as properties in derived classes, however stub functions are provided so that
  735. in the event that a method from one of these base classes is needed later, it
  736. can be added once for all derived classes.
  737. The first of these is |QObject|.
  738. @<Function prototypes for scripting@>=
  739. void setQObjectProperties(QScriptValue value, QScriptEngine *engine);
  740. QScriptValue QObject_setProperty(QScriptContext *context, QScriptEngine *engine);
  741. @ Attaching properties to a |QScriptValue| that wraps a |QObject| does not
  742. create a dynamic property on the underlying |QObject| by default. This can
  743. cause issues with certain interactions between script and native code. Rather
  744. than change every wrapper, we can instead expose a |setProperty()| method.
  745. @<Functions for scripting@>=
  746. void setQObjectProperties(QScriptValue value, QScriptEngine *engine)
  747. {
  748. value.setProperty("setProperty", engine->newFunction(QObject_setProperty));
  749. }
  750. QScriptValue QObject_setProperty(QScriptContext *context, QScriptEngine *)
  751. {
  752. QObject *self = getself<QObject *>(context);
  753. self->setProperty(argument<QString>(0, context).toUtf8().constData(),
  754. argument<QVariant>(1, context));
  755. return QScriptValue();
  756. }
  757. @ The same can be done for |QPaintDevice| and |QLayoutItem|.
  758. @<Function prototypes for scripting@>=
  759. void setQPaintDeviceProperties(QScriptValue value, QScriptEngine *engine);
  760. void setQLayoutItemProperties(QScriptValue value, QScriptEngine *engine);
  761. @ The implementations are similarly empty.
  762. @<Functions for scripting@>=
  763. void setQPaintDeviceProperties(QScriptValue, QScriptEngine *)
  764. {
  765. /* Nothing needs to be done here. */
  766. }
  767. void setQLayoutItemProperties(QScriptValue, QScriptEngine *)
  768. {
  769. /* Nothing needs to be done here. */
  770. }
  771. @* Timers.
  772. \noindent Some features in Typica require access to functionality similar to
  773. what |QTimer| provides from the host environment. This includes allowing
  774. script devices to periodically poll connected hardware and allowing a safety
  775. delay on profile translation.
  776. <@Function prototypes for scripting@>=
  777. void setQTimerProperties(QScriptValue value, QScriptEngine *engine);
  778. QScriptValue constructQTimer(QScriptContext *context, QScriptEngine *engine);
  779. @ The host environment is informed of the constructor.
  780. @<Set up the scripting engine@>=
  781. constructor = engine->newFunction(constructQTimer);
  782. value = engine->newQMetaObject(&QTimer::staticMetaObject, constructor);
  783. engine->globalObject().setProperty("Timer", value);
  784. @ Everything that we are interested in here is a signal, slot, or property so
  785. there is little else to do.
  786. @<Functions for scripting@>=
  787. void setQTimerProperties(QScriptValue value, QScriptEngine *engine)
  788. {
  789. setQObjectProperties(value, engine);
  790. }
  791. QScriptValue constructQTimer(QScriptContext *, QScriptEngine *engine)
  792. {
  793. QScriptValue object = engine->newQObject(new QTimer);
  794. setQTimerProperties(object, engine);
  795. return object;
  796. }
  797. @* Scripting QWidget.
  798. \noindent The first interesting class in this hierarchy is |QWidget|. This is
  799. mainly used as a base class for other widgets and in such a role it is not
  800. particularly interesting. It is, however, possible to apply a layout to a
  801. |QWidget| and use that to manage the size and position of one or more child
  802. widgets. A few functions are used to accomplish this.
  803. @<Function prototypes for scripting@>=
  804. void setQWidgetProperties(QScriptValue value, QScriptEngine *engine);
  805. QScriptValue constructQWidget(QScriptContext *context, QScriptEngine *engine);
  806. QScriptValue QWidget_setLayout(QScriptContext *context, QScriptEngine *engine);
  807. QScriptValue QWidget_activateWindow(QScriptContext *context,
  808. QScriptEngine *engine);
  809. @ The script constructor must be passed to the scripting engine.
  810. @<Set up the scripting engine@>=
  811. constructor = engine->newFunction(constructQWidget);
  812. value = engine->newQMetaObject(&QWidget::staticMetaObject, constructor);
  813. engine->globalObject().setProperty("QWidget", value);
  814. @ The constructor creates a script value, but uses another function to add
  815. properties that wrap methods we want to make available to subclasses. Note that
  816. properties of the base classes are added before properties of this class. This
  817. procedure ensures that properties added from base classes can be be replaced in
  818. subclasses.
  819. @<Functions for scripting@>=
  820. QScriptValue constructQWidget(QScriptContext *, QScriptEngine *engine)
  821. {
  822. QScriptValue object = engine->newQObject(new QWidget);
  823. setQWidgetProperties(object, engine);
  824. return object;
  825. }
  826. void setQWidgetProperties(QScriptValue value, QScriptEngine *engine)
  827. {
  828. setQObjectProperties(value, engine);
  829. setQPaintDeviceProperties(value, engine);
  830. value.setProperty("setLayout", engine->newFunction(QWidget_setLayout));
  831. value.setProperty("activateWindow",
  832. engine->newFunction(QWidget_activateWindow));
  833. }
  834. @ This just leaves the property implementations. |QWidget::setLayout()| takes
  835. one argument, a |QLayout| and returns |void|. This wrapper duplicates this
  836. interface. |QWidget::activateWindow()| takes no arguments and returns nothing
  837. meaningful.
  838. @<Functions for scripting@>=
  839. QScriptValue QWidget_setLayout(QScriptContext *context, QScriptEngine *)
  840. {
  841. if(context->argumentCount() == 1)
  842. {
  843. QWidget *self = getself<QWidget *>(context);
  844. QLayout *layout = argument<QLayout *>(0, context);
  845. if(layout)
  846. {
  847. self->setLayout(layout);
  848. }
  849. else
  850. {
  851. context->throwError("Incorrect argument type passed to "@|
  852. "QWidget::setLayout(). This method requires "@|
  853. "a QLayout.");
  854. }
  855. }
  856. else
  857. {
  858. context->throwError("Incorrect number of arguments passed to "@|
  859. "QWidget::setLayout(). This method takes one "@|
  860. "QLayout as an argument.");
  861. }
  862. return QScriptValue();
  863. }
  864. QScriptValue QWidget_activateWindow(QScriptContext *context,
  865. QScriptEngine *)
  866. {
  867. QWidget *self = getself<QWidget *>(context);
  868. self->activateWindow();
  869. return QScriptValue();
  870. }
  871. @* Scripting QMessageBox.
  872. \noindent Some features require that \pn{} pauses an operation until further
  873. information can be obtained. An example of this is discretionary validation
  874. where input is checked and if it seems unlikely but not impossible to be
  875. correct a dialog should come up asking if that input is correct. If it is not,
  876. the operation should be cancelled and the person using \pn{} should be allowed
  877. to correct the information and try again.
  878. For this use case, it is not necessary to fully expose the |QMessageBox| class.
  879. Instead, it is enough to provide a function that will raise an appropriate
  880. message and return the selected action.
  881. @<Function prototypes for scripting@>=
  882. QScriptValue displayWarning(QScriptContext *context, QScriptEngine *engine);
  883. QScriptValue displayError(QScriptContext *context, QScriptEngine *engine);
  884. QScriptValue displayInfo(QScriptContext *context, QScriptEngine *engine);
  885. @ This function is exposed to the host environment.
  886. @<Set up the scripting engine@>=
  887. constructor = engine->newFunction(displayWarning);
  888. engine->globalObject().setProperty("displayWarning", constructor);
  889. constructor = engine->newFunction(displayError);
  890. engine->globalObject().setProperty("displayError", constructor);
  891. constructor = engine->newFunction(displayInfo);
  892. engine->globalObject().setProperty("displayInfo", constructor);
  893. @ The function takes some arguments.
  894. @<Functions for scripting@>=
  895. QScriptValue displayWarning(QScriptContext *context, QScriptEngine *)
  896. {
  897. QMessageBox::StandardButton selection = QMessageBox::warning(NULL,
  898. argument<QString>(0, context),
  899. argument<QString>(1, context),
  900. QMessageBox::Ok | QMessageBox::Cancel);
  901. if(selection == QMessageBox::Ok) {
  902. return QScriptValue(true);
  903. }
  904. return QScriptValue(false);
  905. }
  906. QScriptValue displayError(QScriptContext *context, QScriptEngine *)
  907. {
  908. QMessageBox::critical(NULL, argument<QString>(0, context),
  909. argument<QString>(1, context));
  910. return QScriptValue();
  911. }
  912. QScriptValue displayInfo(QScriptContext *context, QScriptEngine *)
  913. {
  914. QMessageBox::information(NULL, argument<QString>(0, context),
  915. argument<QString>(1, context));
  916. return QScriptValue();
  917. }
  918. @* Scripting QMainWindow.
  919. \noindent Rather than directly exposing |QMainWindow| to the scripting engine,
  920. we expose a class derived from |QMainWindow| with a minor change allowing the
  921. script to be notified when the window is about to be closed.
  922. This allows us to save settings for objects populating the window. Close
  923. handlers can be established by connecting to the |aboutToClose()| signal which
  924. is emitted in the |closeEvent()| handler. The close event is always accepted
  925. after the script has had a chance to respond, so this cannot be used to present
  926. an, ``Are you sure?'' message without additional modification.
  927. Slots are also provided for saving the size and position of the window to
  928. settings and restoring the window geometry from these settings.
  929. As of version 1.4 window geometry management is provided for all windows. The
  930. |restoreSizeAndPosition()| and |saveSizeAndPosition()| methods should be
  931. considered depreciated.
  932. Version 1.6 adds a new property for handling the |windowModified| property
  933. such that an appropriate prompt is provided to confirm or cancel close events.
  934. Version 1.8 adds a new |setupFinished()| slot which is called after the
  935. initial |show()| at the end of window creation. This emits a |windowReady()|
  936. signal. Scripts can connect to this signal to perform tasks that must happen
  937. after the window has fully finished opening. The initial use for this is
  938. validating that all required configuration has been performed for a given
  939. window to be useful and, if not, immediately closing that. Without this, a
  940. call to |close()| in the script is reversed when the function creating the
  941. window calls |show()|.
  942. @<Class declarations@>=
  943. class ScriptQMainWindow : public QMainWindow@/
  944. {@t\1@>@/
  945. Q_OBJECT@;@/
  946. Q_PROPERTY(QString closePrompt READ closePrompt WRITE setClosePrompt)@;@/
  947. public:@/
  948. ScriptQMainWindow();
  949. QString closePrompt();@/
  950. @t\4@>public slots@t\kern-3pt@>:@/
  951. void show();
  952. void saveSizeAndPosition(const QString &key);
  953. void restoreSizeAndPosition(const QString &key);
  954. void displayStatus(const QString &message = QString());
  955. void setClosePrompt(QString prompt);
  956. void setupFinished();@/
  957. signals:@/
  958. void aboutToClose(void);
  959. void windowReady(void);@/
  960. protected:@/
  961. void closeEvent(QCloseEvent *event);
  962. void showEvent(QShowEvent *event);@/
  963. private:@/
  964. QString cprompt;@t\2@>@/
  965. }@t\kern-3pt@>;
  966. @ The implementation of these functions is simple.
  967. @<Functions for scripting@>=
  968. ScriptQMainWindow::ScriptQMainWindow()@+: QMainWindow(NULL),
  969. cprompt(tr("Closing this window may result in loss of data. Continue?"))@/
  970. {
  971. if(!AppInstance->databaseConnected())
  972. {
  973. statusBar()->addWidget(new QLabel(tr("Not connected to database")));
  974. }
  975. else
  976. {
  977. statusBar()->addWidget(new UserLabel);
  978. }
  979. }
  980. void ScriptQMainWindow::saveSizeAndPosition(const QString &key)
  981. {
  982. QSettings settings;
  983. settings.beginGroup(key);
  984. settings.setValue("pos", pos());
  985. settings.setValue("size", size());
  986. settings.endGroup();
  987. }
  988. void ScriptQMainWindow::restoreSizeAndPosition(const QString &key)
  989. {
  990. QSettings settings;
  991. settings.beginGroup(key);
  992. if(settings.contains("size"))
  993. {
  994. resize(settings.value("size").toSize());
  995. }
  996. if(settings.contains("pos"))
  997. {
  998. move(settings.value("pos").toPoint());
  999. }
  1000. settings.endGroup();
  1001. }
  1002. void ScriptQMainWindow::displayStatus(const QString &message)
  1003. {
  1004. statusBar()->showMessage(message);
  1005. }
  1006. void ScriptQMainWindow::showEvent(QShowEvent *event)
  1007. {
  1008. if(!event->spontaneous())
  1009. {
  1010. @<Restore window geometry@>@;
  1011. event->accept();
  1012. }
  1013. else
  1014. {
  1015. event->ignore();
  1016. }
  1017. }
  1018. void ScriptQMainWindow::show()
  1019. {
  1020. QMainWindow::show();
  1021. }
  1022. void ScriptQMainWindow::setupFinished()
  1023. {
  1024. emit windowReady();
  1025. }
  1026. @ When a close event occurs, we check the |windowModified| property to
  1027. determine if closing the window could result in loss of data. If this is
  1028. true, we allow the event to be cancelled. Otherwise, a signal is emitted which
  1029. allows scripts to perform any cleanup that may be required before closing the
  1030. window and the window geometry data is saved before allowing the window to
  1031. close.
  1032. @<Functions for scripting@>=
  1033. void ScriptQMainWindow::closeEvent(QCloseEvent *event)
  1034. {
  1035. if(isWindowModified()) {
  1036. @<Allow close event to be cancelled@>@;
  1037. }
  1038. emit aboutToClose();
  1039. @<Save window geometry@>@;
  1040. event->accept();
  1041. }
  1042. @ The prompt text for our confirmation window is provided through the
  1043. |closePrompt| property.
  1044. @<Allow close event to be cancelled@>=
  1045. QMessageBox::StandardButton result;
  1046. result = QMessageBox::warning(this, "Typica", closePrompt(),
  1047. QMessageBox::Ok | QMessageBox::Cancel);
  1048. if(result == QMessageBox::Cancel)
  1049. {
  1050. event->ignore();
  1051. return;
  1052. }
  1053. @ Implementation of the |closePrompt| property is trivial.
  1054. @<Functions for scripting@>=
  1055. QString ScriptQMainWindow::closePrompt()
  1056. {
  1057. return cprompt;
  1058. }
  1059. void ScriptQMainWindow::setClosePrompt(QString prompt)
  1060. {
  1061. cprompt = prompt;
  1062. }
  1063. @ Window geometry management from version 1.4 on makes use of the window ID to
  1064. produce an appropriate QSettings key. This decision relies on the ID being set
  1065. before any show or close events are received and it relies on every distinct
  1066. type of window having a unique ID. If this is not the case then other things
  1067. are likely very broken. Note that with this approach multiple instances of the
  1068. same type of window will use the same key. This may not be ideal in all cases,
  1069. but further refinements can be produced if necessary.
  1070. @<Save window geometry@>=
  1071. QSettings settings;
  1072. settings.setValue(QString("geometries/%1").arg(objectName()), saveGeometry());
  1073. @ Restoring saved geometry is performed similarly to saving it.
  1074. @<Restore window geometry@>=
  1075. QSettings settings;
  1076. restoreGeometry(settings.value(QString("geometries/%1").arg(objectName())).
  1077. toByteArray());
  1078. @ Three functions are required to obtain the required functionality from a
  1079. script. A fourth adds properties for the object hierarchy.
  1080. @<Function prototypes for scripting@>=
  1081. QScriptValue constructQMainWindow(QScriptContext *context,
  1082. QScriptEngine *engine);
  1083. QScriptValue QMainWindow_setCentralWidget(QScriptContext *context,@|
  1084. QScriptEngine *engine);
  1085. QScriptValue QMainWindow_menuBar(QScriptContext *context,
  1086. QScriptEngine *engine);
  1087. void setQMainWindowProperties(QScriptValue value, QScriptEngine *engine);
  1088. @ Of these, the engine only needs to be informed of the constructor initially.
  1089. @<Set up the scripting engine@>=
  1090. constructor = engine->newFunction(constructQMainWindow);
  1091. value = engine->newQMetaObject(&ScriptQMainWindow::staticMetaObject,
  1092. constructor);
  1093. engine->globalObject().setProperty("QMainWindow", value);
  1094. @ The constructor calls a function to add the additional properties to the
  1095. newly created value.
  1096. @<Functions for scripting@>=
  1097. QScriptValue constructQMainWindow(QScriptContext *, QScriptEngine *engine)
  1098. {
  1099. QScriptValue object = engine->newQObject(new ScriptQMainWindow);
  1100. setQMainWindowProperties(object, engine);
  1101. return object;
  1102. }
  1103. void setQMainWindowProperties(QScriptValue value, QScriptEngine *engine)
  1104. {
  1105. setQWidgetProperties(value, engine);
  1106. value.setProperty("setCentralWidget",
  1107. engine->newFunction(QMainWindow_setCentralWidget));
  1108. value.setProperty("menuBar", engine->newFunction(QMainWindow_menuBar));
  1109. }
  1110. @ The |"setCentralWidget"| property is used for setting a widget in the main
  1111. area of the window. In \pn{} this will usually be a |QSplitter| object, but it
  1112. could also be a bare |QWidget| with a layout managing multiple widgets or a
  1113. custom widget defined in a local change. This is a simple wrapper around
  1114. |QMainWindow::setCentralWidget()|.
  1115. @<Functions for scripting@>=
  1116. QScriptValue QMainWindow_setCentralWidget(QScriptContext *context,
  1117. QScriptEngine *)
  1118. {
  1119. if(context->argumentCount() == 1)
  1120. {
  1121. QMainWindow *self = getself<QMainWindow *>(context);
  1122. QWidget *widget = argument<QWidget *>(0, context);
  1123. if(widget)
  1124. {
  1125. self->setCentralWidget(widget);
  1126. }
  1127. else
  1128. {
  1129. context->throwError("Incorrect argument type passed to "@|
  1130. "QMainWindow::setCentralWidget(). This "@|
  1131. "method requires a QWidget.");
  1132. }
  1133. }
  1134. else
  1135. {
  1136. context->throwError("Incorrect number of arguments passed to "@|
  1137. "QMainWindow::setCentralWidget(). This method "@|
  1138. "takes one QWidget as an argument.");
  1139. }
  1140. return QScriptValue();
  1141. }
  1142. @ The |"menuBar"| property requires that we expose |QMenuBar| to the scripting
  1143. environment in a limited fashion. We don'@q'@>t need to allow scripts to create a
  1144. new menu bar as it can be obtained from the window, however to add the menus to
  1145. the menu bar, we need to add a property to the |QMenuBar| object before passing
  1146. it back.
  1147. @<Functions for scripting@>=
  1148. QScriptValue QMainWindow_menuBar(QScriptContext *context, QScriptEngine *engine)
  1149. {
  1150. QScriptValue object;
  1151. if(context->argumentCount() == 0)
  1152. {
  1153. QMainWindow *self = getself<@[QMainWindow *@]>(context);
  1154. QMenuBar *bar = self->menuBar();
  1155. object = engine->newQObject(bar);
  1156. setQMenuBarProperties(object, engine);
  1157. }
  1158. else
  1159. {
  1160. context->throwError("Incorrect number of arguments passed to "@|
  1161. "QMainWindow::menuBar(). This method takes no "@|
  1162. "arguments.");
  1163. }
  1164. return object;
  1165. }
  1166. @ The previous function is the only place a new |QMenuBar| is created through
  1167. the host environment. Two functions are used in handling this object creation.
  1168. @<Function prototypes for scripting@>=
  1169. void setQMenuBarProperties(QScriptValue value, QScriptEngine *engine);
  1170. QScriptValue QMenuBar_addMenu(QScriptContext *context, QScriptEngine *engine);
  1171. @ The first of these adds the appropriate properties to the newly created
  1172. object.
  1173. @<Functions for scripting@>=
  1174. void setQMenuBarProperties(QScriptValue value, QScriptEngine *engine)
  1175. {
  1176. setQWidgetProperties(value, engine);
  1177. value.setProperty("addMenu", engine->newFunction(QMenuBar_addMenu));
  1178. }
  1179. @ This function can be used to add new menus to a menu bar. In order to do
  1180. anything with the newly created menus, two properties are added to the |QMenu|
  1181. objects which allow actions to be added as menu items and allow separators to be
  1182. placed between groups of menu items.
  1183. At the time of this writing, there are three |QMenuBar::addMenu()| methods. This
  1184. function wraps |QMenu* QMenuBar::addMenu(const QString &title)|.
  1185. @<Functions for scripting@>=
  1186. QScriptValue QMenuBar_addMenu(QScriptContext *context, QScriptEngine *engine)
  1187. {
  1188. QScriptValue object;
  1189. if(context->argumentCount() == 1)
  1190. {
  1191. QMenuBar *self = getself<@[QMenuBar *@]>(context);
  1192. QString title = argument<QString>(0, context);
  1193. object = engine->newQObject(self->addMenu(title));
  1194. setQMenuProperties(object, engine);
  1195. }
  1196. else
  1197. {
  1198. context->throwError("Incorrect number of arguments passed to "@|
  1199. "QMenuBar::addMenu(). This method takes one "@|
  1200. "string as an argument.");
  1201. }
  1202. return object;
  1203. }
  1204. @ These three functions allow adding items to the menu and adding separators
  1205. between groups of items.
  1206. @<Function prototypes for scripting@>=
  1207. void setQMenuProperties(QScriptValue value, QScriptEngine *engine);
  1208. QScriptValue QMenu_addAction(QScriptContext *context, QScriptEngine *engine);
  1209. QScriptValue QMenu_addSeparator(QScriptContext *context, QScriptEngine *engine);
  1210. @ The first of these add properties to newly created |QMenu| objects.
  1211. @<Functions for scripting@>=
  1212. void setQMenuProperties(QScriptValue value, QScriptEngine *engine)
  1213. {
  1214. setQWidgetProperties(value, engine);
  1215. value.setProperty("addAction", engine->newFunction(QMenu_addAction));
  1216. value.setProperty("addSeparator", engine->newFunction(QMenu_addSeparator));
  1217. }
  1218. @ These functions are simple wrappers around |QMenu| methods.
  1219. @<Functions for scripting@>=
  1220. QScriptValue QMenu_addAction(QScriptContext *context, QScriptEngine *)
  1221. {
  1222. if(context->argumentCount() == 1)
  1223. {
  1224. QMenu *self = getself<@[QMenu *@]>(context);
  1225. QAction *action = argument<QAction *>(0, context);
  1226. if(action)
  1227. {
  1228. self->addAction(action);
  1229. }
  1230. else
  1231. {
  1232. context->throwError("Incorrect argument type passed to "@|
  1233. "QMenu::addAction(). This method requires a "@|
  1234. "QAction.");
  1235. }
  1236. }
  1237. else
  1238. {
  1239. context->throwError("Incorrect number of arguments passed to "@|
  1240. "QMenu::addAction(). This method takes one "@|
  1241. "QAction as an argument.");
  1242. }
  1243. return QScriptValue();
  1244. }
  1245. QScriptValue QMenu_addSeparator(QScriptContext *context, QScriptEngine *)
  1246. {
  1247. if(context->argumentCount() == 0)
  1248. {
  1249. QMenu *self = getself<@[QMenu *@]>(context);
  1250. self->addSeparator();
  1251. }
  1252. else
  1253. {
  1254. context->throwError("Incorrect number of arguments passed to "@|
  1255. "QMenu::addSeparator(). This method takes no "@|
  1256. "arguments.");
  1257. }
  1258. return QScriptValue();
  1259. }
  1260. @* Scripting QFrame.
  1261. \noindent |QFrame| is another class for which little needs to be done. It exists
  1262. as a subclass of |QWidget| and a superclass for |QSplitter|, |QLCDNumber|, and
  1263. |QAbstractScrollArea| among other classes.
  1264. @<Function prototypes for scripting@>=
  1265. void setQFrameProperties(QScriptValue value, QScriptEngine *engine);
  1266. QScriptValue constructQFrame(QScriptContext *context, QScriptEngine *engine);
  1267. @ The constructor must be passed to the scripting engine.
  1268. @<Set up the scripting engine@>=
  1269. constructor = engine->newFunction(constructQFrame);
  1270. value = engine->newQMetaObject(&QFrame::staticMetaObject, constructor);
  1271. engine->globalObject().setProperty("QFrame", value);
  1272. @ The implementation of these functions should seem familiar.
  1273. @<Functions for scripting@>=
  1274. QScriptValue constructQFrame(QScriptContext *, QScriptEngine *engine)
  1275. {
  1276. QScriptValue object = engine->newQObject(new QFrame);
  1277. setQFrameProperties(object, engine);
  1278. return object;
  1279. }
  1280. void setQFrameProperties(QScriptValue value, QScriptEngine *engine)
  1281. {
  1282. setQWidgetProperties(value, engine);
  1283. }
  1284. @* Scripting QLabel.
  1285. \noindent When constructing an interface wholly or partially through dynamic
  1286. means rather than entirely through the XML configuration document it can
  1287. sometimes be desirable to construct |QLabel| instances. This is usually used
  1288. to provide a very small amount of text.
  1289. @<Function prototypes for scripting@>=
  1290. void setQLabelProperties(QScriptValue value, QScriptEngine *engine);
  1291. QScriptValue constructQLabel(QScriptContext *context, QScriptEngine *engine);
  1292. @ The constructor must be passed to the scripting engine.
  1293. @<Set up the scripting engine@>=
  1294. constructor = engine->newFunction(constructQLabel);
  1295. value = engine->newQMetaObject(&QLabel::staticMetaObject, constructor);
  1296. engine->globalObject().setProperty("QLabel", value);
  1297. @ In the constructor we allow an optional argument to specify the text of the
  1298. label.
  1299. @<Functions for scripting@>=
  1300. QScriptValue constructQLabel(QScriptContext *context, QScriptEngine *engine)
  1301. {
  1302. QString text;
  1303. if(context->argumentCount() == 1)
  1304. {
  1305. text = argument<QString>(0, context);
  1306. }
  1307. QScriptValue object = engine->newQObject(new QLabel(text));
  1308. setQLabelProperties(object, engine);
  1309. return object;
  1310. }
  1311. void setQLabelProperties(QScriptValue value, QScriptEngine *engine)
  1312. {
  1313. setQFrameProperties(value, engine);
  1314. }
  1315. @* Scripting QSvgWidget.
  1316. \noindent Sometimes it is useful to provide a space for simple drawings without
  1317. the need for all of the other capabilities of a web view. This was introduced
  1318. as a way to draw box plots to help guide the creation of roast specifications.
  1319. @<Function prototypes for scripting@>=
  1320. void setQSvgWidgetProperties(QScriptValue value, QScriptEngine *engine);
  1321. QScriptValue constructQSvgWidget(QScriptContext *context,
  1322. QScriptEngine *engine);
  1323. QScriptValue QSvgWidget_loadDevice(QScriptContext *context,
  1324. QScriptEngine *engine);
  1325. void addSvgWidgetToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  1326. QStack<QLayout *> *layoutStack);
  1327. @ The constructor must be passed to the scripting engine.
  1328. @<Set up the scripting engine@>=
  1329. constructor = engine->newFunction(constructQSvgWidget);
  1330. value = engine->newQMetaObject(&QSvgWidget::staticMetaObject, constructor);
  1331. engine->globalObject().setProperty("QSvgWidget", value);
  1332. @ The constructor is trivial.
  1333. @<Functions for scripting@>=
  1334. QScriptValue constructQSvgWidget(QScriptContext *,
  1335. QScriptEngine *engine)
  1336. {
  1337. QScriptValue object = engine->newQObject(new QSvgWidget);
  1338. setQSvgWidgetProperties(object, engine);
  1339. return object;
  1340. }
  1341. @ A property is added that allows loading data from a |QIODevice|.
  1342. @<Functions for scripting@>=
  1343. void setQSvgWidgetProperties(QScriptValue value, QScriptEngine *engine)
  1344. {
  1345. setQWidgetProperties(value, engine);
  1346. value.setProperty("loadDevice",
  1347. engine->newFunction(QSvgWidget_loadDevice));
  1348. }
  1349. QScriptValue QSvgWidget_loadDevice(QScriptContext *context, QScriptEngine *)
  1350. {
  1351. if(context->argumentCount() == 1)
  1352. {
  1353. QSvgWidget *self = getself<@[QSvgWidget *@]>(context);
  1354. QIODevice *device = argument<QIODevice *>(0, context);
  1355. device->reset();
  1356. QByteArray data = device->readAll();
  1357. self->load(data);
  1358. }
  1359. else
  1360. {
  1361. context->throwError("Incorrect number of arguments passed to "@|
  1362. "QSvgWidget::loadData(). This method takes one "@|
  1363. "QIODevice as an argument.");
  1364. }
  1365. return QScriptValue();
  1366. }
  1367. @ Additional work is needed to allow including this from the XML description of
  1368. a window.
  1369. @<Additional box layout elements@>=
  1370. else if(currentElement.tagName() == "svgwidget")
  1371. {
  1372. addSvgWidgetToLayout(currentElement, widgetStack, layoutStack);
  1373. }
  1374. @ The function used to create this follows the usual pattern.
  1375. @<Functions for scripting@>=
  1376. void addSvgWidgetToLayout(QDomElement element, QStack<QWidget *> *,
  1377. QStack<QLayout *> *layoutStack)
  1378. {
  1379. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  1380. QSvgWidget *widget = new QSvgWidget;
  1381. layout->addWidget(widget);
  1382. QString id = element.attribute("id");
  1383. if(!id.isEmpty())
  1384. {
  1385. widget->setObjectName(id);
  1386. }
  1387. }
  1388. @* Scripting QLineEdit.
  1389. \noindent Similarly, we may want to allow line edits in interfaces defined
  1390. through the host environment. For example, this is used for the free text
  1391. annotation control for roasters this has been configured on.
  1392. @<Function prototypes for scripting@>=
  1393. void setQLineEditProperties(QScriptValue value, QScriptEngine *engine);
  1394. QScriptValue constructQLineEdit(QScriptContext *context, QScriptEngine *engine);
  1395. @ The constructor must be passed to the host environment.
  1396. @<Set up the scripting engine@>=
  1397. constructor = engine->newFunction(constructQLineEdit);
  1398. value = engine->newQMetaObject(&QLineEdit::staticMetaObject, constructor);
  1399. engine->globalObject().setProperty("QLineEdit", value);
  1400. @ The constructor is trivial.
  1401. @<Functions for scripting@>=
  1402. QScriptValue constructQLineEdit(QScriptContext *, QScriptEngine *engine)
  1403. {
  1404. QScriptValue object = engine->newQObject(new QLineEdit());
  1405. setQLineEditProperties(object, engine);
  1406. return object;
  1407. }
  1408. @ At present all of the QLineEdit functionality exposed through this interface
  1409. is provided automatically through the meta-object system.
  1410. @<Functions for scripting@>=
  1411. void setQLineEditProperties(QScriptValue value, QScriptEngine *engine)
  1412. {
  1413. setQWidgetProperties(value, engine);
  1414. }
  1415. @* Scripting QSplitter.
  1416. \noindent The |QSplitter| class is one of the main classes used for user
  1417. interface object layout in \pn{}. To provide this class to the scripting engine,
  1418. we provide five functions: a constructor, a method for adding widgets to the
  1419. splitter, a method for saving the size of each widget in the splitter, a
  1420. method for restoring these saved sizes, and a function for adding these methods
  1421. as properties of newly created |QSplitter| objects.
  1422. @<Function prototypes for scripting@>=
  1423. QScriptValue constructQSplitter(QScriptContext *context, QScriptEngine *engine);
  1424. QScriptValue QSplitter_addWidget(QScriptContext *context,
  1425. QScriptEngine *engine);
  1426. QScriptValue QSplitter_saveState(QScriptContext *context,
  1427. QScriptEngine *engine);
  1428. QScriptValue QSplitter_restoreState(QScriptContext *context,
  1429. QScriptEngine *engine);
  1430. QScriptValue QSplitter_count(QScriptContext *context,
  1431. QScriptEngine *engine);
  1432. QScriptValue QSplitter_setCollapsible(QScriptContext *context,
  1433. QScriptEngine *engine);
  1434. void setQSplitterProperties(QScriptValue value, QScriptEngine *engine);
  1435. @ Of these, the scripting engine must be informed of the constructor.
  1436. @<Set up the scripting engine@>=
  1437. constructor = engine->newFunction(constructQSplitter);
  1438. value = engine->newQMetaObject(&QSplitter::staticMetaObject, constructor);
  1439. engine->globalObject().setProperty("QSplitter", value);
  1440. @ The constructor creates the object and adds the required properties to it.
  1441. @<Functions for scripting@>=
  1442. QScriptValue constructQSplitter(QScriptContext *, QScriptEngine *engine)
  1443. {
  1444. QScriptValue object = engine->newQObject(new QSplitter);
  1445. setQSplitterProperties(object, engine);
  1446. return object;
  1447. }
  1448. void setQSplitterProperties(QScriptValue value, QScriptEngine *engine)
  1449. {
  1450. setQFrameProperties(value, engine);
  1451. value.setProperty("addWidget", engine->newFunction(QSplitter_addWidget));
  1452. value.setProperty("saveState", engine->newFunction(QSplitter_saveState));
  1453. value.setProperty("restoreState",
  1454. engine->newFunction(QSplitter_restoreState));
  1455. value.setProperty("count", engine->newFunction(QSplitter_count));
  1456. value.setProperty("setCollapsible", engine->newFunction(QSplitter_setCollapsible));
  1457. }
  1458. @ The |"addWidget"| property is a simple wrapper around
  1459. |QSplitter::addWidget()|.
  1460. @<Functions for scripting@>=
  1461. QScriptValue QSplitter_addWidget(QScriptContext *context, QScriptEngine *)
  1462. {
  1463. if(context->argumentCount() == 1)
  1464. {
  1465. QSplitter *self = getself<QSplitter *>(context);
  1466. QWidget *widget = argument<QWidget *>(0, context);
  1467. if(widget)
  1468. {
  1469. self->addWidget(widget);
  1470. }
  1471. else
  1472. {
  1473. context->throwError("Incorrect argument type passed to "@|
  1474. "QSplitter::addWidget(). This method "@|
  1475. "requires a QWidget.");
  1476. }
  1477. }
  1478. else
  1479. {
  1480. context->throwError("Incorrect number of arguments passed to "@|
  1481. "QSplitter::addWidget(). This method takes one "@|
  1482. "QWidget as an argument.");
  1483. }
  1484. return QScriptValue();
  1485. }
  1486. @ The methods for saving and restoring the state of a splitter do not behave
  1487. well when the number of widgets contained in the splitter increase. This is a
  1488. problem in the logging view where we may want to allow zero width indicators
  1489. but reconfiguration can cause the number of indicators to increase. This would
  1490. result in the right most indicators such as the batch timer disappearing. Most
  1491. people do not notice the splitter handle or think to drag that to the left to
  1492. correct this issue so it would be better to save the number of indicators when
  1493. saving the state and if the number of indicators does not match, we should not
  1494. restore the obsolete saved state.
  1495. @<Functions for scripting@>=
  1496. QScriptValue QSplitter_count(QScriptContext *context, QScriptEngine *)
  1497. {
  1498. QSplitter *self = getself<QSplitter *>(context);
  1499. return QScriptValue(self->count());
  1500. }
  1501. @ When saving and restoring the state of a splitter, we always want to do this
  1502. through a |QSettings| object. For this, we take an extra argument specifying the
  1503. settings key to read from or write to. Unlike the equivalent functions called
  1504. from native code, neither of these functions called from a script will return
  1505. the data being saved.
  1506. @<Functions for scripting@>=
  1507. QScriptValue QSplitter_saveState(QScriptContext *context, QScriptEngine *)
  1508. {
  1509. if(context->argumentCount() == 1)
  1510. {
  1511. QSplitter *self = getself<QSplitter *>(context);
  1512. QString key = argument<QString>(0, context);
  1513. QSettings settings;
  1514. settings.setValue(key, self->saveState());
  1515. }
  1516. else
  1517. {
  1518. context->throwError("Incorrect number of arguments passed to "@|
  1519. "QSplitter::saveState(). This method takes one "@|
  1520. "string as an argument.");
  1521. }
  1522. return QScriptValue();
  1523. }
  1524. QScriptValue QSplitter_restoreState(QScriptContext *context, QScriptEngine *)
  1525. {
  1526. if(context->argumentCount() == 1)
  1527. {
  1528. QSplitter *self = getself<QSplitter *>(context);
  1529. QString key = argument<QString>(0, context);
  1530. QSettings settings;
  1531. self->restoreState(settings.value(key).toByteArray());
  1532. }
  1533. else
  1534. {
  1535. context->throwError("Incorrect number of arguments passed to "@|
  1536. "QSplitter::restoreState(). This method takes "@|
  1537. "one string as an argument.");
  1538. }
  1539. return QScriptValue();
  1540. }
  1541. @ Sometimes a |QSplitter| is used to make it easy to hide optional elements. In
  1542. this use case it can be useful to indicate that required elements cannot be
  1543. collapsed.
  1544. @<Functions for scripting@>=
  1545. QScriptValue QSplitter_setCollapsible(QScriptContext *context, QScriptEngine *)
  1546. {
  1547. if(context->argumentCount() == 2)
  1548. {
  1549. QSplitter *self = getself<QSplitter *>(context);
  1550. self->setCollapsible(argument<int>(0, context), argument<bool>(1, context));
  1551. }
  1552. else
  1553. {
  1554. context->throwError("Incorrect number of arguments");
  1555. }
  1556. return QScriptValue();
  1557. }
  1558. @* Scripting Layout classes.
  1559. \noindent Layout classes simplify managing the size and position of widgets in a
  1560. user interface. Qt provides several such classes, including |QBoxLayout| which
  1561. can be used to construct a variety of different interfaces. As widgets
  1562. containing a layout should not really need to care which layout is being used,
  1563. the |QLayout| class acts as an abstract base for all layout classes. A bare
  1564. |QLayout| will never be constructed, however subclasses can make use of the
  1565. |addWidget()| property.
  1566. @<Function prototypes for scripting@>=
  1567. void setQLayoutProperties(QScriptValue value, QScriptEngine *engine);
  1568. QScriptValue QLayout_addWidget(QScriptContext *context, QScriptEngine *engine);
  1569. @ The implementation is trivial.
  1570. @<Functions for scripting@>=
  1571. void setQLayoutProperties(QScriptValue value, QScriptEngine *engine)
  1572. {
  1573. setQLayoutItemProperties(value, engine);
  1574. value.setProperty("addWidget", engine->newFunction(QLayout_addWidget));
  1575. }
  1576. QScriptValue QLayout_addWidget(QScriptContext *context, QScriptEngine *)
  1577. {
  1578. if(context->argumentCount() == 1)
  1579. {
  1580. QLayout *self = getself<QLayout *>(context);
  1581. QWidget *widget = argument<QWidget *>(0, context);
  1582. if(widget)
  1583. {
  1584. self->addWidget(widget);
  1585. }
  1586. else
  1587. {
  1588. context->throwError("Incorrect argument type passed to "@|
  1589. "QLayout::addWidget(). This method requires "@|
  1590. "a QWidget.");
  1591. }
  1592. }
  1593. else
  1594. {
  1595. context->throwError("Incorrect number of arguments passed to "@|
  1596. "QLayout::addWidget(). This method takes one "@|
  1597. "QWidget as an argument.");
  1598. }
  1599. return QScriptValue();
  1600. }
  1601. @ |QBoxLayout| is a more interesting layout class. This allows widgets to be
  1602. arranged in a single row or column and can be used, for example, to arrange a
  1603. row of buttons as in figure \secno.
  1604. \medskip
  1605. \resizebox*{6.3in}{!}{\includegraphics{boxlayoutexample}}
  1606. \smallskip
  1607. \centerline{Figure \secno: Buttons in a |QBoxLayout|.}
  1608. \medskip
  1609. This class makes use of the |addWidget()| method from |QLayout|.
  1610. @<Function prototypes for scripting@>=
  1611. QScriptValue constructQBoxLayout(QScriptContext *context,
  1612. QScriptEngine *engine);
  1613. void setQBoxLayoutProperties(QScriptValue value, QScriptEngine *engine);
  1614. QScriptValue QBoxLayout_addLayout(QScriptContext *context, QScriptEngine *engine);
  1615. QScriptValue QBoxLayout_addWidget(QScriptContext *context, QScriptEngine *engine);
  1616. @ The script constructor must be passed to the scripting engine.
  1617. @<Set up the scripting engine@>=
  1618. constructor = engine->newFunction(constructQBoxLayout);
  1619. value = engine->newQMetaObject(&QBoxLayout::staticMetaObject, constructor);
  1620. engine->globalObject().setProperty("QBoxLayout", value);
  1621. @ The implementation of these functions should seem familiar by now. Note that
  1622. while a horizontal layout is provided by default, this can be changed from the
  1623. script once the layout is created.
  1624. @<Functions for scripting@>=
  1625. QScriptValue constructQBoxLayout(QScriptContext *, QScriptEngine *engine)
  1626. {
  1627. QScriptValue object =
  1628. engine->newQObject(new QBoxLayout(QBoxLayout::LeftToRight));
  1629. setQBoxLayoutProperties(object, engine);
  1630. return object;
  1631. }
  1632. void setQBoxLayoutProperties(QScriptValue value, QScriptEngine *engine)
  1633. {
  1634. setQLayoutProperties(value, engine);
  1635. value.setProperty("addLayout", engine->newFunction(QBoxLayout_addLayout));
  1636. value.setProperty("addWidget", engine->newFunction(QBoxLayout_addWidget));
  1637. }
  1638. QScriptValue QBoxLayout_addLayout(QScriptContext *context, QScriptEngine *)
  1639. {
  1640. if(context->argumentCount() > 0 && context->argumentCount() < 3)
  1641. {
  1642. QBoxLayout *self = getself<QBoxLayout *>(context);
  1643. QLayout *layout = argument<QLayout *>(0, context);
  1644. int stretch = 0;
  1645. if(context->argumentCount() == 2)
  1646. {
  1647. stretch = argument<int>(1, context);
  1648. }
  1649. if(layout)
  1650. {
  1651. self->addLayout(layout, stretch);
  1652. }
  1653. else
  1654. {
  1655. context->throwError("Incorrect argument type passed to "@|
  1656. "QLayout::addLayout(). This method requires "@|
  1657. "a QLayout.");
  1658. }
  1659. }
  1660. else
  1661. {
  1662. context->throwError("Incorrect number of arguments passed to "@|
  1663. "QLayout::addLayout(). This method takes one "@|
  1664. "QLayout as an argument and optionally one integer.");
  1665. }
  1666. return QScriptValue();
  1667. }
  1668. @ We override the base class wrapper for |addWidget| to add two optional
  1669. arguments: one specifies the stretch factor of the widget and the other
  1670. specifies the alignment of the widget within the layout.
  1671. @<Functions for scripting@>=
  1672. QScriptValue QBoxLayout_addWidget(QScriptContext *context, QScriptEngine *)
  1673. {
  1674. if(context->argumentCount() > 0 && context->argumentCount() < 4)
  1675. {
  1676. QBoxLayout *self = getself<QBoxLayout *>(context);
  1677. QWidget *widget = argument<QWidget *>(0, context);
  1678. int stretch = 0;
  1679. Qt::Alignment alignment = 0;
  1680. if(context->argumentCount() > 1)
  1681. {
  1682. stretch = argument<int>(1, context);
  1683. }
  1684. if(context->argumentCount() > 2)
  1685. {
  1686. alignment = (Qt::Alignment)(argument<int>(2, context));
  1687. }
  1688. if(widget)
  1689. {
  1690. self->addWidget(widget, stretch, alignment);
  1691. }
  1692. else
  1693. {
  1694. context->throwError("Incorrect argument type passed to "@|
  1695. "QBoxLayout::addWidget(). This method requires "@|
  1696. "a QWidget.");
  1697. }
  1698. }
  1699. else
  1700. {
  1701. context->throwError("Incorrect number of arguments passed to "@|
  1702. "QBoxLayout::addWidget(). This method takes one "@|
  1703. "QWidget and optionally up to two integers as "@|
  1704. "arguments.");
  1705. }
  1706. return QScriptValue();
  1707. }
  1708. @* Scripting QAction.
  1709. \noindent The |QAction| class is used in \pn{} to create menu items and respond
  1710. to the selection of these items. Three functions are required for our scripting
  1711. needs with regard to this class.
  1712. @<Function prototypes for scripting@>=
  1713. QScriptValue constructQAction(QScriptContext *context, QScriptEngine *engine);
  1714. QScriptValue QAction_setShortcut(QScriptContext *context,
  1715. QScriptEngine *engine);
  1716. void setQActionProperties(QScriptValue value, QScriptEngine *engine);
  1717. @ The scripting engine must be informed of the constructor.
  1718. @<Set up the scripting engine@>=
  1719. constructor = engine->newFunction(constructQAction);
  1720. value = engine->newQMetaObject(&QAction::staticMetaObject, constructor);
  1721. engine->globalObject().setProperty("QAction", value);
  1722. @ The constructor is simple, however some might sensibly question why the
  1723. |"setShortcut"| property is needed at all. Why not have scripts simply set the
  1724. |shortcut| property of the |QAction| directly? The answer to this is that the
  1725. property expects data of the |QKeySequence| type. While this can be created from
  1726. a |QString|, passing a string to the property through the scripting engine did
  1727. not work at the time this was written.
  1728. @<Functions for scripting@>=
  1729. QScriptValue constructQAction(QScriptContext *, QScriptEngine *engine)
  1730. {
  1731. QScriptValue object = engine->newQObject(new QAction(NULL));
  1732. setQActionProperties(object, engine);
  1733. return object;
  1734. }
  1735. void setQActionProperties(QScriptValue value, QScriptEngine *engine)
  1736. {
  1737. setQObjectProperties(value, engine);
  1738. value.setProperty("setShortcut", engine->newFunction(QAction_setShortcut));
  1739. }
  1740. QScriptValue QAction_setShortcut(QScriptContext *context, QScriptEngine *)
  1741. {
  1742. if(context->argumentCount() == 1)
  1743. {
  1744. QAction *self = getself<@[QAction *@]>(context);
  1745. self->setShortcut(argument<QString>(0, context));
  1746. }
  1747. else
  1748. {
  1749. context->throwError("Incorrect number of arguments passed to "@|
  1750. "QAction::setShortcut(). This method takes one "@|
  1751. "string as an argument.");
  1752. }
  1753. return QScriptValue();
  1754. }
  1755. @* Scripting QFileDialog.
  1756. \noindent |QFileDialog| provides two static member functions which is all that
  1757. we need. The objects returned from these methods are built on the |QDialog|
  1758. abstract base class.
  1759. @<Function prototypes for scripting@>=
  1760. QScriptValue QFileDialog_getOpenFileName(QScriptContext *context,
  1761. QScriptEngine *engine);
  1762. QScriptValue QFileDialog_getSaveFileName(QScriptContext *context,
  1763. QScriptEngine *engine);
  1764. void setQFileDialogProperties(QScriptValue value, QScriptEngine *engine);
  1765. void setQDialogProperties(QScriptValue value, QScriptEngine *engine);
  1766. @ The scripting engine must be informed of the wrapper functions for the static
  1767. methods.
  1768. @<Set up the scripting engine@>=
  1769. value = engine->newQMetaObject(&QFileDialog::staticMetaObject);
  1770. value.setProperty("getOpenFileName",
  1771. engine->newFunction(QFileDialog_getOpenFileName));
  1772. value.setProperty("getSaveFileName",
  1773. engine->newFunction(QFileDialog_getSaveFileName));
  1774. engine->globalObject().setProperty("QFileDialog", value);
  1775. @ This function is just a simple wrapper around the |QFileDialog| method, but
  1776. the object returned has any properties added to the base class available.
  1777. @<Functions for scripting@>=
  1778. QScriptValue QFileDialog_getOpenFileName(QScriptContext *context,
  1779. QScriptEngine *engine)
  1780. {
  1781. QScriptValue retval;
  1782. if(context->argumentCount() == 3)
  1783. {
  1784. QWidget *widget = argument<QWidget *>(0, context);
  1785. if(widget)
  1786. {
  1787. QString caption = argument<QString>(1, context);
  1788. QString dir = argument<QString>(2, context);
  1789. retval = QScriptValue(engine,
  1790. QFileDialog::getOpenFileName(widget, caption,
  1791. dir, "", 0, 0));
  1792. setQFileDialogProperties(retval, engine);
  1793. }
  1794. else
  1795. {
  1796. context->throwError("Incorrect argument type passed to "@|
  1797. "QFileDialog::getOpenFileName(). The first "@|
  1798. "argument to this method must be a QWidget.");
  1799. }
  1800. }
  1801. else
  1802. {
  1803. context->throwError("Incorrect number of arguments passed to "@|
  1804. "QFileDialog::getOpenFileName(). This method "@|
  1805. "takes one QWidget followed by two strings for a "@|
  1806. "total of three arguments.");
  1807. }
  1808. return retval;
  1809. }
  1810. @ Similarly, this just wraps |QFileDialog::getSaveFileName()|.
  1811. @<Functions for scripting@>=
  1812. QScriptValue QFileDialog_getSaveFileName(QScriptContext *context,
  1813. QScriptEngine *engine)
  1814. {
  1815. QScriptValue retval;
  1816. if(context->argumentCount() == 3)
  1817. {
  1818. QWidget *widget = argument<QWidget *>(0, context);
  1819. if(widget)
  1820. {
  1821. QString caption = argument<QString>(1, context);
  1822. QString dir = argument<QString>(2, context);
  1823. retval = QScriptValue(engine,
  1824. QFileDialog::getSaveFileName(widget, caption,
  1825. dir, "", 0, 0));
  1826. setQFileDialogProperties(retval, engine);
  1827. }
  1828. else
  1829. {
  1830. context->throwError("Incorrect argument type passed to "@|
  1831. "QFileDialog::getSaveFileName(). The first "@|
  1832. "argument to this method must be a QWidget.");
  1833. }
  1834. }
  1835. else
  1836. {
  1837. context->throwError("Incorrect number of arguments passed to "@|
  1838. "QFileDialog::getSaveFileName(). This method "@|
  1839. "takes one QWidget followed by two strings for a "@|
  1840. "total of three arguments.");
  1841. }
  1842. return retval;
  1843. }
  1844. @ Adding object hierarchy properties to the objects created above is simple.
  1845. @<Functions for scripting@>=
  1846. void setQFileDialogProperties(QScriptValue value, QScriptEngine *engine)
  1847. {
  1848. setQDialogProperties(value, engine);
  1849. }
  1850. void setQDialogProperties(QScriptValue value, QScriptEngine *engine)
  1851. {
  1852. setQWidgetProperties(value, engine);
  1853. }
  1854. @* Scripting QFile.
  1855. \noindent When using a |QFile| in a script, we only need the constructor and two
  1856. functions for hooking it in with the rest of the object hierarchy.
  1857. @<Function prototypes for scripting@>=
  1858. QScriptValue constructQFile(QScriptContext *context, QScriptEngine *engine);
  1859. void setQFileProperties(QScriptValue value, QScriptEngine *engine);
  1860. QScriptValue QFile_remove(QScriptContext *context, QScriptEngine *engine);
  1861. void setQIODeviceProperties(QScriptValue value, QScriptEngine *engine);
  1862. QScriptValue QIODevice_open(QScriptContext *context, QScriptEngine *engine);
  1863. QScriptValue QIODevice_close(QScriptContext *context, QScriptEngine *engine);
  1864. QScriptValue QIODevice_readToString(QScriptContext *context,
  1865. QScriptEngine *engine);
  1866. QScriptValue QIODevice_putChar(QScriptContext *context, QScriptEngine *engine);
  1867. QScriptValue QIODevice_writeString(QScriptContext *context, QScriptEngine *engine);
  1868. QScriptValue QIODevice_writeBytes(QScriptContext *context, QScriptEngine *engine);
  1869. QScriptValue QIODevice_readBytes(QScriptContext *context, QScriptEngine *engine);
  1870. QScriptValue QIODevice_peek(QScriptContext *context, QScriptEngine *engine);
  1871. QScriptValue QIODevice_read(QScriptContext *context, QScriptEngine *engine);
  1872. @ This function is passed to the scripting engine.
  1873. @<Set up the scripting engine@>=
  1874. constructor = engine->newFunction(constructQFile);
  1875. value = engine->newQMetaObject(&QFile::staticMetaObject, constructor);
  1876. engine->globalObject().setProperty("QFile", value);
  1877. @ The implementation is trivial.
  1878. @<Functions for scripting@>=
  1879. QScriptValue constructQFile(QScriptContext *context, QScriptEngine *engine)
  1880. {
  1881. QScriptValue object =
  1882. engine->newQObject(new QFile(argument<QString>(0, context)));@/
  1883. setQFileProperties(object, engine);
  1884. return object;
  1885. }
  1886. @ |QFile| gets a wrapper around |remove()| to support deleting temporary files.
  1887. @<Functions for scripting@>=
  1888. void setQFileProperties(QScriptValue value, QScriptEngine *engine)
  1889. {
  1890. setQIODeviceProperties(value, engine);
  1891. value.setProperty("remove", engine->newFunction(QFile_remove));
  1892. }
  1893. QScriptValue QFile_remove(QScriptContext *context, QScriptEngine *engine)
  1894. {
  1895. QFile *self = getself<QFile *>(context);
  1896. bool retval = self->remove();
  1897. return QScriptValue(engine, retval);
  1898. }
  1899. @ Although we aren'@q'@>t going to create any instances of |QIODevice| directly,
  1900. subclasses such as |QFile| and |QBuffer| get two additional properties for
  1901. opening and closing the device.
  1902. In order to solve some class interoperability issues, a convenience method is
  1903. also added which is equivalent to creating a |QString| from the |QByteArray|
  1904. returned from the |readAll()| method.
  1905. @<Functions for scripting@>=
  1906. void setQIODeviceProperties(QScriptValue value, QScriptEngine *engine)
  1907. {
  1908. setQObjectProperties(value, engine);
  1909. value.setProperty("open", engine->newFunction(QIODevice_open));
  1910. value.setProperty("close", engine->newFunction(QIODevice_close));
  1911. value.setProperty("readToString",
  1912. engine->newFunction(QIODevice_readToString));
  1913. value.setProperty("putChar", engine->newFunction(QIODevice_putChar));
  1914. value.setProperty("writeString", engine->newFunction(QIODevice_writeString));
  1915. value.setProperty("writeBytes", engine->newFunction(QIODevice_writeBytes));
  1916. value.setProperty("readBytes", engine->newFunction(QIODevice_readBytes));
  1917. value.setProperty("peek", engine->newFunction(QIODevice_peek));
  1918. value.setProperty("read", engine->newFunction(QIODevice_read));
  1919. }
  1920. @ These are simple wrappers. In the case of the |open()| property, one argument
  1921. may be passed specifying the mode used for opening. The supported values for
  1922. this are 1 (Read Only), 2 (Write Only), and 3 (Read Write). If this argument is
  1923. not passed, it is assumed that the user wants read and write access.
  1924. @<Functions for scripting@>=
  1925. QScriptValue QIODevice_open(QScriptContext *context, QScriptEngine *)
  1926. {
  1927. QIODevice *self = getself<QIODevice *>(context);
  1928. bool retval = false;
  1929. if(context->argumentCount() == 1)
  1930. {
  1931. switch(argument<int>(0, context))
  1932. {
  1933. case 1:
  1934. retval = self->open(QIODevice::ReadOnly);
  1935. break;
  1936. case 2:
  1937. retval = self->open(QIODevice::WriteOnly);
  1938. break;
  1939. case 3:
  1940. retval = self->open(QIODevice::ReadWrite);
  1941. break;
  1942. default:
  1943. break;
  1944. }
  1945. }
  1946. else
  1947. {
  1948. retval = self->open(QIODevice::ReadWrite);
  1949. }
  1950. return QScriptValue(retval);
  1951. }
  1952. QScriptValue QIODevice_close(QScriptContext *context, QScriptEngine *)
  1953. {
  1954. QIODevice *self = getself<QIODevice *>(context);
  1955. self->close();
  1956. return QScriptValue();
  1957. }
  1958. @ The |readToString()| method is a simple extension of |QIODevice::readAll()| to
  1959. interface with classes that expect document data in the form of a string. Most
  1960. notably, this includes |QWebView|.
  1961. @<Functions for scripting@>=
  1962. QScriptValue QIODevice_readToString(QScriptContext *context, QScriptEngine *)
  1963. {
  1964. QIODevice *self = getself<QIODevice *>(context);
  1965. self->reset();
  1966. return QScriptValue(QString(self->readAll()));
  1967. }
  1968. @ In support of serial port communications, wrappers around two methods for
  1969. writing data have been added. As these are valid for other classes derived from
  1970. |QIODevice|, they are added here so the functionality is available more
  1971. broadly.
  1972. As we are unable to pass a type that guarantees only a single character, we
  1973. instead accept a string and only pass along the first character.
  1974. @<Functions for scripting@>=
  1975. QScriptValue QIODevice_putChar(QScriptContext *context, QScriptEngine *)
  1976. {
  1977. QIODevice *self = getself<QIODevice *>(context);
  1978. if(context->argumentCount() == 1)
  1979. {
  1980. return QScriptValue(self->putChar(argument<QString>(0, context).toUtf8().at(0)));
  1981. }
  1982. context->throwError("Incorrect number of arguments passed to "@|
  1983. "QIODevice::putChar()");
  1984. return QScriptValue();
  1985. }
  1986. @ Two wrappers are provided around |QIODevice::write()| for outputting
  1987. multi-byte data. If we are writing strings that are valid UTF-8, we can use the
  1988. |writeString| wrapper, but if we require full control over exactly which bytes
  1989. are output, the |writeBytes| wrapper is more appropriate.
  1990. @<Functions for scripting@>=
  1991. QScriptValue QIODevice_writeString(QScriptContext *context, QScriptEngine *)
  1992. {
  1993. QIODevice *self = getself<QIODevice *>(context);
  1994. if(context->argumentCount() == 1)
  1995. {
  1996. self->write(argument<QString>(0, context).toUtf8());
  1997. }
  1998. else
  1999. {
  2000. context->throwError("Incorrect number of arguments passed to "@|
  2001. "QIODevice::writeString()");
  2002. }
  2003. return QScriptValue();
  2004. }
  2005. QScriptValue QIODevice_writeBytes(QScriptContext *context, QScriptEngine *)
  2006. {
  2007. QIODevice *self = getself<QIODevice *>(context);
  2008. if(context->argumentCount() == 1)
  2009. {
  2010. self->write(argument<QByteArray>(0, context));
  2011. }
  2012. else
  2013. {
  2014. context->throwError("Incorrect number of arguments passed to "@|
  2015. "QIODevice::writeBytes()");
  2016. }
  2017. return QScriptValue();
  2018. }
  2019. @ The readBytes method is an alternate wrapper around |QByteArray::readAll()|
  2020. which returns the |QByteArray| instead of converting this to a |QString|.
  2021. @<Functions for scripting@>=
  2022. QScriptValue QIODevice_readBytes(QScriptContext *context, QScriptEngine *engine)
  2023. {
  2024. QIODevice *self = getself<QIODevice *>(context);
  2025. QScriptValue value = engine->toScriptValue<QByteArray>(self->readAll());
  2026. setQByteArrayProperties(value, engine);
  2027. return value;
  2028. }
  2029. @ Wrappers around |peek()| and |read()| are also provided.
  2030. @<Functions for scripting@>=
  2031. QScriptValue QIODevice_peek(QScriptContext *context, QScriptEngine *engine)
  2032. {
  2033. QIODevice *self = getself<QIODevice *>(context);
  2034. QScriptValue value = engine->toScriptValue<QByteArray>(
  2035. self->peek(argument<int>(0, context)));
  2036. setQByteArrayProperties(value, engine);
  2037. return value;
  2038. }
  2039. QScriptValue QIODevice_read(QScriptContext *context, QScriptEngine *engine)
  2040. {
  2041. QIODevice *self = getself<QIODevice *>(context);
  2042. QScriptValue value = engine->toScriptValue<QByteArray>(
  2043. self->read(argument<int>(0, context)));
  2044. setQByteArrayProperties(value, engine);
  2045. return value;
  2046. }
  2047. @* Scripting QProcess.
  2048. \noindent Sometimes it is useful to have \pn work with an external program.
  2049. The initial use case was document generation by typesetting instructions to a
  2050. file and then running \TeX to generate a shelf sign or a sheet of labels.
  2051. Other likely use cases include interfacing with external programs that output
  2052. measurement streams. There are several methods which we may want to expose,
  2053. however this is being done only as needed.
  2054. @<Function prototypes for scripting@>=
  2055. QScriptValue constructQProcess(QScriptContext *context, QScriptEngine *engine);
  2056. void setQProcessProperties(QScriptValue value, QScriptEngine *engine);
  2057. QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *engine);
  2058. QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *engine);
  2059. QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *engine);
  2060. QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *engine);
  2061. @ We follow the same pattern with this as with many other types.
  2062. @<Set up the scripting engine@>=
  2063. constructor = engine->newFunction(constructQProcess);
  2064. value = engine->newQMetaObject(&QProcess::staticMetaObject, constructor);
  2065. engine->globalObject().setProperty("QProcess", value);
  2066. @ The constructor is trivial.
  2067. @<Functions for scripting@>=
  2068. QScriptValue constructQProcess(QScriptContext *, QScriptEngine *engine)
  2069. {
  2070. QScriptValue object = engine->newQObject(new QProcess);
  2071. setQProcessProperties(object, engine);
  2072. return object;
  2073. }
  2074. @ As |QProcess| is a |QIODevice| we inherit some properties from that. We also
  2075. expose some details that are specific to |QProcess|.
  2076. @<Functions for scripting@>=
  2077. void setQProcessProperties(QScriptValue value, QScriptEngine *engine)
  2078. {
  2079. setQIODeviceProperties(value, engine);
  2080. value.setProperty("execute", engine->newFunction(QProcess_execute));
  2081. value.setProperty("startDetached", engine->newFunction(QProcess_startDetached));
  2082. value.setProperty("setWorkingDirectory", engine->newFunction(QProcess_setWorkingDirectory));
  2083. value.setProperty("start", engine->newFunction(QProcess_start));
  2084. }
  2085. @ The |execute()| method comes in two flavors: one with arguments and one without.
  2086. We always call the one with arguments and simply pass in an empty list if no
  2087. arguments are specified.
  2088. @<Functions for scripting@>=
  2089. QScriptValue QProcess_execute(QScriptContext *context, QScriptEngine *)
  2090. {
  2091. QProcess *self = getself<QProcess *>(context);
  2092. QString program = argument<QString>(0, context);
  2093. QStringList arguments = QStringList();
  2094. if(context->argumentCount() > 1) {
  2095. arguments = argument<QVariant>(1, context).toStringList();
  2096. }
  2097. int retval = self->execute(program, arguments);
  2098. return QScriptValue(retval);
  2099. }
  2100. @ Similarly |startDetached()| can be called in a few different ways.
  2101. @<Functions for scripting@>=
  2102. QScriptValue QProcess_startDetached(QScriptContext *context, QScriptEngine *)
  2103. {
  2104. QProcess *self = getself<QProcess *>(context);
  2105. QString program = argument<QString>(0, context);
  2106. QStringList arguments = QStringList();
  2107. if(context->argumentCount() > 1) {
  2108. arguments = argument<QVariant>(1, context).toStringList();
  2109. }
  2110. QString workingDirectory = "";
  2111. if(context->argumentCount() > 2) {
  2112. workingDirectory = argument<QString>(2, context);
  2113. }
  2114. bool retval;
  2115. switch(context->argumentCount())
  2116. {
  2117. case 1:
  2118. retval = self->startDetached(program);
  2119. break;
  2120. case 2:
  2121. retval = self->startDetached(program, arguments);
  2122. break;
  2123. case 3:
  2124. retval = self->startDetached(program, arguments, workingDirectory);
  2125. break;
  2126. default:
  2127. retval = false;
  2128. }
  2129. return QScriptValue(retval);
  2130. }
  2131. @ Sometimes we care about the working directory for our program.
  2132. @<Functions for scripting@>=
  2133. QScriptValue QProcess_setWorkingDirectory(QScriptContext *context, QScriptEngine *)
  2134. {
  2135. QProcess *self = getself<QProcess *>(context);
  2136. QString directory = argument<QString>(0, context);
  2137. self->setWorkingDirectory(directory);
  2138. return QScriptValue();
  2139. }
  2140. @ When using the |start()| method we always assume that we want read and write
  2141. access.
  2142. @<Functions for scripting@>=
  2143. QScriptValue QProcess_start(QScriptContext *context, QScriptEngine *)
  2144. {
  2145. QProcess *self = getself<QProcess *>(context);
  2146. QString program = argument<QString>(0, context);
  2147. QStringList arguments = QStringList();
  2148. if(context->argumentCount() > 1) {
  2149. arguments = argument<QVariant>(1, context).toStringList();
  2150. }
  2151. self->start(program, arguments);
  2152. return QScriptValue();
  2153. }
  2154. @ In order to work with |QByteArray| this should also be exposed to the host
  2155. environment.
  2156. @<Function prototypes for scripting@>=
  2157. QScriptValue QByteArray_toScriptValue(QScriptEngine *engine, const QByteArray &bytes);
  2158. void QByteArray_fromScriptValue(const QScriptValue &value, QByteArray &bytes);
  2159. QScriptValue constructQByteArray(QScriptContext *context, QScriptEngine *engine);
  2160. void setQByteArrayProperties(QScriptValue value, QScriptEngine *engine);
  2161. QScriptValue QByteArray_fromHex(QScriptContext *context, QScriptEngine *engine);
  2162. QScriptValue QByteArray_getAt(QScriptContext *context, QScriptEngine *engine);
  2163. QScriptValue QByteArray_setAt(QScriptContext *context, QScriptEngine *engine);
  2164. QScriptValue QByteArray_appendBytes(QScriptContext *context, QScriptEngine *engine);
  2165. QScriptValue QByteArray_appendString(QScriptContext *context, QScriptEngine *engine);
  2166. QScriptValue QByteArray_size(QScriptContext *context, QScriptEngine *engine);
  2167. QScriptValue QByteArray_left(QScriptContext *context, QScriptEngine *engine);
  2168. QScriptValue QByteArray_right(QScriptContext *context, QScriptEngine *engine);
  2169. QScriptValue QByteArray_mid(QScriptContext *context, QScriptEngine *engine);
  2170. QScriptValue QByteArray_chop(QScriptContext *context, QScriptEngine *engine);
  2171. QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine);
  2172. QScriptValue QByteArray_toInt8(QScriptContext *context, QScriptEngine *engine);
  2173. QScriptValue QByteArray_toInt16(QScriptContext *context, QScriptEngine *engine);
  2174. QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *engine);
  2175. QScriptValue QByteArray_toFloat(QScriptContext *context, QScriptEngine *engine);
  2176. QScriptValue QByteArray_toDouble(QScriptContext *context, QScriptEngine *engine);
  2177. @ First, we provide some functionns for moving array data across the
  2178. language barrier.
  2179. @<Functions for scripting@>=
  2180. QScriptValue QByteArray_toScriptValue(QScriptEngine *engine, const QByteArray &bytes)
  2181. {
  2182. QScriptValue object = engine->newVariant(QVariant(bytes));
  2183. setQByteArrayProperties(object, engine);
  2184. return object;
  2185. }
  2186. void QByteArray_fromScriptValue(const QScriptValue &value, QByteArray &bytes)
  2187. {
  2188. bytes = value.toVariant().toByteArray();
  2189. }
  2190. @ We register this our conversion functions and allow creation of new arrays
  2191. next.
  2192. @<Set up the scripting engine@>=
  2193. qScriptRegisterMetaType(engine, QByteArray_toScriptValue, QByteArray_fromScriptValue);
  2194. constructor = engine->newFunction(constructQByteArray);
  2195. engine->globalObject().setProperty("QByteArray", constructor);
  2196. @ The constructor is straightforward.
  2197. @<Functions for scripting@>=
  2198. QScriptValue constructQByteArray(QScriptContext *, QScriptEngine *engine)
  2199. {
  2200. QScriptValue object = engine->toScriptValue<QByteArray>(QByteArray());
  2201. setQByteArrayProperties(object, engine);
  2202. return object;
  2203. }
  2204. @ There are many methods which are not automatically available which we may
  2205. want to have wrappers around. These should be added as required.
  2206. @<Functions for scripting@>=
  2207. void setQByteArrayProperties(QScriptValue value, QScriptEngine *engine)
  2208. {
  2209. value.setProperty("fromHex", engine->newFunction(QByteArray_fromHex));
  2210. value.setProperty("getAt", engine->newFunction(QByteArray_getAt));
  2211. value.setProperty("setAt", engine->newFunction(QByteArray_setAt));
  2212. value.setProperty("appendBytes", engine->newFunction(QByteArray_appendBytes));
  2213. value.setProperty("appendString", engine->newFunction(QByteArray_appendString));
  2214. value.setProperty("size", engine->newFunction(QByteArray_size));
  2215. value.setProperty("left", engine->newFunction(QByteArray_left));
  2216. value.setProperty("right", engine->newFunction(QByteArray_right));
  2217. value.setProperty("mid", engine->newFunction(QByteArray_mid));
  2218. value.setProperty("chop", engine->newFunction(QByteArray_chop));
  2219. value.setProperty("remove", engine->newFunction(QByteArray_remove));
  2220. value.setProperty("toInt8", engine->newFunction(QByteArray_toInt8));
  2221. value.setProperty("toInt16", engine->newFunction(QByteArray_toInt16));
  2222. value.setProperty("toInt32", engine->newFunction(QByteArray_toInt32));
  2223. value.setProperty("toFloat", engine->newFunction(QByteArray_toFloat));
  2224. value.setProperty("toDouble", engine->newFunction(QByteArray_toDouble));
  2225. }
  2226. @ Perhaps the easiest way to deal with fixed byte strings for serial
  2227. communications across script boundaries is to use a hex encoded string.
  2228. @<Functions for scripting@>=
  2229. QScriptValue QByteArray_fromHex(QScriptContext *context, QScriptEngine *engine)
  2230. {
  2231. QByteArray self = getself<QByteArray>(context);
  2232. QByteArray retval;
  2233. retval = self.fromHex(argument<QString>(0, context).toUtf8());
  2234. QScriptValue value = engine->toScriptValue<QByteArray>(retval);
  2235. setQByteArrayProperties(value, engine);
  2236. return value;
  2237. }
  2238. @ A pair of methods is provided for getting and setting values at a particular
  2239. byte.
  2240. @<Functions for scripting@>=
  2241. QScriptValue QByteArray_getAt(QScriptContext *context, QScriptEngine *)
  2242. {
  2243. QByteArray self = getself<QByteArray>(context);
  2244. return QScriptValue((int)(self.at(argument<int>(0, context))));
  2245. }
  2246. QScriptValue QByteArray_setAt(QScriptContext *context, QScriptEngine *)
  2247. {
  2248. QByteArray self = getself<QByteArray>(context);
  2249. self[argument<int>(0, context)] = (char)(argument<int>(1, context));
  2250. return QScriptValue();
  2251. }
  2252. @ Methods are provided for appending either another |QByteArray| or a string
  2253. to a |QByteArray|. The only difference between these functions is the expected
  2254. argument type.
  2255. @<Functions for scripting@>=
  2256. QScriptValue QByteArray_appendBytes(QScriptContext *context, QScriptEngine *engine)
  2257. {
  2258. QByteArray self = getself<QByteArray>(context);
  2259. QScriptValue value =
  2260. engine->toScriptValue<QByteArray>(
  2261. self.append(argument<QByteArray>(0, context)));
  2262. setQByteArrayProperties(value, engine);
  2263. return value;
  2264. }
  2265. QScriptValue QByteArray_appendString(QScriptContext *context, QScriptEngine *engine)
  2266. {
  2267. QByteArray self = getself<QByteArray>(context);
  2268. QScriptValue value = engine->toScriptValue<QByteArray>(
  2269. self.append(argument<QString>(0, context)));
  2270. setQByteArrayProperties(value, engine);
  2271. return value;
  2272. }
  2273. @ Checking the size of our byte array frequently a requirement.
  2274. @<Functions for scripting@>=
  2275. QScriptValue QByteArray_size(QScriptContext *context, QScriptEngine *)
  2276. {
  2277. QByteArray self = getself<QByteArray>(context);
  2278. return QScriptValue(self.size());
  2279. }
  2280. @ It is also frequently useful to be able to work with specific parts of a byte
  2281. array, so a few methods are provided for carving these up.
  2282. @<Functions for scripting@>=
  2283. QScriptValue QByteArray_left(QScriptContext *context, QScriptEngine *engine)
  2284. {
  2285. QByteArray self = getself<QByteArray>(context);
  2286. QScriptValue value = engine->toScriptValue<QByteArray>(
  2287. self.left(argument<int>(0, context)));
  2288. setQByteArrayProperties(value, engine);
  2289. return value;
  2290. }
  2291. QScriptValue QByteArray_right(QScriptContext *context, QScriptEngine *engine)
  2292. {
  2293. QByteArray self = getself<QByteArray>(context);
  2294. QScriptValue value = engine->toScriptValue<QByteArray>(
  2295. self.right(argument<int>(0, context)));
  2296. setQByteArrayProperties(value, engine);
  2297. return value;
  2298. }
  2299. QScriptValue QByteArray_mid(QScriptContext *context, QScriptEngine *engine)
  2300. {
  2301. QByteArray self = getself<QByteArray>(context);
  2302. int length = -1;
  2303. if(context->argumentCount() > 1)
  2304. {
  2305. length = argument<int>(1, context);
  2306. }
  2307. QScriptValue value = engine->toScriptValue<QByteArray>(
  2308. self.mid(argument<int>(0, context), length));
  2309. setQByteArrayProperties(value, engine);
  2310. return value;
  2311. }
  2312. @ We may also want to remove bytes from an array.
  2313. @<Functions for scripting@>=
  2314. QScriptValue QByteArray_chop(QScriptContext *context, QScriptEngine *)
  2315. {
  2316. QByteArray self = getself<QByteArray>(context);
  2317. self.chop(argument<int>(0, context));
  2318. return QScriptValue();
  2319. }
  2320. QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine)
  2321. {
  2322. QByteArray self = getself<QByteArray>(context);
  2323. QScriptValue value = engine->toScriptValue<QByteArray>(
  2324. self.remove(argument<int>(0, context), argument<int>(1, context)));
  2325. setQByteArrayProperties(value, engine);
  2326. return value;
  2327. }
  2328. @ When receiving data in a byte array, bytes are sometimes intended to
  2329. represent 8, 16, or 32 bit integers. In such cases we often want to perform
  2330. some computation on these values so having the ability to split off that
  2331. portion of the array (for example, with |mid()|) and convert to a Number is
  2332. useful.
  2333. @<Functions for scripting@>=
  2334. QScriptValue QByteArray_toInt8(QScriptContext *context, QScriptEngine *)
  2335. {
  2336. QByteArray self = getself<QByteArray>(context);
  2337. int value = 0;
  2338. char *bytes = (char *)&value;
  2339. bytes[0] = self[0];
  2340. return QScriptValue(value);
  2341. }
  2342. QScriptValue QByteArray_toInt16(QScriptContext *context, QScriptEngine *)
  2343. {
  2344. QByteArray self = getself<QByteArray>(context);
  2345. int value = 0;
  2346. char *bytes = (char *)&value;
  2347. bytes[0] = self[0];
  2348. bytes[1] = self[1];
  2349. return QScriptValue(value);
  2350. }
  2351. QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *)
  2352. {
  2353. QByteArray self = getself<QByteArray>(context);
  2354. int value = 0;
  2355. char *bytes = (char *)&value;
  2356. bytes[0] = self[0];
  2357. bytes[1] = self[1];
  2358. bytes[2] = self[2];
  2359. bytes[3] = self[3];
  2360. return QScriptValue(value);
  2361. }
  2362. @ Similar methods are provided for converting bytes to a |float| or |double|.
  2363. Note that the return value from |toFloat| will, in the host environment, be
  2364. represented as a |double|.
  2365. @<Functions for scripting@>=
  2366. QScriptValue QByteArray_toFloat(QScriptContext *context, QScriptEngine *)
  2367. {
  2368. QByteArray self = getself<QByteArray>(context);
  2369. float value = 0.0;
  2370. char *bytes = (char *)&value;
  2371. bytes[0] = self[0];
  2372. bytes[1] = self[1];
  2373. bytes[2] = self[2];
  2374. bytes[3] = self[3];
  2375. return QScriptValue(value);
  2376. }
  2377. QScriptValue QByteArray_toDouble(QScriptContext *context, QScriptEngine *)
  2378. {
  2379. QByteArray self = getself<QByteArray>(context);
  2380. double value = 0.0;
  2381. char *bytes = (char *)&value;
  2382. bytes[0] = self[0];
  2383. bytes[1] = self[1];
  2384. bytes[2] = self[2];
  2385. bytes[3] = self[3];
  2386. bytes[4] = self[4];
  2387. bytes[5] = self[5];
  2388. bytes[6] = self[6];
  2389. bytes[7] = self[7];
  2390. return QScriptValue(value);
  2391. }
  2392. @ Some protocols require manipulating larger than 8 bit numbers as a sequence
  2393. of bytes. To facilitate this, methods are provided to construct a |QByteArray|
  2394. from different sized numbers. 8 bit numbers are provided for uniformity.
  2395. @<Function prototypes for scripting@>=
  2396. QScriptValue bytesFromInt8(QScriptContext *context, QScriptEngine *engine);
  2397. QScriptValue bytesFromInt16(QScriptContext *context, QScriptEngine *engine);
  2398. QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine);
  2399. QScriptValue bytesFromFloat(QScriptContext *context, QScriptEngine *engine);
  2400. QScriptValue bytesFromDouble(QScriptContext *context, QScriptEngine *engine);
  2401. @ These are globally available.
  2402. @<Set up the scripting engine@>=
  2403. engine->globalObject().setProperty("bytesFromInt8", engine->newFunction(bytesFromInt8));
  2404. engine->globalObject().setProperty("bytesFromInt16", engine->newFunction(bytesFromInt16));
  2405. engine->globalObject().setProperty("bytesFromInt32", engine->newFunction(bytesFromInt32));
  2406. engine->globalObject().setProperty("bytesFromFloat", engine->newFunction(bytesFromFloat));
  2407. engine->globalObject().setProperty("bytesFromDouble", engine->newFunction(bytesFromDouble));
  2408. @ The methods all work by casting the appropriate numeric type to a |char *|
  2409. and copying the bytes to a new |QByteArray|. Note that the ECMA-262 standard
  2410. only has one type of number and this is an IEEE 754 binary64 double precision
  2411. floating point number. Functions other than |bytesFromDouble| will be cast
  2412. from |double|.
  2413. @<Functions for scripting@>=
  2414. QScriptValue bytesFromInt8(QScriptContext *context, QScriptEngine *engine)
  2415. {
  2416. qint8 value = (qint8)(argument<int>(0, context));
  2417. char *bytes = (char *)&value;
  2418. QByteArray retval;
  2419. retval.resize(1);
  2420. retval[0] = bytes[0];
  2421. QScriptValue v = engine->toScriptValue<QByteArray>(retval);
  2422. setQByteArrayProperties(v, engine);
  2423. return v;
  2424. }
  2425. QScriptValue bytesFromInt16(QScriptContext *context, QScriptEngine *engine)
  2426. {
  2427. qint16 value = (qint16)(argument<int>(0, context));
  2428. char *bytes = (char *)&value;
  2429. QByteArray retval;
  2430. retval.resize(2);
  2431. retval[0] = bytes[0];
  2432. retval[1] = bytes[1];
  2433. QScriptValue v = engine->toScriptValue<QByteArray>(retval);
  2434. setQByteArrayProperties(v, engine);
  2435. return v;
  2436. }
  2437. QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine)
  2438. {
  2439. qint32 value = (qint32)(argument<int>(0, context));
  2440. char *bytes = (char *)&value;
  2441. QByteArray retval;
  2442. retval.resize(4);
  2443. retval[0] = bytes[0];
  2444. retval[1] = bytes[1];
  2445. retval[2] = bytes[2];
  2446. retval[3] = bytes[3];
  2447. QScriptValue v = engine->toScriptValue<QByteArray>(retval);
  2448. setQByteArrayProperties(v, engine);
  2449. return v;
  2450. }
  2451. QScriptValue bytesFromFloat(QScriptContext *context, QScriptEngine *engine)
  2452. {
  2453. float value = (float)(argument<double>(0, context));
  2454. char *bytes = (char *)&value;
  2455. QByteArray retval;
  2456. retval.resize(4);
  2457. retval[0] = bytes[0];
  2458. retval[1] = bytes[1];
  2459. retval[2] = bytes[2];
  2460. retval[3] = bytes[3];
  2461. QScriptValue v = engine->toScriptValue<QByteArray>(retval);
  2462. setQByteArrayProperties(v, engine);
  2463. return v;
  2464. }
  2465. QScriptValue bytesFromDouble(QScriptContext *context, QScriptEngine *engine)
  2466. {
  2467. double value = (double)(argument<double>(0, context));
  2468. char *bytes = (char *)&value;
  2469. QByteArray retval;
  2470. retval.resize(8);
  2471. retval[0] = bytes[0];
  2472. retval[1] = bytes[1];
  2473. retval[2] = bytes[2];
  2474. retval[3] = bytes[3];
  2475. retval[4] = bytes[4];
  2476. retval[5] = bytes[5];
  2477. retval[6] = bytes[6];
  2478. retval[7] = bytes[7];
  2479. QScriptValue v = engine->toScriptValue<QByteArray>(retval);
  2480. setQByteArrayProperties(v, engine);
  2481. return v;
  2482. }
  2483. @* Scripting QBuffer.
  2484. \noindent Sometimes it is desirable to load a roast profile from a file. At
  2485. other times, it is more useful to load that profile from a byte array stored in
  2486. a database. The |XMLInput| class takes data from a |QIODevice| object, which
  2487. means that we can choose from a |QFile| when we want the former or a |QBuffer|
  2488. when we want the latter.
  2489. @<Function prototypes for scripting@>=
  2490. QScriptValue constructQBuffer(QScriptContext *context, QScriptEngine *engine);
  2491. void setQBufferProperties(QScriptValue value, QScriptEngine *engine);
  2492. QScriptValue QBuffer_setData(QScriptContext *context, QScriptEngine *engine);
  2493. QScriptValue QBuffer_data(QScriptContext *context, QScriptEngine *engine);
  2494. @ The host environment needs to be aware of the constructor.
  2495. @<Set up the scripting engine@>=
  2496. constructor = engine->newFunction(constructQBuffer);
  2497. value = engine->newQMetaObject(&QBuffer::staticMetaObject, constructor);
  2498. engine->globalObject().setProperty("QBuffer", value);
  2499. @ The implementation is trivial.
  2500. @<Functions for scripting@>=
  2501. QScriptValue constructQBuffer(QScriptContext *context, QScriptEngine *engine)
  2502. {
  2503. QByteArray *array = new QByteArray(argument<QString>(0, context).toAscii());
  2504. QScriptValue object = engine->newQObject(new QBuffer(array));
  2505. setQBufferProperties(object, engine);
  2506. return object;
  2507. }
  2508. void setQBufferProperties(QScriptValue value, QScriptEngine *engine)
  2509. {
  2510. setQIODeviceProperties(value, engine);
  2511. value.setProperty("setData", engine->newFunction(QBuffer_setData));
  2512. value.setProperty("data", engine->newFunction(QBuffer_data));
  2513. }
  2514. QScriptValue QBuffer_setData(QScriptContext *context, QScriptEngine *)
  2515. {
  2516. QBuffer *self = getself<QBuffer *>(context);
  2517. self->setData(argument<QString>(0, context).toAscii());
  2518. return QScriptValue();
  2519. }
  2520. QScriptValue QBuffer_data(QScriptContext *context, QScriptEngine *)
  2521. {
  2522. QBuffer *self = getself<QBuffer *>(context);
  2523. return QScriptValue(QString(self->data()));
  2524. }
  2525. @* Scripting QXmlQuery.
  2526. \noindent Sometimes we have some XML data in a file or a buffer and we would
  2527. like to extract certain information from that data in the host environment.
  2528. Rather than write complicated string manipulation routines in an attempt to deal
  2529. with this sensibly, we can use the XQuery language to extract the information we
  2530. want. One common use case for this is extracting all measurements from a roast
  2531. profile that are associated with an annotation.
  2532. @<Function prototypes for scripting@>=
  2533. QScriptValue constructXQuery(QScriptContext *context, QScriptEngine *engine);
  2534. QScriptValue XQuery_bind(QScriptContext *context, QScriptEngine *engine);
  2535. QScriptValue XQuery_exec(QScriptContext *context, QScriptEngine *engine);
  2536. QScriptValue XQuery_setQuery(QScriptContext *context, QScriptEngine *engine);
  2537. QScriptValue XQuery_invalidate(QScriptContext *context, QScriptEngine *engine);
  2538. void setXQueryProperties(QScriptValue value, QScriptEngine *engine);
  2539. @ The constructor must be registered with the host environment. This is done a
  2540. bit differently from most classes as |QXmlQuery| is not a |QObject|.
  2541. @<Set up the scripting engine@>=
  2542. constructor = engine->newFunction(constructXQuery);
  2543. engine->globalObject().setProperty("XQuery", constructor);
  2544. @ The constructor just needs to make sure the functions we want to make
  2545. available are applied. A method is also provided to free the |QXmlQuery|.
  2546. @<Functions for scripting@>=
  2547. QScriptValue constructXQuery(QScriptContext *, QScriptEngine *engine)
  2548. {
  2549. QScriptValue object = engine->toScriptValue<void *>(new QXmlQuery);
  2550. setXQueryProperties(object, engine);
  2551. return object;
  2552. }
  2553. QScriptValue XQuery_invalidate(QScriptContext *context, QScriptEngine *)
  2554. {
  2555. QXmlQuery *self = getself<QXmlQuery *>(context);
  2556. delete self;
  2557. return QScriptValue();
  2558. }
  2559. void setXQueryProperties(QScriptValue value, QScriptEngine *engine)
  2560. {
  2561. value.setProperty("bind", engine->newFunction(XQuery_bind));
  2562. value.setProperty("exec", engine->newFunction(XQuery_exec));
  2563. value.setProperty("setQuery", engine->newFunction(XQuery_setQuery));
  2564. value.setProperty("invalidate", engine->newFunction(XQuery_invalidate));
  2565. }
  2566. @ The |bind()| property can be used to specify a |QIODevice| to be referenced by
  2567. a variable within a query.
  2568. @<Functions for scripting@>=
  2569. QScriptValue XQuery_bind(QScriptContext *context, QScriptEngine *)
  2570. {
  2571. QXmlQuery *self = getself<QXmlQuery *>(context);
  2572. QIODevice *buffer = argument<QIODevice *>(1, context);
  2573. self->bindVariable(argument<QString>(0, context), buffer);
  2574. return QScriptValue();
  2575. }
  2576. @ A method is also required for setting the query we wish to conduct.
  2577. @<Functions for scripting@>=
  2578. QScriptValue XQuery_setQuery(QScriptContext *context, QScriptEngine *)
  2579. {
  2580. QXmlQuery *self = getself<QXmlQuery *>(context);
  2581. self->setQuery(argument<QString>(0, context));
  2582. return QScriptValue();
  2583. }
  2584. @ This method runs the previously specified query.
  2585. @<Functions for scripting@>=
  2586. QScriptValue XQuery_exec(QScriptContext *context, QScriptEngine *)
  2587. {
  2588. QXmlQuery *self = getself<QXmlQuery *>(context);
  2589. QString result;
  2590. self->evaluateTo(&result);
  2591. return QScriptValue(result);
  2592. }
  2593. @* Scripting QXmlStreamWriter.
  2594. \noindent There are some cases where it may be desirable to produce XML from the
  2595. host environment. While there are several ways to accomplish this, the
  2596. |QXmlStreamWriter| class greatly simplifies generating complex XML documents.
  2597. This class is not related to |QObject|, so several functions are needed to
  2598. expose the functionality of this class to the host environment.
  2599. @<Function prototypes for scripting@>=
  2600. QScriptValue constructXmlWriter(QScriptContext *context, QScriptEngine *engine);
  2601. QScriptValue XmlWriter_setDevice(QScriptContext *context,
  2602. QScriptEngine *engine);
  2603. QScriptValue XmlWriter_writeAttribute(QScriptContext *context,
  2604. QScriptEngine *engine);
  2605. QScriptValue XmlWriter_writeCDATA(QScriptContext *context,
  2606. QScriptEngine *engine);
  2607. QScriptValue XmlWriter_writeCharacters(QScriptContext *context,
  2608. QScriptEngine *engine);
  2609. QScriptValue XmlWriter_writeDTD(QScriptContext *context, QScriptEngine *engine);
  2610. QScriptValue XmlWriter_writeEmptyElement(QScriptContext *context,
  2611. QScriptEngine *engine);
  2612. QScriptValue XmlWriter_writeEndDocument(QScriptContext *context,
  2613. QScriptEngine *engine);
  2614. QScriptValue XmlWriter_writeEndElement(QScriptContext *context,
  2615. QScriptEngine *engine);
  2616. QScriptValue XmlWriter_writeEntityReference(QScriptContext *context,
  2617. QScriptEngine *engine);
  2618. QScriptValue XmlWriter_writeProcessingInstruction(QScriptContext *context,
  2619. QScriptEngine *engine);
  2620. QScriptValue XmlWriter_writeStartDocument(QScriptContext *context,
  2621. QScriptEngine *engine);
  2622. QScriptValue XmlWriter_writeStartElement(QScriptContext *context,
  2623. QScriptEngine *engine);
  2624. QScriptValue XmlWriter_writeTextElement(QScriptContext *context,
  2625. QScriptEngine *engine);
  2626. void setXmlWriterProperties(QScriptValue value, QScriptEngine *engine);
  2627. @ The constructor must be registered with the host environment.
  2628. @<Set up the scripting engine@>=
  2629. constructor = engine->newFunction(constructXmlWriter);
  2630. engine->globalObject().setProperty("XmlWriter", constructor);
  2631. @ The constructor takes an optional argument allowing the output device to be
  2632. specified.
  2633. @<Functions for scripting@>=
  2634. QScriptValue constructXmlWriter(QScriptContext *context, QScriptEngine *engine)
  2635. {
  2636. QXmlStreamWriter *retval;
  2637. if(context->argumentCount() == 1)
  2638. {
  2639. retval = new QXmlStreamWriter(argument<QIODevice *>(0, context));
  2640. }
  2641. else
  2642. {
  2643. retval = new QXmlStreamWriter;
  2644. }
  2645. QScriptValue object = engine->toScriptValue<void *>(retval);
  2646. setXmlWriterProperties(object, engine);
  2647. return object;
  2648. }
  2649. void setXmlWriterProperties(QScriptValue value, QScriptEngine *engine)
  2650. {
  2651. value.setProperty("setDevice", engine->newFunction(XmlWriter_setDevice));
  2652. value.setProperty("writeAttribute",
  2653. engine->newFunction(XmlWriter_writeAttribute));
  2654. value.setProperty("writeCDATA", engine->newFunction(XmlWriter_writeCDATA));
  2655. value.setProperty("writeCharacters",
  2656. engine->newFunction(XmlWriter_writeCharacters));
  2657. value.setProperty("writeDTD", engine->newFunction(XmlWriter_writeDTD));
  2658. value.setProperty("writeEmptyElement",
  2659. engine->newFunction(XmlWriter_writeEmptyElement));
  2660. value.setProperty("writeEndDocument",
  2661. engine->newFunction(XmlWriter_writeEndDocument));
  2662. value.setProperty("writeEndElement",
  2663. engine->newFunction(XmlWriter_writeEndElement));
  2664. value.setProperty("writeEntityReference",
  2665. engine->newFunction(XmlWriter_writeEntityReference));
  2666. value.setProperty("writeProcessingInstruction",
  2667. engine->newFunction(XmlWriter_writeProcessingInstruction));
  2668. value.setProperty("writeStartDocument",
  2669. engine->newFunction(XmlWriter_writeStartDocument));
  2670. value.setProperty("writeStartElement",
  2671. engine->newFunction(XmlWriter_writeStartElement));
  2672. value.setProperty("writeTextElement",
  2673. engine->newFunction(XmlWriter_writeTextElement));
  2674. }
  2675. @ If the output device needs to be changed or if one is not passed to the
  2676. constructor, the |setDevice()| method can be used.
  2677. @<Functions for scripting@>=
  2678. QScriptValue XmlWriter_setDevice(QScriptContext *context, QScriptEngine *)
  2679. {
  2680. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2681. QIODevice *device = argument<QIODevice *>(0, context);
  2682. self->setDevice(device);
  2683. return QScriptValue();
  2684. }
  2685. @ The remaining functions are simple wrappers used for writing various types of
  2686. data. After creating a writer and setting the output device, the start of the
  2687. document should be written. One argument is required containing the XML version
  2688. number. Another function handles writing the end of the document.
  2689. @<Functions for scripting@>=
  2690. QScriptValue XmlWriter_writeStartDocument(QScriptContext *context,
  2691. QScriptEngine *)
  2692. {
  2693. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2694. self->writeStartDocument(argument<QString>(0, context));
  2695. return QScriptValue();
  2696. }
  2697. QScriptValue XmlWriter_writeEndDocument(QScriptContext *context,
  2698. QScriptEngine *)
  2699. {
  2700. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2701. self->writeEndDocument();
  2702. return QScriptValue();
  2703. }
  2704. @ After the start of the document, a DTD is commonly needed.
  2705. @<Functions for scripting@>=
  2706. QScriptValue XmlWriter_writeDTD(QScriptContext *context, QScriptEngine *)
  2707. {
  2708. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2709. self->writeDTD(argument<QString>(0, context));
  2710. return QScriptValue();
  2711. }
  2712. @ After this, elements need to be written. For this, we write the start
  2713. element, any attributes needed, character data, and the end element.
  2714. @<Functions for scripting@>=
  2715. QScriptValue XmlWriter_writeStartElement(QScriptContext *context,
  2716. QScriptEngine *)
  2717. {
  2718. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2719. self->writeStartElement(argument<QString>(0, context));
  2720. return QScriptValue();
  2721. }
  2722. QScriptValue XmlWriter_writeAttribute(QScriptContext *context, QScriptEngine *)
  2723. {
  2724. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2725. self->writeAttribute(argument<QString>(0, context),
  2726. argument<QString>(1, context));
  2727. return QScriptValue();
  2728. }
  2729. QScriptValue XmlWriter_writeCharacters(QScriptContext *context, QScriptEngine *)
  2730. {
  2731. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2732. self->writeCharacters(argument<QString>(0, context));
  2733. return QScriptValue();
  2734. }
  2735. QScriptValue XmlWriter_writeEndElement(QScriptContext *context, QScriptEngine *)
  2736. {
  2737. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2738. self->writeEndElement();
  2739. return QScriptValue();
  2740. }
  2741. @ For convenience, two other methods are provided for writing elements. Elements
  2742. which do not require anything between the start and end elements can be created
  2743. with |writeEmptyElement()|. Elements which do not require attributes, but do
  2744. contain text can be created with |writeTextElement()|.
  2745. @<Functions for scripting@>=
  2746. QScriptValue XmlWriter_writeEmptyElement(QScriptContext *context,
  2747. QScriptEngine *)
  2748. {
  2749. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2750. self->writeEmptyElement(argument<QString>(0, context));
  2751. return QScriptValue();
  2752. }
  2753. QScriptValue XmlWriter_writeTextElement(QScriptContext *context,
  2754. QScriptEngine *)
  2755. {
  2756. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2757. self->writeTextElement(argument<QString>(0, context),
  2758. argument<QString>(1, context));
  2759. return QScriptValue();
  2760. }
  2761. @ Less commonly needed are functions for writing CDATA sections, entity
  2762. references, and processing instructions.
  2763. @<Functions for scripting@>=
  2764. QScriptValue XmlWriter_writeCDATA(QScriptContext *context, QScriptEngine *)
  2765. {
  2766. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2767. self->writeCDATA(argument<QString>(0, context));
  2768. return QScriptValue();
  2769. }
  2770. QScriptValue XmlWriter_writeEntityReference(QScriptContext *context,
  2771. QScriptEngine *)
  2772. {
  2773. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2774. self->writeEntityReference(argument<QString>(0, context));
  2775. return QScriptValue();
  2776. }
  2777. QScriptValue XmlWriter_writeProcessingInstruction(QScriptContext *context,
  2778. QScriptEngine *)
  2779. {
  2780. QXmlStreamWriter *self = getself<QXmlStreamWriter *>(context);
  2781. self->writeProcessingInstruction(argument<QString>(0, context),
  2782. argument<QString>(1, context));
  2783. return QScriptValue();
  2784. }
  2785. @* Scripting QXmlStreamReader.
  2786. \noindent When a serializer is written using |QXmlStreamWriter|, a corresponding
  2787. deserializer should also be written. While there are several possible ways to do
  2788. this, using |QXmlStreamReader| is often the best choice. \pn{} provides a subset
  2789. of the functionality from this class which should be adequate for most purposes.
  2790. @<Function prototypes for scripting@>=
  2791. QScriptValue constructXmlReader(QScriptContext *context, QScriptEngine *engine);
  2792. QScriptValue XmlReader_atEnd(QScriptContext *context, QScriptEngine *engine);
  2793. QScriptValue XmlReader_attribute(QScriptContext *context,
  2794. QScriptEngine *engine);
  2795. QScriptValue XmlReader_hasAttribute(QScriptContext *context,
  2796. QScriptEngine *engine);
  2797. QScriptValue XmlReader_isDTD(QScriptContext *context, QScriptEngine *engine);
  2798. QScriptValue XmlReader_isStartElement(QScriptContext *context,
  2799. QScriptEngine *engine);
  2800. QScriptValue XmlReader_name(QScriptContext *context, QScriptEngine *engine);
  2801. QScriptValue XmlReader_readElementText(QScriptContext *context,
  2802. QScriptEngine *engine);
  2803. QScriptValue XmlReader_readNext(QScriptContext *context, QScriptEngine *engine);
  2804. QScriptValue XmlReader_text(QScriptContext *context, QScriptEngine *engine);
  2805. void setXmlReaderProperties(QScriptValue value, QScriptEngine *engine);
  2806. @ The constructor must be registered with the host environment.
  2807. @<Set up the scripting engine@>=
  2808. constructor = engine->newFunction(constructXmlReader);
  2809. engine->globalObject().setProperty("XmlReader", constructor);
  2810. @ The constructor requires an argument specifying the output device. This can be
  2811. any |QIODevice|. The |open()| method must be called on the device before passing
  2812. it as an argument to this function.
  2813. @<Functions for scripting@>=
  2814. QScriptValue constructXmlReader(QScriptContext *context, QScriptEngine *engine)
  2815. {
  2816. QXmlStreamReader *retval =
  2817. new QXmlStreamReader(argument<QIODevice *>(0, context));
  2818. QScriptValue object = engine->toScriptValue<void *>(retval);
  2819. setXmlReaderProperties(object, engine);
  2820. return object;
  2821. }
  2822. void setXmlReaderProperties(QScriptValue value, QScriptEngine *engine)
  2823. {
  2824. value.setProperty("atEnd", engine->newFunction(XmlReader_atEnd));
  2825. value.setProperty("attribute", engine->newFunction(XmlReader_attribute));
  2826. value.setProperty("hasAttribute",
  2827. engine->newFunction(XmlReader_hasAttribute));
  2828. value.setProperty("isDTD", engine->newFunction(XmlReader_isDTD));
  2829. value.setProperty("isStartElement",
  2830. engine->newFunction(XmlReader_isStartElement));
  2831. value.setProperty("name", engine->newFunction(XmlReader_name));
  2832. value.setProperty("readElementText",
  2833. engine->newFunction(XmlReader_readElementText));
  2834. value.setProperty("readNext",
  2835. engine->newFunction(XmlReader_readNext));
  2836. value.setProperty("text", engine->newFunction(XmlReader_text));
  2837. }
  2838. @ Most of the functions are simple member function wrappers. Two of these
  2839. properties are not. These are the |attribute()| and |hasAttribute()| properties.
  2840. @<Functions for scripting@>=
  2841. QScriptValue XmlReader_attribute(QScriptContext *context, QScriptEngine *)
  2842. {
  2843. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2844. QString retval =
  2845. self->attributes().value(argument<QString>(0, context)).toString();
  2846. return QScriptValue(retval);
  2847. }
  2848. QScriptValue XmlReader_hasAttribute(QScriptContext *context, QScriptEngine *)
  2849. {
  2850. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2851. bool retval =
  2852. self->attributes().hasAttribute(argument<QString>(0, context));
  2853. return QScriptValue(retval);
  2854. }
  2855. @ Other properties can be used for determining how to proceed with the
  2856. processing.
  2857. @<Functions for scripting@>=
  2858. QScriptValue XmlReader_atEnd(QScriptContext *context, QScriptEngine *)
  2859. {
  2860. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2861. return QScriptValue(self->atEnd());
  2862. }
  2863. QScriptValue XmlReader_isDTD(QScriptContext *context, QScriptEngine *)
  2864. {
  2865. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2866. return QScriptValue(self->isDTD());
  2867. }
  2868. QScriptValue XmlReader_isStartElement(QScriptContext *context, QScriptEngine *)
  2869. {
  2870. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2871. return QScriptValue(self->isStartElement());
  2872. }
  2873. @ We move from one element to the next with the |readNext()| property.
  2874. @<Functions for scripting@>=
  2875. QScriptValue XmlReader_readNext(QScriptContext *context, QScriptEngine *)
  2876. {
  2877. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2878. self->readNext();
  2879. return QScriptValue();
  2880. }
  2881. @ The remaining properties return the element name and text.
  2882. @<Functions for scripting@>=
  2883. QScriptValue XmlReader_name(QScriptContext *context, QScriptEngine *)
  2884. {
  2885. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2886. return QScriptValue(self->name().toString());
  2887. }
  2888. QScriptValue XmlReader_readElementText(QScriptContext *context, QScriptEngine *)
  2889. {
  2890. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2891. return QScriptValue(self->readElementText());
  2892. }
  2893. QScriptValue XmlReader_text(QScriptContext *context, QScriptEngine *)
  2894. {
  2895. QXmlStreamReader *self = getself<QXmlStreamReader *>(context);
  2896. return QScriptValue(self->text().toString());
  2897. }
  2898. @* Scripting QSettings.
  2899. \noindent Rather than have a script create a |QSettings| object when it needs to
  2900. save or load settings, the object is provided along with properties for getting
  2901. and setting values. Two functions are needed for this along with a third which
  2902. ensures any properties added to |QObject| are also available to |QSettings| from
  2903. the host environment.
  2904. @<Function prototypes for scripting@>=
  2905. QScriptValue QSettings_value(QScriptContext *context, QScriptEngine *engine);
  2906. QScriptValue QSettings_setValue(QScriptContext *context, QScriptEngine *engine);
  2907. void setQSettingsProperties(QScriptValue value, QScriptEngine *engine);
  2908. @ The object with properties for these functions is passed to the scripting
  2909. engine.
  2910. @<Set up the scripting engine@>=
  2911. value = engine->newQObject(&settings);
  2912. setQSettingsProperties(value, engine);
  2913. engine->globalObject().setProperty("QSettings", value);
  2914. @ Adding properties to the |QSettings| object should seem familiar.
  2915. @<Functions for scripting@>=
  2916. void setQSettingsProperties(QScriptValue value, QScriptEngine *engine)
  2917. {
  2918. setQObjectProperties(value, engine);
  2919. value.setProperty("value", engine->newFunction(QSettings_value));
  2920. value.setProperty("setValue", engine->newFunction(QSettings_setValue));
  2921. }
  2922. @ When getting a value from saved settings, there is the possibility that there
  2923. will not be a value saved for the requested key. An optional second argument can
  2924. be used to supply a default value.
  2925. @<Functions for scripting@>=
  2926. QScriptValue QSettings_value(QScriptContext *context, QScriptEngine *engine)
  2927. {
  2928. QScriptValue object;
  2929. if(context->argumentCount() == 1 || context->argumentCount() == 2)
  2930. {
  2931. QSettings settings;
  2932. QString key = argument<QString>(0, context);
  2933. QVariant value;
  2934. QVariant retval;
  2935. if(context->argumentCount() > 1)
  2936. {
  2937. value = argument<QVariant>(1, context);
  2938. retval = settings.value(key, value);
  2939. }
  2940. else
  2941. {
  2942. retval = settings.value(key);
  2943. }
  2944. object = engine->newVariant(retval);
  2945. }
  2946. else
  2947. {
  2948. context->throwError("Incorrect number of arguments passed to "@|
  2949. "QSettings::value(). This method takes one "@|
  2950. "string and one optional variant type.");
  2951. }
  2952. return object;
  2953. }
  2954. QScriptValue QSettings_setValue(QScriptContext *context, QScriptEngine *)
  2955. {
  2956. if(context->argumentCount() == 2)
  2957. {
  2958. QSettings settings;
  2959. QString key = argument<QString>(0, context);
  2960. QVariant value = argument<QVariant>(1, context);
  2961. settings.setValue(key, value);
  2962. }
  2963. else
  2964. {
  2965. context->throwError("Incorrect number of arguments passed to "@|
  2966. "QSettings::setValue(). This method takes one "@|
  2967. "string and one variant type for a total of two "@|
  2968. "arguments.");
  2969. }
  2970. return QScriptValue();
  2971. }
  2972. @* Scripting QLCDNumber.
  2973. \noindent |QLCDNumber| is used as a base class for \pn{}'@q'@>s |TemperatureDisplay|
  2974. and |TimerDisplay| classes, but it can also be used on its own for the display
  2975. of mainly numeric information.
  2976. @<Function prototypes for scripting@>=
  2977. QScriptValue constructQLCDNumber(QScriptContext *context,
  2978. QScriptEngine *engine);
  2979. void setQLCDNumberProperties(QScriptValue value, QScriptEngine *engine);
  2980. @ The constructor must be passed to the scripting engine.
  2981. @<Set up the scripting engine@>=
  2982. constructor = engine->newFunction(constructQLCDNumber);
  2983. value = engine->newQMetaObject(&QLCDNumber::staticMetaObject, constructor);
  2984. engine->globalObject().setProperty("QLCDNumber", value);
  2985. @ There is nothing special about the implementation.
  2986. @<Functions for scripting@>=
  2987. QScriptValue constructQLCDNumber(QScriptContext *, QScriptEngine *engine)
  2988. {
  2989. QScriptValue object = engine->newQObject(new QLCDNumber());
  2990. setQLCDNumberProperties(object, engine);
  2991. return object;
  2992. }
  2993. void setQLCDNumberProperties(QScriptValue value, QScriptEngine *engine)
  2994. {
  2995. setQFrameProperties(value, engine);
  2996. }
  2997. @* Scripting QTime.
  2998. \noindent |QTime| is a little different from the classes examined so far. This
  2999. class can be used for synchonizing time among various objects by creating a
  3000. common base reference time. This should not be needed as ECMA-262 already
  3001. specifies a |Date| class, however this has historically been troublesome to use.
  3002. One thing that makes this class different is that it is not related to
  3003. |QObject|. This makes usefully exposing it to the scripting engine a little more
  3004. difficult.
  3005. @<Function prototypes for scripting@>=
  3006. QScriptValue constructQTime(QScriptContext *context, QScriptEngine *engine);
  3007. QScriptValue QTime_addMSecs(QScriptContext *context, QScriptEngine *engine);
  3008. QScriptValue QTime_addSecs(QScriptContext *context, QScriptEngine *engine);
  3009. QScriptValue QTime_elapsed(QScriptContext *context, QScriptEngine *engine);
  3010. QScriptValue QTime_hour(QScriptContext *context, QScriptEngine *engine);
  3011. QScriptValue QTime_isNull(QScriptContext *context, QScriptEngine *engine);
  3012. QScriptValue QTime_isValid(QScriptContext *context, QScriptEngine *engine);
  3013. QScriptValue QTime_minute(QScriptContext *context, QScriptEngine *engine);
  3014. QScriptValue QTime_msec(QScriptContext *context, QScriptEngine *engine);
  3015. QScriptValue QTime_msecsTo(QScriptContext *context, QScriptEngine *engine);
  3016. QScriptValue QTime_restart(QScriptContext *context, QScriptEngine *engine);
  3017. QScriptValue QTime_second(QScriptContext *context, QScriptEngine *engine);
  3018. QScriptValue QTime_secsTo(QScriptContext *context, QScriptEngine *engine);
  3019. QScriptValue QTime_setHMS(QScriptContext *context, QScriptEngine *engine);
  3020. QScriptValue QTime_start(QScriptContext *context, QScriptEngine *engine);
  3021. QScriptValue QTime_toString(QScriptContext *context, QScriptEngine *engine);
  3022. QScriptValue QTime_currentTime(QScriptContext *context, QScriptEngine *engine);
  3023. QScriptValue QTime_fromString(QScriptContext *context, QScriptEngine *engine);
  3024. QScriptValue QTime_valueOf(QScriptContext *context, QScriptEngine *engine);
  3025. void setQTimeProperties(QScriptValue value, QScriptEngine *engine);
  3026. @ We must tell the script engine about the constructor. This is not done in
  3027. quite the same way as is done for |QObject| derived types.
  3028. @<Set up the scripting engine@>=
  3029. constructor = engine->newFunction(constructQTime);
  3030. engine->globalObject().setProperty("QTime", constructor);
  3031. @ The constructor has a couple interesting twists. The first is the ability to
  3032. accept a variable number of integer arguments. The other is that |QTime| is not
  3033. derived from |QObject|. The lack of |break| statements in the |switch| is
  3034. intended.
  3035. @<Functions for scripting@>=
  3036. QScriptValue constructQTime(QScriptContext *context,
  3037. QScriptEngine *engine)
  3038. {
  3039. QScriptValue object;
  3040. if(context->argumentCount() == 0 ||
  3041. (context->argumentCount() >= 2 && context->argumentCount() <= 4))@/
  3042. {
  3043. int arg1 = 0;
  3044. int arg2 = 0;
  3045. int arg3 = 0;
  3046. int arg4 = 0;
  3047. switch(context->argumentCount())
  3048. {@t\1@>@/
  3049. case 4:@/
  3050. arg4 = argument<int>(3, context);
  3051. case 3:@/
  3052. arg3 = argument<int>(2, context);
  3053. case 2:@/
  3054. arg2 = argument<int>(1, context);
  3055. arg1 = argument<int>(0, context);
  3056. default:@/
  3057. break;@t\2@>@/
  3058. }
  3059. if(context->argumentCount())
  3060. {
  3061. object = engine->toScriptValue<QTime>(QTime(arg1, arg2, arg3,
  3062. arg4));
  3063. }
  3064. else
  3065. {
  3066. object = engine->toScriptValue<QTime>(QTime());
  3067. }
  3068. setQTimeProperties(object, engine);
  3069. }
  3070. else
  3071. {
  3072. context->throwError("Incorrect number of arguments passed to "@|
  3073. "QTime::QTime(). This method takes zero, two, "@|
  3074. "three, or four integer arguments.");
  3075. }
  3076. return object;
  3077. }
  3078. @ In order to use the various |QTime| methods, we must add wrapper functions as
  3079. properties of newly created script objects. The last two of these should really
  3080. be callable without starting from an existing |QTime|.
  3081. @<Functions for scripting@>=
  3082. void setQTimeProperties(QScriptValue value, QScriptEngine *engine)
  3083. {
  3084. value.setProperty("addMSecs", engine->newFunction(QTime_addMSecs));
  3085. value.setProperty("addSecs", engine->newFunction(QTime_addSecs));
  3086. value.setProperty("elapsed", engine->newFunction(QTime_elapsed));
  3087. value.setProperty("hour", engine->newFunction(QTime_hour));
  3088. value.setProperty("isNull", engine->newFunction(QTime_isNull));
  3089. value.setProperty("isValid", engine->newFunction(QTime_isValid));
  3090. value.setProperty("minute", engine->newFunction(QTime_minute));
  3091. value.setProperty("msec", engine->newFunction(QTime_msec));
  3092. value.setProperty("msecsTo", engine->newFunction(QTime_msecsTo));
  3093. value.setProperty("restart", engine->newFunction(QTime_restart));
  3094. value.setProperty("second", engine->newFunction(QTime_second));
  3095. value.setProperty("secsTo", engine->newFunction(QTime_secsTo));
  3096. value.setProperty("setHMS", engine->newFunction(QTime_setHMS));
  3097. value.setProperty("start", engine->newFunction(QTime_start));
  3098. value.setProperty("toString", engine->newFunction(QTime_toString));
  3099. value.setProperty("currentTime", engine->newFunction(QTime_currentTime));
  3100. value.setProperty("fromString", engine->newFunction(QTime_fromString));
  3101. value.setProperty("valueOf", engine->newFunction(QTime_valueOf));
  3102. }
  3103. @ The |valueOf()| method exposes a numeric representation of the time
  3104. suitable for use in comparing two time values. With this it is possible to
  3105. take two |QTime| values in script code {\tt t1} and {\tt t2} and get the
  3106. expected results from {\tt t1 == t2}, {\tt t1 < t2}, {\tt t1 > t2} and
  3107. similar comparative operations.
  3108. @<Functions for scripting@>=
  3109. QScriptValue QTime_valueOf(QScriptContext *context, QScriptEngine *)
  3110. {
  3111. QTime self = getself<QTime>(context);
  3112. int retval = (self.hour() * 60 * 60 * 1000) + (self.minute() * 60 * 1000) +
  3113. (self.second() * 1000) + self.msec();
  3114. return QScriptValue(retval);
  3115. }
  3116. @ These functions are effectively wrapper functions around existing |QTime|
  3117. functionality with some error checking for the scripting engine.
  3118. The |addMSecs()| and |addSecs()| methods return a new |QTime| object.
  3119. @<Functions for scripting@>=
  3120. QScriptValue QTime_addMSecs(QScriptContext *context, QScriptEngine *engine)
  3121. {
  3122. QTime time;
  3123. QScriptValue retval;
  3124. if(context->argumentCount() == 1)
  3125. {
  3126. QTime self = getself<QTime>(context);
  3127. time = self.addMSecs(argument<int>(0, context));
  3128. retval = engine->toScriptValue<QTime>(time);
  3129. setQTimeProperties(retval, engine);
  3130. }
  3131. else
  3132. {
  3133. context->throwError("Incorrect number of arguments passed to "@|
  3134. "QTime::addMSecs(). This method takes one "@|
  3135. "integer as an argument.");
  3136. }
  3137. return retval;
  3138. }
  3139. QScriptValue QTime_addSecs(QScriptContext *context, QScriptEngine *engine)
  3140. {
  3141. QTime time;
  3142. QScriptValue retval;
  3143. if(context->argumentCount() == 1)
  3144. {
  3145. QTime self = getself<QTime>(context);
  3146. time = self.addSecs(argument<int>(0, context));
  3147. retval = engine->toScriptValue<QTime>(time);
  3148. setQTimeProperties(retval, engine);
  3149. }
  3150. else
  3151. {
  3152. context->throwError("Incorrect number of arguments passed to "@|
  3153. "QTime::addSecs(). This method takes one "@|
  3154. "integer as an argument.");
  3155. }
  3156. return retval;
  3157. }
  3158. @ The |elapsed()| method returns an integer value.
  3159. @<Functions for scripting@>=
  3160. QScriptValue QTime_elapsed(QScriptContext *context, QScriptEngine *engine)
  3161. {
  3162. QScriptValue retval;
  3163. if(context->argumentCount() == 0)
  3164. {
  3165. QTime self = getself<QTime>(context);
  3166. retval = QScriptValue(engine, self.elapsed());
  3167. }
  3168. else
  3169. {
  3170. context->throwError("Incorrect number of arguments passed to "@|
  3171. "QTime::elapsed(). This method takes no "@|
  3172. "arguments.");
  3173. }
  3174. return retval;
  3175. }
  3176. @ The |hour()|, |minute()|, |second()| and |msec()| methods return an integer
  3177. with various parts of the time. The |hour()| method is typical of these methods.
  3178. @<Functions for scripting@>=
  3179. QScriptValue QTime_hour(QScriptContext *context, QScriptEngine *engine)
  3180. {
  3181. QScriptValue retval;
  3182. if(context->argumentCount() == 0)
  3183. {
  3184. QTime self = getself<QTime>(context);
  3185. retval = QScriptValue(engine, self.hour());
  3186. }
  3187. else
  3188. {
  3189. context->throwError("Incorrect number of arguments passed to "@|
  3190. "QTime::hour(). This method takes no "@|
  3191. "arguments.");
  3192. }
  3193. return retval;
  3194. }
  3195. @ The |minute()|, |second()|, and |msec()| methods are implemented similarly.
  3196. @<Functions for scripting@>=
  3197. QScriptValue QTime_minute(QScriptContext *context, QScriptEngine *engine)
  3198. {
  3199. QScriptValue retval;
  3200. if(context->argumentCount() == 0)
  3201. {
  3202. QTime self = getself<QTime>(context);
  3203. retval = QScriptValue(engine, self.minute());
  3204. }
  3205. else
  3206. {
  3207. context->throwError("Incorrect number of arguments passed to "@|
  3208. "QTime::minute(). This method takes no "@|
  3209. "arguments.");
  3210. }
  3211. return retval;
  3212. }
  3213. QScriptValue QTime_second(QScriptContext *context, QScriptEngine *engine)
  3214. {
  3215. QScriptValue retval;
  3216. if(context->argumentCount() == 0)
  3217. {
  3218. QTime self = getself<QTime>(context);
  3219. retval = QScriptValue(engine, self.second());
  3220. }
  3221. else
  3222. {
  3223. context->throwError("Incorrect number of arguments passed to "@|
  3224. "QTime::second(). This method takes no "@|
  3225. "arguments.");
  3226. }
  3227. return retval;
  3228. }
  3229. QScriptValue QTime_msec(QScriptContext *context, QScriptEngine *engine)
  3230. {
  3231. QScriptValue retval;
  3232. if(context->argumentCount() == 0)
  3233. {
  3234. QTime self = getself<QTime>(context);
  3235. retval = QScriptValue(engine, self.msec());
  3236. }
  3237. else
  3238. {
  3239. context->throwError("Incorrect number of arguments passed to "@|
  3240. "QTime::msec(). This method takes no "@|
  3241. "arguments.");
  3242. }
  3243. return retval;
  3244. }
  3245. @ The |isNull()| and |isValid()| methods return a boolean value. A |QTime| is
  3246. considered null if it was created with a constructor with no arguments. It is
  3247. considered invalid if it is null or if part of the time is out of range.
  3248. @<Functions for scripting@>=
  3249. QScriptValue QTime_isNull(QScriptContext *context, QScriptEngine *engine)
  3250. {
  3251. QScriptValue retval;
  3252. if(context->argumentCount() == 0)
  3253. {
  3254. QTime self = getself<QTime>(context);
  3255. retval = QScriptValue(engine, self.isNull());
  3256. }
  3257. else
  3258. {
  3259. context->throwError("Incorrect number of arguments passed to "@|
  3260. "QTime::isNull(). This method takes no "@|
  3261. "arguments.");
  3262. }
  3263. return retval;
  3264. }
  3265. QScriptValue QTime_isValid(QScriptContext *context, QScriptEngine *engine)
  3266. {
  3267. QScriptValue retval;
  3268. if(context->argumentCount() == 0)
  3269. {
  3270. QTime self = getself<QTime>(context);
  3271. retval = QScriptValue(engine, self.isValid());
  3272. }
  3273. else
  3274. {
  3275. context->throwError("Incorrect number of arguments passed to "@|
  3276. "QTime::isValid(). This method takes no "@|
  3277. "arguments.");
  3278. }
  3279. return retval;
  3280. }
  3281. @ The |secsTo()| and |msecsTo()| methods return an integer value indicating the
  3282. number of seconds or milliseconds until a |QTime| argument.
  3283. @<Functions for scripting@>=
  3284. QScriptValue QTime_msecsTo(QScriptContext *context, QScriptEngine *engine)
  3285. {
  3286. QScriptValue retval;
  3287. if(context->argumentCount() == 1)
  3288. {
  3289. QTime self = getself<QTime>(context);
  3290. QTime arg = argument<QVariant>(0, context).toTime();
  3291. retval = QScriptValue(engine, self.msecsTo(arg));
  3292. }
  3293. else
  3294. {
  3295. context->throwError("Incorrect number of arguments passed to "@|
  3296. "QTime::msecsTo(). This method takes one QTime.");
  3297. }
  3298. return retval;
  3299. }
  3300. QScriptValue QTime_secsTo(QScriptContext *context, QScriptEngine *engine)
  3301. {
  3302. QScriptValue retval;
  3303. if(context->argumentCount() == 1)
  3304. {
  3305. QTime self = getself<QTime>(context);
  3306. QTime arg = argument<QVariant>(0, context).toTime();
  3307. retval = QScriptValue(engine, self.secsTo(arg));
  3308. }
  3309. else
  3310. {
  3311. context->throwError("Incorrect number of arguments passed to "@|
  3312. "QTime::secsTo(). This method takes one QTime.");
  3313. }
  3314. return retval;
  3315. }
  3316. @ The |start()| and |restart()| methods each set the value of the |QTime()| to
  3317. the current time. The |restart()| method additionally returns the same value as
  3318. the |elapsed()| method.
  3319. @<Functions for scripting@>=
  3320. QScriptValue QTime_restart(QScriptContext *context, QScriptEngine *engine)
  3321. {
  3322. QScriptValue retval;
  3323. if(context->argumentCount() == 0)
  3324. {
  3325. QTime self = getself<QTime>(context);
  3326. retval = QScriptValue(engine, self.restart());
  3327. }
  3328. else
  3329. {
  3330. context->throwError("Incorrect number of arguments passed to "@|
  3331. "QTime::restart(). This method takes no "@|
  3332. "arguments.");
  3333. }
  3334. return retval;
  3335. }
  3336. QScriptValue QTime_start(QScriptContext *context, QScriptEngine *)
  3337. {
  3338. if(context->argumentCount() == 0)
  3339. {
  3340. QTime self = getself<QTime>(context);
  3341. self.start();
  3342. }
  3343. else
  3344. {
  3345. context->throwError("Incorrect number of arguments passed to "@|
  3346. "QTime::start(). This method takes no arguments.");
  3347. }
  3348. return QScriptValue();
  3349. }
  3350. @ The slightly inappropriately named |setHMS()| method changes the current value
  3351. of the time and returns a boolean to indicate if the new time value is valid.
  3352. @<Functions for scripting@>=
  3353. QScriptValue QTime_setHMS(QScriptContext *context, QScriptEngine *engine)
  3354. {
  3355. QScriptValue retval;
  3356. if(context->argumentCount() == 3 || context->argumentCount() == 4)
  3357. {
  3358. QTime self = getself<QTime>(context);
  3359. int arg1 = 0;
  3360. int arg2 = 0;
  3361. int arg3 = 0;
  3362. int arg4 = 0;
  3363. switch(context->argumentCount())@/
  3364. {@t\1@>@/
  3365. case 4:@/
  3366. arg4 = argument<int>(3, context);
  3367. case 3:@/
  3368. arg3 = argument<int>(2, context);
  3369. arg2 = argument<int>(1, context);
  3370. arg1 = argument<int>(0, context);
  3371. default:@/
  3372. break;@t\2@>@/
  3373. }
  3374. retval = QScriptValue(engine, self.setHMS(arg1, arg2, arg3, arg4));
  3375. }
  3376. else
  3377. {
  3378. context->throwError("Incorrect number of arguments passed to "@|
  3379. "QTime::setHMS(). This method takes three or "@|
  3380. "four integer arguments.");
  3381. }
  3382. return retval;
  3383. }
  3384. @ The |toString()| method returns a string representation of the time. See the
  3385. Qt documentation for instructions on creating a valid format string.
  3386. @<Functions for scripting@>=
  3387. QScriptValue QTime_toString(QScriptContext *context, QScriptEngine *engine)
  3388. {
  3389. QScriptValue retval;
  3390. if(context->argumentCount() == 1)
  3391. {
  3392. QTime self = getself<QTime>(context);
  3393. retval = QScriptValue(engine, self.toString(argument<QString>(0, context)));
  3394. }
  3395. else
  3396. {
  3397. context->throwError("Incorrect number of arguments passed to "@|
  3398. "QTime::toString(). This method takes one QString "@|
  3399. "as an argument.");
  3400. }
  3401. return retval;
  3402. }
  3403. @ The |currentTime()| and |fromString()| methods return a new |QTime| object.
  3404. These methods make no reference to the any other existing |QTime|.
  3405. @<Functions for scripting@>=
  3406. QScriptValue QTime_currentTime(QScriptContext *, QScriptEngine *engine)
  3407. {
  3408. QScriptValue object;
  3409. object = engine->toScriptValue<QTime>(QTime::currentTime());
  3410. setQTimeProperties(object, engine);
  3411. return object;
  3412. }
  3413. QScriptValue QTime_fromString(QScriptContext *context, QScriptEngine *engine)
  3414. {
  3415. QScriptValue object;
  3416. if(context->argumentCount() == 2)
  3417. {
  3418. QString time = argument<QString>(0, context);
  3419. QString format = argument<QString>(1, context);
  3420. object = engine->toScriptValue<QTime>(QTime::fromString(time, format));
  3421. setQTimeProperties(object, engine);
  3422. }
  3423. else
  3424. {
  3425. context->throwError("Incorrect number of arguments passed to "@|
  3426. "QTime::fromString(). This method takes two "@|
  3427. "string arguments.");
  3428. }
  3429. return object;
  3430. }
  3431. @ In order to pass |QTime| objects back from a script, we also need to overload
  3432. |argument()| for this type.
  3433. @<Functions for scripting@>=
  3434. template<> QTime argument(int arg, QScriptContext *context)
  3435. {
  3436. return qscriptvalue_cast<QTime>(context->argument(arg));
  3437. }
  3438. @* Scripting QColor.
  3439. \noindent |QColor| support is limited to creating colors from strings to pass
  3440. to objects expecting a color.
  3441. @<Function prototypes for scripting@>=
  3442. QScriptValue constructQColor(QScriptContext *context, QScriptEngine *engine);
  3443. @ We must tell the script engine about the constructor. This is not done in
  3444. quite the same way as is done for |QObject| derived types.
  3445. @<Set up the scripting engine@>=
  3446. constructor = engine->newFunction(constructQColor);
  3447. engine->globalObject().setProperty("QColor", constructor);
  3448. @ The constructor is trivial.
  3449. @<Functions for scripting@>=
  3450. QScriptValue constructQColor(QScriptContext *context, QScriptEngine *engine)
  3451. {
  3452. QScriptValue object = engine->toScriptValue<QColor>(QColor(argument<QString>(0, context)));
  3453. return object;
  3454. }
  3455. @* Scripting QBrush.
  3456. \noindent |QBrush| support is limited to creating brushes from color strings to
  3457. pass to objects expecting a brush.
  3458. @<Function prototypes for scripting@>=
  3459. QScriptValue constructQBrush(QScriptContext *context, QScriptEngine *engine);
  3460. @ The script is informed of the constructor.
  3461. @<Set up the scripting engine@>=
  3462. constructor = engine->newFunction(constructQBrush);
  3463. engine->globalObject().setProperty("QBrush", constructor);
  3464. @ The constructor is trivial.
  3465. @<Functions for scripting@>=
  3466. QScriptValue constructQBrush(QScriptContext *context, QScriptEngine *engine)
  3467. {
  3468. QBrush theBrush = QBrush(QColor(argument<QString>(0, context)));
  3469. QScriptValue object = engine->toScriptValue(theBrush);
  3470. return object;
  3471. }
  3472. @* Scripting Item View Classes.
  3473. \noindent |QAbstractScrollArea| is a |QFrame| that serves as the base class for
  3474. classes such as |QGraphicsView| and |QAbstractItemView|. Objects from this class
  3475. are not created directly.
  3476. @<Function prototypes for scripting@>=
  3477. void setQAbstractScrollAreaProperties(QScriptValue value,
  3478. QScriptEngine *engine);
  3479. @ The implementation of this is simple.
  3480. @<Functions for scripting@>=
  3481. void setQAbstractScrollAreaProperties(QScriptValue value, QScriptEngine *engine)
  3482. {
  3483. setQFrameProperties(value, engine);
  3484. }
  3485. @ This class is used by the |QAbstractItemView| class. This is another class
  3486. that we do not need a script constructor for.
  3487. @<Function prototypes for scripting@>=
  3488. void setQAbstractItemViewProperties(QScriptValue value, QScriptEngine *engine);
  3489. @ This function has another simple implementation.
  3490. @<Functions for scripting@>=
  3491. void setQAbstractItemViewProperties(QScriptValue value, QScriptEngine *engine)
  3492. {
  3493. setQAbstractScrollAreaProperties(value, engine);
  3494. }
  3495. @ The |QGraphicsView| and |QTableView| classes form the base of \pn{} classes.
  3496. @<Function prototypes for scripting@>=
  3497. void setQGraphicsViewProperties(QScriptValue value, QScriptEngine *engine);
  3498. void setQTableViewProperties(QScriptValue value, QScriptEngine *engine);
  3499. @ Again, the implementations are boring.
  3500. @<Functions for scripting@>=
  3501. void setQGraphicsViewProperties(QScriptValue value, QScriptEngine *engine)
  3502. {
  3503. setQAbstractScrollAreaProperties(value, engine);
  3504. }
  3505. void setQTableViewProperties(QScriptValue value, QScriptEngine *engine)
  3506. {
  3507. setQAbstractItemViewProperties(value, engine);
  3508. }
  3509. @* Scripting Button Classes.
  3510. \noindent \pn{} provides an |AnnotationButton| class which is a special kind of
  3511. |QPushButton| which in turn comes from |QAbstractButton|. While
  3512. |AnnotationButton| can be used in exactly the same way as a |QPushButton|, if
  3513. an annotation is not needed, there is little reason not to use the base class.
  3514. @<Function prototypes for scripting@>=
  3515. void setQAbstractButtonProperties(QScriptValue value, QScriptEngine *engine);
  3516. void setQPushButtonProperties(QScriptValue value, QScriptEngine *engine);
  3517. QScriptValue constructQPushButton(QScriptContext *context,
  3518. QScriptEngine *engine);
  3519. @ The constructor for |QPushButton| should be passed to the scripting engine.
  3520. @<Set up the scripting engine@>=
  3521. constructor = engine->newFunction(constructQPushButton);
  3522. value = engine->newQMetaObject(&QPushButton::staticMetaObject, constructor);
  3523. engine->globalObject().setProperty("QPushButton", value);
  3524. @ The implementation should seem familiar.
  3525. @<Functions for scripting@>=
  3526. QScriptValue constructQPushButton(QScriptContext *, QScriptEngine *engine)
  3527. {
  3528. QScriptValue object = engine->newQObject(new QPushButton());
  3529. setQPushButtonProperties(object, engine);
  3530. return object;
  3531. }
  3532. void setQPushButtonProperties(QScriptValue value, QScriptEngine *engine)
  3533. {
  3534. setQAbstractButtonProperties(value, engine);
  3535. }
  3536. void setQAbstractButtonProperties(QScriptValue value, QScriptEngine *engine)
  3537. {
  3538. setQWidgetProperties(value, engine);
  3539. }
  3540. @* Scripting QSqlQuery.
  3541. \noindent With this class exposed to the host environment, it becomes possible
  3542. for script code to execute SQL queries and evaluate the result.
  3543. Rather than use |QSqlQuery| directly, however, we use a proxy \nfnote{Erich
  3544. Gamma, Richard Helm, Raph Johnson, and John
  3545. Vlissides,\par\indent\underbar{Design Patterns: elements of reusable
  3546. object-oriented software} (1995) pp. 207--217} class. This class obtains its own
  3547. database connection and handles properly closing and removing these connections
  3548. when the query object is destroyed.
  3549. @<Class declarations@>=
  3550. class SqlQueryConnection : public QSqlQuery@/
  3551. {
  3552. public:@/
  3553. SqlQueryConnection(const QString &query = QString());
  3554. ~SqlQueryConnection();
  3555. QSqlQuery* operator->() const;
  3556. private:@/
  3557. QString connection;
  3558. QSqlQuery *q;
  3559. };
  3560. @ The constructor can be somewhat simplified from the four forms of |QSqlQuery|.
  3561. We are not interested in creating an object from a |QSqlResult| or from another
  3562. |QSqlQuery|. The database connection is managed by the class itself so the
  3563. constructor only needs an optional string containing a query. This is used to
  3564. initialize a real |QSqlQuery| object.
  3565. @<SqlQueryConnection implementation@>=
  3566. SqlQueryConnection::SqlQueryConnection(const QString &query)
  3567. {
  3568. QSqlDatabase database = AppInstance->database();
  3569. database.open();
  3570. q = new QSqlQuery(query, database);
  3571. connection = database.connectionName();
  3572. }
  3573. @ The destructor handles removing the |QSqlQuery| and the database connection
  3574. associated with it. The extra brackets introduce a new scope for the
  3575. |QSqlDatabase| so that it is out of scope when the connection is removed.
  3576. @<SqlQueryConnection implementation@>=
  3577. SqlQueryConnection::~SqlQueryConnection()
  3578. {
  3579. delete q;
  3580. {
  3581. QSqlDatabase database = QSqlDatabase::database(connection);
  3582. database.close();
  3583. }
  3584. QSqlDatabase::removeDatabase(connection);
  3585. }
  3586. @ For all other functionality, we simply forward the request to our |QSqlQuery|
  3587. object.
  3588. @<SqlQueryConnection implementation@>=
  3589. QSqlQuery* SqlQueryConnection::operator->() const
  3590. {
  3591. return q;
  3592. }
  3593. @ In order to use this new class in the host environment, a number of functions
  3594. are needed.
  3595. @<Function prototypes for scripting@>=
  3596. void setQSqlQueryProperties(QScriptValue value, QScriptEngine *engine);
  3597. QScriptValue constructQSqlQuery(QScriptContext *context, QScriptEngine *engine);
  3598. QScriptValue QSqlQuery_bind(QScriptContext *context, QScriptEngine *engine);
  3599. QScriptValue QSqlQuery_bindDeviceData(QScriptContext *context,
  3600. QScriptEngine *engine);
  3601. QScriptValue QSqlQuery_bindFileData(QScriptContext *context,
  3602. QScriptEngine *engine);
  3603. QScriptValue QSqlQuery_exec(QScriptContext *context,
  3604. QScriptEngine *engine);
  3605. QScriptValue QSqlQuery_executedQuery(QScriptContext *context,
  3606. QScriptEngine *engine);
  3607. QScriptValue QSqlQuery_invalidate(QScriptContext *context, QScriptEngine *engine);
  3608. QScriptValue QSqlQuery_next(QScriptContext *context, QScriptEngine *engine);
  3609. QScriptValue QSqlQuery_prepare(QScriptContext *context, QScriptEngine *engine);
  3610. QScriptValue QSqlQuery_value(QScriptContext *context, QScriptEngine *engine);
  3611. @ For conceptual convenience we simply pretend that we are working with a real
  3612. |QSqlQuery| object.
  3613. @<Set up the scripting engine@>=
  3614. constructor = engine->newFunction(constructQSqlQuery);
  3615. engine->globalObject().setProperty("QSqlQuery", constructor);
  3616. @ With connection creation no longer needed in the constructor, all that is
  3617. needed is object creation and applying the appropriate properties to the script
  3618. value.
  3619. @<Functions for scripting@>=
  3620. QScriptValue constructQSqlQuery(QScriptContext *, QScriptEngine *engine)
  3621. {
  3622. SqlQueryConnection *obj = new SqlQueryConnection();
  3623. QScriptValue object =
  3624. engine->toScriptValue<void *>(obj);
  3625. setQSqlQueryProperties(object, engine);
  3626. return object;
  3627. }
  3628. @ As this class does not derive from |QObject|, we must wrap all of the methods
  3629. we might want to use.
  3630. @<Functions for scripting@>=
  3631. void setQSqlQueryProperties(QScriptValue value, QScriptEngine *engine)
  3632. {
  3633. value.setProperty("bind", engine->newFunction(QSqlQuery_bind));
  3634. value.setProperty("bindFileData",
  3635. engine->newFunction(QSqlQuery_bindFileData));
  3636. value.setProperty("bindDeviceData",
  3637. engine->newFunction(QSqlQuery_bindDeviceData));
  3638. value.setProperty("exec", engine->newFunction(QSqlQuery_exec));
  3639. value.setProperty("executedQuery", engine->newFunction(QSqlQuery_executedQuery));
  3640. value.setProperty("invalidate", engine->newFunction(QSqlQuery_invalidate));
  3641. value.setProperty("next", engine->newFunction(QSqlQuery_next));
  3642. value.setProperty("prepare", engine->newFunction(QSqlQuery_prepare));
  3643. value.setProperty("value", engine->newFunction(QSqlQuery_value));
  3644. }
  3645. @ Most of these properties are wrappers around existing |QSqlQuery| functions.
  3646. @<Functions for scripting@>=
  3647. QScriptValue QSqlQuery_exec(QScriptContext *context, QScriptEngine *engine)
  3648. {
  3649. QSqlQuery *q = getself<SqlQueryConnection *>(context)->operator->();
  3650. QScriptValue retval;
  3651. if(context->argumentCount() == 1)
  3652. {
  3653. retval = QScriptValue(engine,
  3654. q->exec(argument<QString>(0, context)));
  3655. }
  3656. else
  3657. {
  3658. retval = QScriptValue(engine, q->exec());
  3659. }
  3660. if(q->lastError().isValid())
  3661. {
  3662. qDebug() << q->lastQuery();
  3663. qDebug() << q->lastError().text();
  3664. }
  3665. return retval;
  3666. }
  3667. QScriptValue QSqlQuery_executedQuery(QScriptContext *context, QScriptEngine *)
  3668. {
  3669. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3670. return QScriptValue(query->lastQuery());
  3671. }
  3672. QScriptValue QSqlQuery_next(QScriptContext *context, QScriptEngine *engine)
  3673. {
  3674. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3675. return QScriptValue(engine, query->next());
  3676. }
  3677. QScriptValue QSqlQuery_value(QScriptContext *context, QScriptEngine *engine)
  3678. {
  3679. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3680. return QScriptValue(engine,
  3681. query->value(argument<int>(0, context)).toString());
  3682. }
  3683. @ For prepared queries, we support binding variables available to the script,
  3684. data available in a named file, or data from any open |QIODevice|.
  3685. @<Functions for scripting@>=
  3686. QScriptValue QSqlQuery_prepare(QScriptContext *context, QScriptEngine *engine)
  3687. {
  3688. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3689. return QScriptValue(engine, query->prepare(argument<QString>(0, context)));
  3690. }
  3691. QScriptValue QSqlQuery_bind(QScriptContext *context, QScriptEngine *)
  3692. {
  3693. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3694. query->bindValue(argument<QString>(0, context),
  3695. argument<QVariant>(1, context));
  3696. return QScriptValue();
  3697. }
  3698. QScriptValue QSqlQuery_bindFileData(QScriptContext *context,
  3699. QScriptEngine *)
  3700. {
  3701. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3702. QString placeholder = argument<QString>(0, context);
  3703. QString filename = argument<QString>(1, context);
  3704. QFile file(filename);
  3705. QByteArray data;
  3706. if(file.open(QIODevice::ReadOnly))
  3707. {
  3708. data = file.readAll();
  3709. file.close();
  3710. }
  3711. query->bindValue(placeholder, data);
  3712. return QScriptValue();
  3713. }
  3714. QScriptValue QSqlQuery_bindDeviceData(QScriptContext *context,
  3715. QScriptEngine *)
  3716. {
  3717. QSqlQuery *query = getself<SqlQueryConnection *>(context)->operator->();
  3718. QString placeholder = argument<QString>(0, context);
  3719. QIODevice *device = argument<QIODevice *>(1, context);
  3720. device->reset();
  3721. QByteArray data;
  3722. data = device->readAll();
  3723. query->bindValue(placeholder, data);
  3724. return QScriptValue();
  3725. }
  3726. @ To avoid leaking database connections, we add the |invalidate()| property
  3727. which destroys our object. The object on which this method is called must not be
  3728. used after calling this method. In script code this will typically be used as in
  3729. the following example:
  3730. {\tt query = query.invalidate();}
  3731. @<Functions for scripting@>=
  3732. QScriptValue QSqlQuery_invalidate(QScriptContext *context, QScriptEngine *)
  3733. {
  3734. SqlQueryConnection *query = getself<SqlQueryConnection *>(context);
  3735. delete query;
  3736. return QScriptValue::UndefinedValue;
  3737. }
  3738. @* Other scripting functions.
  3739. \noindent There are a few functions that are exposed to the scripting engine
  3740. that are not associated with any class. Two functions are used for extracting
  3741. information from file names. Another is used to construct array values from SQL
  3742. array values. There is also a function for setting the default font for the
  3743. application or some part of the application.
  3744. @<Function prototypes for scripting@>=
  3745. QScriptValue baseName(QScriptContext *context, QScriptEngine *engine);
  3746. QScriptValue dir(QScriptContext *context, QScriptEngine *engine);
  3747. QScriptValue sqlToArray(QScriptContext *context, QScriptEngine *engine);
  3748. QScriptValue setFont(QScriptContext *context, QScriptEngine *engine);
  3749. QScriptValue annotationFromRecord(QScriptContext *context,
  3750. QScriptEngine *engine);
  3751. QScriptValue setTabOrder(QScriptContext *context, QScriptEngine *engine);
  3752. QScriptValue saveFileFromDatabase(QScriptContext *context, QScriptEngine *engine);
  3753. QScriptValue scriptTr(QScriptContext *context, QScriptEngine *engine);
  3754. @ These functions are passed to the scripting engine.
  3755. @<Set up the scripting engine@>=
  3756. engine->globalObject().setProperty("baseName", engine->newFunction(baseName));
  3757. engine->globalObject().setProperty("dir", engine->newFunction(dir));
  3758. engine->globalObject().setProperty("sqlToArray",
  3759. engine->newFunction(sqlToArray));
  3760. engine->globalObject().setProperty("setFont", engine->newFunction(setFont));
  3761. engine->globalObject().setProperty("annotationFromRecord",
  3762. engine->newFunction(annotationFromRecord));
  3763. engine->globalObject().setProperty("setTabOrder",
  3764. engine->newFunction(setTabOrder));
  3765. engine->globalObject().setProperty("saveFileFromDatabase",
  3766. engine->newFunction(saveFileFromDatabase));
  3767. engine->globalObject().setProperty("TTR", engine->newFunction(scriptTr));
  3768. @ These functions are not part of an object. They expect a string specifying
  3769. the path to a file and return a string with either the name of the file without
  3770. the path and extension or the path of the directory containing the file.
  3771. @<Functions for scripting@>=
  3772. QScriptValue baseName(QScriptContext *context, QScriptEngine *engine)
  3773. {
  3774. QFileInfo info(argument<QString>(0, context));
  3775. QScriptValue retval(engine, info.baseName());
  3776. return retval;
  3777. }
  3778. QScriptValue dir(QScriptContext *context, QScriptEngine *engine)
  3779. {
  3780. QFileInfo info(argument<QString>(0, context));
  3781. QDir dir = info.dir();
  3782. QScriptValue retval(engine, dir.path());
  3783. return retval;
  3784. }
  3785. @ This function takes a file ID and a file name and copies file data stored in
  3786. the database out to the file system.
  3787. @<Functions for scripting@>=
  3788. QScriptValue saveFileFromDatabase(QScriptContext *context, QScriptEngine *)
  3789. {
  3790. SqlQueryConnection h;
  3791. QSqlQuery *query = h.operator->();
  3792. QString q = "SELECT file FROM files WHERE id = :file";
  3793. query->prepare(q);
  3794. query->bindValue(":file", argument<int>(0, context));
  3795. query->exec();
  3796. query->next();
  3797. QByteArray array = query->value(0).toByteArray();
  3798. QFile file(argument<QString>(1, context));
  3799. file.open(QIODevice::WriteOnly);
  3800. file.write(array);
  3801. file.close();
  3802. return QScriptValue();
  3803. }
  3804. @ This function takes a string representing a SQL array and returns an array
  3805. value.
  3806. @<Functions for scripting@>=
  3807. QScriptValue sqlToArray(QScriptContext *context, QScriptEngine *engine)
  3808. {
  3809. QString source = argument<QString>(0, context);
  3810. source.remove(0, 1);
  3811. source.chop(1);
  3812. QStringList elements = source.split(",");
  3813. QString element;
  3814. QScriptValue dest = engine->newArray(elements.size());
  3815. int i = 0;
  3816. foreach(element, elements)
  3817. {
  3818. if(element.startsWith("\"") && element.endsWith("\""))
  3819. {
  3820. element.chop(1);
  3821. element = element.remove(0, 1);
  3822. }
  3823. dest.setProperty(i, QScriptValue(engine, element));
  3824. i++;
  3825. }
  3826. return dest;
  3827. }
  3828. @ This function can be used to set the default font for the application or on
  3829. a per-class hierarchy basis.
  3830. @<Functions for scripting@>=
  3831. QScriptValue setFont(QScriptContext *context, QScriptEngine *)
  3832. {
  3833. QString font = argument<QString>(0, context);
  3834. QString classname;
  3835. if(context->argumentCount() > 1)
  3836. {
  3837. classname = argument<QString>(1, context);
  3838. QApplication::setFont(QFont(font), classname.toLatin1().constData());
  3839. }
  3840. else
  3841. {
  3842. QApplication::setFont(QFont(font));
  3843. }
  3844. return QScriptValue();
  3845. }
  3846. @ This function was briefly used prior to adding support for |QXmlQuery| in the
  3847. host environment. The function is now depreciated and should not be used.
  3848. @<Functions for scripting@>=
  3849. QScriptValue annotationFromRecord(QScriptContext *context, QScriptEngine *)
  3850. {
  3851. SqlQueryConnection h;
  3852. QSqlQuery *query = h.operator->();
  3853. QString q = "SELECT file FROM files WHERE id = :file";
  3854. query->prepare(q);
  3855. query->bindValue(":file", argument<int>(0, context));
  3856. query->exec();
  3857. query->next();
  3858. QByteArray array = query->value(0).toByteArray();
  3859. QBuffer buffer(&array);
  3860. buffer.open(QIODevice::ReadOnly);
  3861. QXmlQuery xquery;
  3862. xquery.bindVariable("profile", &buffer);
  3863. QString xq;
  3864. xq = "for $b in doc($profile) //tuple where exists($b/annotation) return $b";
  3865. xquery.setQuery(xq);
  3866. QString result;
  3867. xquery.evaluateTo(&result);
  3868. return QScriptValue(result);
  3869. }
  3870. @ This function can be used to change the tab order for controls in Typica.
  3871. Changes to the example configuration in \pn{} 1.4 made the default handling
  3872. of tab controls in the logging window unacceptable.
  3873. @<Functions for scripting@>=
  3874. QScriptValue setTabOrder(QScriptContext *context, QScriptEngine *)
  3875. {
  3876. QWidget::setTabOrder(argument<QWidget*>(0, context),
  3877. argument<QWidget*>(1, context));
  3878. return QScriptValue();
  3879. }
  3880. @ This function is used to allow text that must be placed in scripts to be
  3881. translated into other languages.
  3882. @<Functions for scripting@>=
  3883. QScriptValue scriptTr(QScriptContext *context, QScriptEngine *)
  3884. {
  3885. return QScriptValue(QCoreApplication::translate(
  3886. "configuration",
  3887. argument<QString>(1, context).toUtf8().data()));
  3888. }
  3889. @** Application Configuration.
  3890. \noindent While \pn{} is intended as a data logging application, the diversity
  3891. of equipment and supporting technology precludes the option of providing a
  3892. single interface for common tasks. It is important that the application can be
  3893. configured to work with different roasting equipment, databases, and the like.
  3894. To accomplish this, \pn{} utilizes an XML description of the desired application
  3895. configuration and provides an ECMA-262 host environment which allows application
  3896. dataflow to be configured.
  3897. The scripting environment provides access to elements of the XML file and also
  3898. allows access to most of the application classes. A selection of classes
  3899. provided by Qt is also available. See the section on The Scripting Engine for
  3900. more details.
  3901. \danger While the code is the ultimate documentation of what is possible with
  3902. this interface, additional documentation should be provided to document the
  3903. meaning of supported elements and the objects available through the scripting
  3904. engine.\endanger
  3905. The application configuration is loaded when the program is started.
  3906. Starting with version 1.4, we check for a command line option with the path to
  3907. the configuration file and load that instead of prompting for the information
  3908. if possible.
  3909. Starting with version 1.8, if there is not a -c argument, Typica will first
  3910. search a small number of locations relative to the executable.
  3911. @<Load the application configuration@>=
  3912. QStringList arguments = QCoreApplication::arguments();
  3913. int position = arguments.indexOf("-c");
  3914. QString filename = QString();
  3915. if(position != -1)
  3916. {
  3917. if(arguments.size() >= position + 1)
  3918. {
  3919. filename = arguments.at(position + 1);
  3920. }
  3921. } else {
  3922. QDir checkPath(QCoreApplication::applicationDirPath() + "/../config/");
  3923. if(checkPath.exists("config.xml")) {
  3924. filename = checkPath.filePath("config.xml");
  3925. } else {
  3926. checkPath = QDir(QCoreApplication::applicationDirPath() + "/config/");
  3927. if(checkPath.exists("config.xml")) {
  3928. filename = checkPath.filePath("config.xml");
  3929. }
  3930. }
  3931. }
  3932. if(filename.isEmpty())
  3933. {
  3934. filename = QFileDialog::getOpenFileName(NULL, "Open Configuration File",
  3935. settings.value("config", "").toString());
  3936. }
  3937. QDir directory;
  3938. if(!filename.isEmpty())
  3939. {
  3940. QFile file(filename);
  3941. QFileInfo info(filename);
  3942. directory = info.dir();
  3943. QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
  3944. QTranslator *configtr = new QTranslator;
  3945. if(configtr->load(QString("config.%1").arg(QLocale::system().name()),
  3946. QString("%1/Translations").arg(directory.canonicalPath())))
  3947. {
  3948. QCoreApplication::installTranslator(configtr);
  3949. }
  3950. settings.setValue("config", directory.path());
  3951. if(file.open(QIODevice::ReadOnly))
  3952. {
  3953. app.configuration()->setContent(&file, true);
  3954. }
  3955. } else {
  3956. return 1;
  3957. }
  3958. @<Substitute included fragments@>@;
  3959. @ The {\tt <application>} element can contain an arbitrary number of
  3960. {\tt <include>} elements. These elements should not appear in the DOM. Instead,
  3961. the element should be replaced by the content of the specified document.
  3962. @<Substitute included fragments@>=
  3963. QDomElement root = app.configuration()->documentElement();
  3964. QDomNodeList children = root.childNodes();
  3965. QString replacementDoc;
  3966. QDomDocument includedDoc;
  3967. QDomDocumentFragment fragment;
  3968. for(int i = 0; i < children.size(); i++)
  3969. {
  3970. QDomNode currentNode = children.at(i);
  3971. QDomElement currentElement;
  3972. if(currentNode.nodeName() == "include")
  3973. {
  3974. currentElement = currentNode.toElement();
  3975. if(currentElement.hasAttribute("src"))
  3976. {
  3977. replacementDoc = directory.path();
  3978. replacementDoc.append('/');
  3979. replacementDoc.append(currentElement.attribute("src"));
  3980. QFile doc(replacementDoc);
  3981. if(doc.open(QIODevice::ReadOnly))
  3982. {
  3983. includedDoc.setContent(&doc, true);
  3984. fragment = includedDoc.createDocumentFragment();
  3985. fragment.appendChild(includedDoc.documentElement());
  3986. root.replaceChild(fragment, currentNode);
  3987. doc.close();
  3988. }
  3989. }
  3990. }
  3991. }
  3992. @ Simply loading the configuration document does not display a user interface or
  3993. set up any objects that allow the program to do anything. To do this, a script
  3994. obtained from the configuration document is run. The root element of the
  3995. document should be {\tt <application>}. This element should have a number of
  3996. child elements including {\tt <window>} elements which describe the various
  3997. windows that can be opened in the application and {\tt <program>} elements
  3998. containing script code. These {\tt <program>} elements can occur in a number of
  3999. different contexts including within {\tt <window>} elements which would indicate
  4000. that such scripts should be evaluated when the window being described is
  4001. created. After the configuration document is loaded, all {\tt <program>}
  4002. elements that are direct children of the {\tt <application>} element are
  4003. concatenated and the script is run.
  4004. Before the script is run and user interface elements are drawn, we also check
  4005. for {\tt <style>} elements which can be used to set up a stylesheet for the
  4006. application.
  4007. @<Find and evaluate starting script@>=
  4008. QString styleText;
  4009. QString programText;
  4010. QDomElement currentElement;
  4011. for(int i = 0; i < children.size(); i++)
  4012. {
  4013. QDomNode currentNode = children.at(i);
  4014. if(currentNode.nodeName() == "style")
  4015. {
  4016. currentElement = currentNode.toElement();
  4017. styleText.append(currentElement.text());
  4018. }
  4019. else if(currentNode.nodeName() == "program")
  4020. {
  4021. currentElement = currentNode.toElement();
  4022. programText.append(currentElement.text());
  4023. }
  4024. }
  4025. app.setStyleSheet(styleText);
  4026. QScriptValue result = engine->evaluate(programText);
  4027. @<Report scripting errors@>
  4028. @ When a script is evaluated, there is a chance that there will be some error in
  4029. the execution of that script. If this occurs, we want to report that.
  4030. @<Report scripting errors@>=
  4031. if(engine->hasUncaughtException())
  4032. {
  4033. int line = engine->uncaughtExceptionLineNumber();
  4034. qDebug() << "Uncaught excpetion at line " << line << " : " <<
  4035. result.toString();
  4036. QString trace;
  4037. foreach(trace, engine->uncaughtExceptionBacktrace())
  4038. {
  4039. qDebug() << trace;
  4040. }
  4041. }
  4042. @* Creating a window.
  4043. \noindent When a configuration document is loaded, none of the {\tt <window>}
  4044. elements are interpreted or used to create a graphical user interface. Instead,
  4045. any {\tt <program>} elements that are immediate children of the
  4046. {\tt <application>} element are interpreted. In order to convert a
  4047. {\tt <window>} element into a window displayed on screen, the script in the
  4048. {\tt <program>} elements must call a function to display a specified window.
  4049. Report windows can be produced by scripts in a similar, but slightly different
  4050. manner.
  4051. \danger This design works, but it'@q'@>s not particularly good design. It was written
  4052. under severe time constraints and should be redesigned or at least cleaned up
  4053. and reorganized.\endanger
  4054. @<Function prototypes for scripting@>=
  4055. QScriptValue createWindow(QScriptContext *context, QScriptEngine *engine);
  4056. QScriptValue createReport(QScriptContext *context, QScriptEngine *engine);
  4057. void addLayoutToWidget(QDomElement element, QStack<QWidget*> *widgetStack,
  4058. QStack<QLayout*> *layoutStack);
  4059. void addLayoutToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4060. QStack<QLayout *> *layoutStack);
  4061. void addSplitterToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4062. QStack<QLayout *> *layoutStack);
  4063. void addSplitterToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  4064. QStack<QLayout *> *layoutStack);
  4065. void populateGridLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4066. QStack<QLayout *> *layoutStack);
  4067. void populateBoxLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4068. QStack<QLayout *> *layoutStack);
  4069. void populateSplitter(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4070. QStack<QLayout *> *layoutStack);
  4071. void populateWidget(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4072. QStack<QLayout *> *layoutStack);
  4073. void populateStackedLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4074. QStack<QLayout *> *layoutStack);
  4075. void populateFormLayout(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4076. QStack<QLayout *> *layoutStack);
  4077. void addTemperatureDisplayToSplitter(QDomElement element,@|
  4078. QStack<QWidget *> *widgetStack,
  4079. QStack<QLayout *> *layoutStack);
  4080. void addTemperatureDisplayToLayout(QDomElement element,@|
  4081. QStack<QWidget *> *widgetStack,
  4082. QStack<QLayout *> *layoutStack);
  4083. void addTimerDisplayToSplitter(QDomElement element,@|
  4084. QStack<QWidget *> *widgetStack,
  4085. QStack<QLayout *> *layoutStack);
  4086. void addTimerDisplayToLayout(QDomElement element,@|
  4087. QStack<QWidget *> *widgetStack,
  4088. QStack<QLayout *> *layoutStack);
  4089. void addDecorationToSplitter(QDomElement element,@|
  4090. QStack<QWidget *> *widgetStack,
  4091. QStack<QLayout *> *layoutStack);
  4092. void addDecorationToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4093. QStack<QLayout *> *layoutStack);
  4094. void addWidgetToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  4095. QStack<QLayout *> *layoutStack);
  4096. void addButtonToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4097. QStack<QLayout *> *layoutStack);
  4098. void addZoomLogToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  4099. QStack<QLayout *> *layoutStack);
  4100. void addGraphToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  4101. QStack<QLayout *> *layoutStack);
  4102. void addSqlDropToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4103. QStack<QLayout *> *layoutStack);
  4104. void addSaltToLayout(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4105. QStack<QLayout *> *layoutStack);
  4106. void addLineToLayout(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4107. QStack<QLayout *> *layoutStack);
  4108. void addTextToLayout(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4109. QStack<QLayout *> *layoutStack);
  4110. void addSqlQueryViewToLayout(QDomElement element,
  4111. QStack<QWidget *> *widgetStack,
  4112. QStack<QLayout *> *layoutStack);
  4113. void addCalendarToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4114. QStack<QLayout *> *layoutStack);
  4115. void addSpinBoxToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4116. QStack<QLayout *> *layoutStack);
  4117. void addTimeEditToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4118. QStack<QLayout *> *layoutStack);
  4119. @ The functions for creating windows must be made available to the scripting
  4120. engine.
  4121. @<Set up the scripting engine@>=
  4122. engine->globalObject().setProperty("createWindow",
  4123. engine->newFunction(createWindow));
  4124. engine->globalObject().setProperty("createReport",
  4125. engine->newFunction(createReport));
  4126. @ This function must examine the configuration document in search of the
  4127. appropriate window element, parse the contents of that element, and create a
  4128. multitude of objects, all of which must be passed to the scripting engine.
  4129. @<Functions for scripting@>=
  4130. QScriptValue createWindow(QScriptContext *context, QScriptEngine *engine)@/
  4131. {
  4132. QString targetID = argument<QString>(0, context);
  4133. QDomNode element;
  4134. QScriptValue object;
  4135. @<Find the window element@>@;
  4136. if(!element.isNull())
  4137. {
  4138. @<Display the window@>@;
  4139. }
  4140. return object;
  4141. }
  4142. @ Report files are not part of the configuration document and must be created
  4143. differently. While there is a special menu type that handles all of this
  4144. without involving the host environment, scripted generation and manipulation of
  4145. report windows requires another function. This function will only work after a
  4146. window with a reports menu has been created.
  4147. @<Functions for scripting@>=
  4148. QScriptValue createReport(QScriptContext *context, QScriptEngine *engine)
  4149. {
  4150. QString targetID = argument<QString>(0, context);
  4151. QFile file(QString("reports:%1").arg(targetID));
  4152. QScriptValue object;
  4153. if(file.open(QIODevice::ReadOnly))
  4154. {
  4155. QDomDocument document;
  4156. document.setContent(&file, true);
  4157. QDomElement element = document.documentElement();
  4158. if(!element.isNull())
  4159. {
  4160. @<Display the window@>@;
  4161. }
  4162. file.close();
  4163. }
  4164. return object;
  4165. }
  4166. @ First we must locate the {\tt <window>} element. The most sensible way to do
  4167. this would require that each {\tt <window>} element has an ID attribute and
  4168. search the DOM tree for that ID. Unfortunately, as of this writing,
  4169. |QDomDocument::elementByID()| always returns a null element, so that won'@q'@>t work.
  4170. Instead, we search the tree for all {\tt <window>} elements and then examine
  4171. the resulting list to find the element with the appropriate ID.
  4172. @<Find the window element@>=
  4173. QDomNodeList windows =
  4174. AppInstance->configuration()->documentElement().elementsByTagName("window");
  4175. QDomNode nullNode;
  4176. int i = 0;
  4177. element = nullNode;
  4178. while(i < windows.count())
  4179. {
  4180. element = windows.at(i);
  4181. QDomNamedNodeMap attributes = element.attributes();
  4182. if(attributes.contains("id"))
  4183. {
  4184. if(attributes.namedItem("id").toAttr().value() == targetID)
  4185. {
  4186. break;
  4187. }
  4188. }
  4189. element = nullNode;
  4190. i++;
  4191. }
  4192. @ In order to display a window, we start by creating a new |ScriptQMainWindow|
  4193. and set the central widget of that window to a new |QWidget|. After this, we see
  4194. if the window element has any children and proceed to populate the window.
  4195. When creating child elements, care must be taken that all objects are descended
  4196. from the window. If an object is descended from the window and has an object
  4197. name, it will be possible for script code to recover the created object.
  4198. As of version 1.4, the window itself is given the value of its {\tt id}
  4199. attribute as an object name to facilitate automatic window geometry management.
  4200. @<Display the window@>=
  4201. ScriptQMainWindow *window = new ScriptQMainWindow;
  4202. window->setObjectName(targetID);
  4203. object = engine->newQObject(window);
  4204. setQMainWindowProperties(object, engine);
  4205. QWidget *central = new(QWidget);
  4206. central->setParent(window);
  4207. central->setObjectName("centralWidget");
  4208. window->setCentralWidget(central);
  4209. if(element.hasChildNodes())
  4210. {
  4211. @<Process window children@>@;
  4212. }
  4213. @<Insert help menu@>@;
  4214. window->show();
  4215. window->setupFinished();
  4216. @ Three element types make sense as top level children of a {\tt <window>}
  4217. element. An element representing a layout element can be used to apply that
  4218. layout to the central widget. An element representing a menu can be used to add
  4219. a menu to the window. A {\tt <program>} element can be used to specify a script
  4220. to be run after the window has been assembled.
  4221. \danger As the window comes with a blank central widget, elements representing
  4222. a widget to be used as the central widget of the window cannot be used directly
  4223. here. If only one widget is needed in the window, there is a need to create a
  4224. layout element and place that widget in the layout. Also note that there is not
  4225. enough error checking in the following code. Provide invalid input at your
  4226. peril.\endanger
  4227. Program fragments pulled from the window description are executed with the
  4228. newly created window available as {\tt this}. When such a fragment is run, the
  4229. entire description of the window will have already been evaluated and any
  4230. necessary objects created. Obtaining a child object of the window can be done
  4231. by calling |findChildObject()|.
  4232. @<Process window children@>=
  4233. QStack<QWidget*> widgetStack;
  4234. QStack<QLayout*> layoutStack;
  4235. QString windowScript;
  4236. widgetStack.push(central);
  4237. QDomNodeList windowChildren = element.childNodes();
  4238. int i = 0;
  4239. while(i < windowChildren.count())
  4240. {
  4241. QDomNode current;
  4242. QDomElement element;
  4243. current = windowChildren.at(i);
  4244. if(current.isElement())
  4245. {
  4246. element = current.toElement();
  4247. if(element.tagName() == "program")
  4248. {
  4249. windowScript.append(element.text());
  4250. }
  4251. else if(element.tagName() == "layout")
  4252. {
  4253. element.setAttribute("trcontext", "configuration");
  4254. addLayoutToWidget(element, &widgetStack, &layoutStack);
  4255. }
  4256. else if(element.tagName() == "menu")
  4257. {
  4258. @<Process menus@>@;
  4259. }
  4260. }
  4261. i++;
  4262. }
  4263. QScriptValue oldThis = context->thisObject();
  4264. context->setThisObject(object);
  4265. QScriptValue result = engine->evaluate(windowScript);
  4266. @<Report scripting errors@>@;
  4267. context->setThisObject(oldThis);
  4268. @ Elements representing menus may provide a number of child elements
  4269. representing the items in that menu. The XML portion of the configuration will
  4270. not provide any information on what these menu items do. The contents of the
  4271. {\tt <program>} element for the window will need to request the |QAction|
  4272. objects and connect a signal from that object to the desired functionality.
  4273. One special consideration is the Reports menu. This menu will populate itself
  4274. according to its own logic and will have a {\tt type} property of
  4275. {\tt "reports"} and a {\tt src} property indicating the directory where reports
  4276. can be found.
  4277. @<Process menus@>=
  4278. QMenuBar *bar = window->menuBar();
  4279. bar->setParent(window);
  4280. bar->setObjectName("menuBar");
  4281. if(element.hasAttribute("name"))
  4282. {
  4283. QMenu *menu = bar->addMenu(QCoreApplication::translate("configuration",
  4284. element.attribute("name").toUtf8().data()));
  4285. menu->setParent(bar);
  4286. if(element.hasAttribute("type"))
  4287. {
  4288. if(element.attribute("type") == "reports")
  4289. {
  4290. if(element.hasAttribute("src"))
  4291. {
  4292. @<Populate reports menu@>@;
  4293. }
  4294. }
  4295. }
  4296. if(element.hasChildNodes())
  4297. {
  4298. @<Process menu items@>@;
  4299. }
  4300. }
  4301. @ To add items to a menu, we check for {\tt <item>} elements under the
  4302. {\tt <menu>} element and create a |QAction| for each item.
  4303. @<Process menu items@>=
  4304. QDomNodeList menuItems = element.childNodes();
  4305. int j = 0;
  4306. while(j < menuItems.count())
  4307. {
  4308. QDomNode item = menuItems.at(j);
  4309. if(item.isElement())
  4310. {
  4311. QDomElement itemElement = item.toElement();
  4312. if(itemElement.tagName() == "item")
  4313. {
  4314. QAction *itemAction = new QAction(QCoreApplication::translate("configuration",
  4315. itemElement.text().toUtf8().data()), menu);
  4316. if(itemElement.hasAttribute("id"))
  4317. {
  4318. itemAction->setObjectName(itemElement.attribute("id"));
  4319. }
  4320. if(itemElement.hasAttribute("shortcut"))
  4321. {
  4322. itemAction->setShortcut(itemElement.attribute("shortcut"));
  4323. }
  4324. menu->addAction(itemAction);
  4325. }
  4326. else if(itemElement.tagName() == "separator")
  4327. {
  4328. menu->addSeparator();
  4329. }
  4330. else if(itemElement.tagName() == "plugins")
  4331. {
  4332. @<Process plugin item@>@;
  4333. }
  4334. }
  4335. j++;
  4336. }
  4337. @i helpmenu.w
  4338. @i licensewindow.w
  4339. @ A layout can contain a number of different elements including a variety of
  4340. widget types and other layouts. This function is responsible for applying any
  4341. layout class to the widget currently being populated and processing children of
  4342. the {\tt <layout>} element to populate that layout. External stacks are used to
  4343. keep track of which widgets and layouts are currently being populated. The
  4344. {\tt type} attribute is used to determine what sort of layout should be created.
  4345. Currently, {\tt horizontal}, {\tt vertical}, {\tt grid}, and {\tt stack} types
  4346. are supported. The first two resolve to |QBoxLayout| layouts, {\tt grid}
  4347. resolves to a |QGridLayout|, and {\tt stack} resolves to a |QStackedLayout|.
  4348. @<Functions for scripting@>=
  4349. void addLayoutToWidget(QDomElement element, QStack<QWidget*> *widgetStack,
  4350. QStack<QLayout*> *layoutStack)
  4351. {
  4352. if(element.hasAttribute("type"))
  4353. {
  4354. @<Create and populate layout@>@;
  4355. QWidget *widget = widgetStack->top();
  4356. if(layout)
  4357. {
  4358. widget->setLayout(layout);
  4359. }
  4360. layoutStack->pop();
  4361. }
  4362. }
  4363. @ As there are multiple places where a {\tt <layout>} element is parsed with
  4364. slightly different semantics, the code for creating and populating the layout is
  4365. broken out so that code written to support additional layout types only needs to
  4366. be written once.
  4367. @<Create and populate layout@>=
  4368. QLayout *layout;
  4369. QString layoutType = element.attribute("type");
  4370. if(layoutType == "horizontal")
  4371. {
  4372. layout = new QHBoxLayout;
  4373. layoutStack->push(layout);
  4374. populateBoxLayout(element, widgetStack, layoutStack);
  4375. }
  4376. else if(layoutType == "vertical")
  4377. {
  4378. layout = new QVBoxLayout;
  4379. layoutStack->push(layout);
  4380. populateBoxLayout(element, widgetStack, layoutStack);
  4381. }
  4382. else if(layoutType == "grid")
  4383. {
  4384. layout = new QGridLayout;
  4385. layoutStack->push(layout);
  4386. populateGridLayout(element, widgetStack, layoutStack);
  4387. }
  4388. else if(layoutType == "stack")
  4389. {
  4390. layout = new QStackedLayout;
  4391. layoutStack->push(layout);
  4392. populateStackedLayout(element, widgetStack, layoutStack);
  4393. }
  4394. else if(layoutType == "form")
  4395. {
  4396. layout = new QFormLayout;
  4397. layoutStack->push(layout);
  4398. populateFormLayout(element, widgetStack, layoutStack);
  4399. }
  4400. if(element.hasAttribute("id"))
  4401. {
  4402. layout->setObjectName(element.attribute("id"));
  4403. }
  4404. if(element.hasAttribute("spacing"))
  4405. {
  4406. layout->setSpacing(element.attribute("spacing").toInt());
  4407. }
  4408. if(element.hasAttribute("margin"))
  4409. {
  4410. int m = element.attribute("margin").toInt();
  4411. layout->setContentsMargins(m, m, m, m);
  4412. }
  4413. @ Any direct child of a form layout must be a {\tt <row>} element to specify
  4414. the label for the given row. The field for the given row will always be a
  4415. |QVBoxLayout| containing whatever is specified by children of the {\tt <row>}.
  4416. @<Functions for scripting@>=
  4417. void populateFormLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4418. QStack<QLayout *> *layoutStack)
  4419. {
  4420. QDomNodeList children = element.childNodes();
  4421. QFormLayout *layout = qobject_cast<QFormLayout *>(layoutStack->top());
  4422. for(int i = 0; i < children.count(); i++)
  4423. {
  4424. QDomNode current;
  4425. QDomElement currentElement;
  4426. current = children.at(i);
  4427. if(current.isElement())
  4428. {
  4429. currentElement = current.toElement();
  4430. if(currentElement.tagName() == "row")
  4431. {
  4432. QString label = QString();
  4433. if(currentElement.hasAttribute("label"))
  4434. {
  4435. label = currentElement.attribute("label");
  4436. }
  4437. QVBoxLayout *childLayout = new QVBoxLayout;
  4438. layoutStack->push(childLayout);
  4439. populateBoxLayout(currentElement, widgetStack, layoutStack);
  4440. if(label.isEmpty())
  4441. {
  4442. layout->addRow(childLayout);
  4443. }
  4444. else
  4445. {
  4446. layout->addRow(label, childLayout);
  4447. }
  4448. }
  4449. }
  4450. }
  4451. }
  4452. @ Stacked layouts are a bit different from the other types. A stacked layout has
  4453. an arbitrary number of {\tt <page>} children which are just a |QWidget| which
  4454. can have the same child elements as {\tt <widget>} elements elsewhere. Only the
  4455. first page will be visible initially, however it is possible to use script code
  4456. to set the currently visible page provided that an ID is set for the layout.
  4457. @<Functions for scripting@>=
  4458. void populateStackedLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4459. QStack<QLayout *> *layoutStack)
  4460. {
  4461. QDomNodeList children = element.childNodes();
  4462. QStackedLayout *layout = qobject_cast<QStackedLayout *>(layoutStack->top());
  4463. for(int i = 0; i < children.count(); i++)
  4464. {
  4465. QDomNode current;
  4466. QDomElement currentElement;
  4467. current = children.at(i);
  4468. if(current.isElement())
  4469. {
  4470. currentElement = current.toElement();
  4471. if(currentElement.tagName() == "page")
  4472. {
  4473. QWidget *widget = new QWidget;
  4474. layout->addWidget(widget);
  4475. widgetStack->push(widget);
  4476. currentElement.setAttribute("trcontext", "configuration");
  4477. populateWidget(currentElement, widgetStack, layoutStack);
  4478. widgetStack->pop();
  4479. }
  4480. }
  4481. }
  4482. }
  4483. @ A common use of stacked layouts is in the creation of tabbed interfaces, but
  4484. there are also many uses in \pn{} where the tabs are not required. Therefore,
  4485. tab bar creation requires a separate XML element.
  4486. @<Additional box layout elements@>=
  4487. else if(currentElement.tagName() == "tabbar")
  4488. {
  4489. addTabBarToLayout(currentElement, widgetStack, layoutStack);
  4490. }
  4491. @ The function used to create this follows the usual pattern.
  4492. @<Functions for scripting@>=
  4493. void addTabBarToLayout(QDomElement element, QStack<QWidget*> *, QStack<QLayout*> *layoutStack)
  4494. {
  4495. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4496. QTabBar *widget = new QTabBar;
  4497. layout->addWidget(widget);
  4498. if(!element.attribute("id").isEmpty())
  4499. {
  4500. widget->setObjectName(element.attribute("id"));
  4501. }
  4502. }
  4503. @ Rather than define the tab set in XML, this is left to the host environment.
  4504. This means that some additional scripting support is required.
  4505. @<Set up the scripting engine@>=
  4506. constructor = engine->newFunction(constructQTabBar);
  4507. value = engine->newQMetaObject(&QTabBar::staticMetaObject, constructor);
  4508. engine->globalObject().setProperty("QTabBar", value);
  4509. @ The constructor is trivial.
  4510. @<Functions for scripting@>=
  4511. QScriptValue constructQTabBar(QScriptContext *, QScriptEngine *engine)
  4512. {
  4513. QScriptValue object = engine->newQObject(new QTabBar);
  4514. setQTabBarProperties(object, engine);
  4515. return object;
  4516. }
  4517. @ There are many functions that I might want to some day add support for, but
  4518. the immediate need is just creating the tabs in the first place.
  4519. @<Functions for scripting@>=
  4520. void setQTabBarProperties(QScriptValue value, QScriptEngine *engine)
  4521. {
  4522. setQWidgetProperties(value, engine);
  4523. value.setProperty("addTab", engine->newFunction(QTabBar_addTab));
  4524. }
  4525. QScriptValue QTabBar_addTab(QScriptContext *context, QScriptEngine *)
  4526. {
  4527. QTabBar *self = getself<QTabBar *>(context);
  4528. if(context->argumentCount() > 0)
  4529. {
  4530. self->addTab(argument<QString>(0, context));
  4531. }
  4532. else
  4533. {
  4534. context->throwError("Incorrect number of arguments passed to "@|
  4535. "QTabBar::addTab().");
  4536. }
  4537. return QScriptValue();
  4538. }
  4539. @ Function prototypes are needed.
  4540. @<Function prototypes for scripting@>=
  4541. QScriptValue constructQTabBar(QScriptContext *context, QScriptEngine *engine);
  4542. void setQTabBarProperties(QScriptValue value, QScriptEngine *engine);
  4543. QScriptValue QTabBar_addTab(QScriptContext *context, QScriptEngine *engine);
  4544. @ Using a grid layout is a bit different from using a box layout. Child elements
  4545. with various attributes are required to take full advantage of this layout type.
  4546. All direct children of a grid layout element should be {\tt <row>} elements
  4547. which may have optional {\tt height} and {\tt stretch} attributes which apply to
  4548. that row.
  4549. @<Functions for scripting@>=
  4550. void populateGridLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4551. QStack<QLayout *> *layoutStack)
  4552. {
  4553. QDomNodeList children = element.childNodes();
  4554. int row = -1;
  4555. QGridLayout *layout = qobject_cast<QGridLayout *>(layoutStack->top());
  4556. for(int i = 0; i < children.count(); i++)
  4557. {
  4558. QDomNode current;
  4559. QDomElement currentElement;
  4560. current = children.at(i);
  4561. if(current.isElement())
  4562. {
  4563. currentElement = current.toElement();
  4564. if(currentElement.tagName() == "row")
  4565. {
  4566. row++;
  4567. if(currentElement.hasAttribute("height"))
  4568. {
  4569. layout->setRowMinimumHeight(row,
  4570. currentElement.attribute("height").toInt());
  4571. }
  4572. if(currentElement.hasAttribute("stretch"))
  4573. {
  4574. layout->setRowStretch(row,
  4575. currentElement.attribute("stretch").toInt());
  4576. }
  4577. @<Populate grid layout row@>@;
  4578. }
  4579. }
  4580. }
  4581. }
  4582. @ Each {\tt <row>} may have arbitrarily many {\tt <column>} children. A row with
  4583. nothing in it or that is entirely populated by spanning cells from previous rows
  4584. might have no children.
  4585. The {\tt <column>} element supports several optional attributes. The
  4586. {\tt column} attribute can be used to specify which column the element refers
  4587. to. Sibling {\tt <column>} elements will refer to columns farther right unless
  4588. a lower column number is specified. This does mean that it is possible to
  4589. specify the same column more than once, however actually doing so is not
  4590. recommended. The {\tt width} attribute specifies the minimum width of the
  4591. column. If multiple cells in a column specify this attribute, the last one takes
  4592. priority. Similarly, the {\tt stretch} attribute specifies the column stretch.
  4593. The {\tt rowspan} and {\tt colspan} attributes can be used for cells that span
  4594. more than one row or column. A value of |-1| can be used to have the cell span
  4595. to the last row or column in the layout.
  4596. Once the attributes of the cell are known, a |QHBoxLayout| is added to the
  4597. layout at the appropriate location in the grid and it is this layout which is
  4598. further populated by child elements. Anything that can be placed under a
  4599. {\tt <layout>} element with {\tt "horizontal"} or {\tt "vertical"} {\tt type}
  4600. attribute can be a child of a {\tt <column>} element in this context.
  4601. @<Populate grid layout row@>=
  4602. int column = -1;
  4603. QDomNodeList rowChildren = currentElement.childNodes();
  4604. for(int j = 0; j < rowChildren.count(); j++)
  4605. {
  4606. QDomNode columnNode;
  4607. QDomElement columnElement;
  4608. columnNode = rowChildren.at(j);
  4609. if(columnNode.isElement())
  4610. {
  4611. columnElement = columnNode.toElement();
  4612. if(columnElement.tagName() == "column")
  4613. {
  4614. column++;
  4615. if(columnElement.hasAttribute("column"))
  4616. {
  4617. column = columnElement.attribute("column").toInt();
  4618. }
  4619. if(columnElement.hasAttribute("width"))
  4620. {
  4621. layout->setColumnMinimumWidth(column,
  4622. columnElement.attribute("width").toInt());
  4623. }
  4624. if(columnElement.hasAttribute("stretch"))
  4625. {
  4626. layout->setColumnStretch(column,
  4627. columnElement.attribute("stretch").toInt());
  4628. }
  4629. int hspan = 1;
  4630. int vspan = 1;
  4631. if(columnElement.hasAttribute("rowspan"))
  4632. {
  4633. vspan = columnElement.attribute("rowspan").toInt();
  4634. }
  4635. if(columnElement.hasAttribute("colspan"))
  4636. {
  4637. hspan = columnElement.attribute("colspan").toInt();
  4638. }
  4639. QHBoxLayout *cell = new QHBoxLayout;
  4640. layout->addLayout(cell, row, column, vspan, hspan);
  4641. layoutStack->push(cell);
  4642. columnElement.setAttribute("trcontext", "configuration");
  4643. populateBoxLayout(columnElement, widgetStack, layoutStack);
  4644. layoutStack->pop();
  4645. }
  4646. }
  4647. }
  4648. @ Box layouts are populated by checking for child elements representing
  4649. supported widget types and layouts and adding these to the current layout.
  4650. @<Functions for scripting@>=
  4651. void populateBoxLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4652. QStack<QLayout *> *layoutStack)
  4653. {
  4654. QDomNodeList children = element.childNodes();
  4655. for(int i = 0; i < children.count(); i++)
  4656. {
  4657. QDomNode current;
  4658. QDomElement currentElement;
  4659. current = children.at(i);
  4660. if(current.isElement())
  4661. {
  4662. currentElement = current.toElement();
  4663. currentElement.setAttribute("trcontext", "configuration");
  4664. if(currentElement.tagName() == "button")
  4665. {
  4666. addButtonToLayout(currentElement, widgetStack, layoutStack);
  4667. }
  4668. else if(currentElement.tagName() == "calendar")
  4669. {
  4670. addCalendarToLayout(currentElement, widgetStack, layoutStack);
  4671. }
  4672. else if(currentElement.tagName() == "timeedit")
  4673. {
  4674. addTimeEditToLayout(currentElement, widgetStack, layoutStack);
  4675. }
  4676. else if(currentElement.tagName() == "decoration")
  4677. {
  4678. addDecorationToLayout(currentElement, widgetStack,
  4679. layoutStack);
  4680. }
  4681. else if(currentElement.tagName() == "layout")
  4682. {
  4683. addLayoutToLayout(currentElement, widgetStack, layoutStack);
  4684. }
  4685. else if(currentElement.tagName() == "splitter")
  4686. {
  4687. addSplitterToLayout(currentElement, widgetStack, layoutStack);
  4688. }
  4689. else if(currentElement.tagName() == "label")
  4690. {
  4691. QBoxLayout *layout =
  4692. qobject_cast<QBoxLayout *>(layoutStack->top());
  4693. QLabel *label = new QLabel(QCoreApplication::translate(
  4694. "configuration",
  4695. currentElement.text().toUtf8().data()));
  4696. if(currentElement.hasAttribute("id"))
  4697. {
  4698. label->setObjectName(currentElement.attribute("id"));
  4699. }
  4700. layout->addWidget(label);
  4701. }
  4702. else if(currentElement.tagName() == "lcdtemperature")
  4703. {
  4704. addTemperatureDisplayToLayout(currentElement, widgetStack,
  4705. layoutStack);
  4706. }
  4707. else if(currentElement.tagName() == "lcdtimer")
  4708. {
  4709. addTimerDisplayToLayout(currentElement, widgetStack,
  4710. layoutStack);
  4711. }
  4712. else if(currentElement.tagName() == "line")
  4713. {
  4714. addLineToLayout(currentElement, widgetStack, layoutStack);
  4715. }
  4716. else if(currentElement.tagName() == "report")
  4717. {
  4718. addReportToLayout(currentElement, widgetStack, layoutStack);
  4719. }
  4720. else if(currentElement.tagName() == "sqldrop")
  4721. {
  4722. addSqlDropToLayout(currentElement, widgetStack, layoutStack);
  4723. }
  4724. else if(currentElement.tagName() == "sqltablearray")
  4725. {
  4726. addSaltToLayout(currentElement, widgetStack, layoutStack);
  4727. }
  4728. else if(currentElement.tagName() == "sqlview")
  4729. {
  4730. addSqlQueryViewToLayout(currentElement, widgetStack,
  4731. layoutStack);
  4732. }
  4733. else if(currentElement.tagName() == "textarea")
  4734. {
  4735. addTextToLayout(currentElement, widgetStack, layoutStack);
  4736. }
  4737. else if(currentElement.tagName() == "spinbox")
  4738. {
  4739. addSpinBoxToLayout(currentElement, widgetStack, layoutStack);
  4740. }
  4741. else if(currentElement.tagName() == "formarray")
  4742. {
  4743. addFormArrayToLayout(currentElement, widgetStack, layoutStack);
  4744. }
  4745. else if(currentElement.tagName() =="hscale")
  4746. {
  4747. addScaleControlToLayout(currentElement, widgetStack,
  4748. layoutStack);
  4749. }
  4750. else if(currentElement.tagName() == "vscale")
  4751. {
  4752. addIntensityControlToLayout(currentElement, widgetStack,
  4753. layoutStack);
  4754. }
  4755. else if(currentElement.tagName() == "webview")
  4756. {
  4757. addWebViewToLayout(currentElement, widgetStack, layoutStack);
  4758. }
  4759. else if(currentElement.tagName() == "stretch")
  4760. {
  4761. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4762. layout->addStretch();
  4763. }
  4764. @<Additional box layout elements@>@;
  4765. }
  4766. }
  4767. }
  4768. @ Box layouts support adding additional layouts to the layout. The form of the
  4769. function is very similar to |addLayoutToWidget()|.
  4770. @<Functions for scripting@>=
  4771. void addLayoutToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4772. QStack<QLayout *> *layoutStack)
  4773. {
  4774. QLayout *targetLayout = layoutStack->pop();
  4775. QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(targetLayout);
  4776. if(element.hasAttribute("type"))
  4777. {
  4778. @<Create and populate layout@>@;
  4779. boxLayout->addLayout(layout);
  4780. layoutStack->pop();
  4781. }
  4782. layoutStack->push(targetLayout);
  4783. }
  4784. @ A splitter is similar to a layout in that it manages the size and position of
  4785. one or more widgets, however it is not a layout and therefore needs to be
  4786. handled separately.
  4787. @<Functions for scripting@>=
  4788. void addSplitterToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  4789. QStack<QLayout *> *layoutStack)
  4790. {
  4791. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4792. QSplitter *splitter = new(QSplitter);
  4793. layout->addWidget(splitter);
  4794. @<Set up splitter@>@;
  4795. }
  4796. @ As there are multiple places where a splitter element must be examined, the
  4797. common code is set aside.
  4798. @<Set up splitter@>=
  4799. QString orientation = element.attribute("type");
  4800. if(orientation == "horizontal")
  4801. {
  4802. splitter->setOrientation(Qt::Horizontal);
  4803. }
  4804. else if(orientation == "vertical")
  4805. {
  4806. splitter->setOrientation(Qt::Vertical);
  4807. }
  4808. QString id = element.attribute("id");
  4809. if(!id.isEmpty())
  4810. {
  4811. splitter->setObjectName(id);
  4812. }
  4813. if(element.hasChildNodes())
  4814. {
  4815. widgetStack->push(splitter);
  4816. populateSplitter(element, widgetStack, layoutStack);
  4817. widgetStack->pop();
  4818. }
  4819. @ When populating a splitter, it is important to note that only widgets can be
  4820. added. If a layout is needed, this can be handled by adding a |QWidget| and
  4821. applying the layout to that widget.
  4822. @<Functions for scripting@>=
  4823. void populateSplitter(QDomElement element, QStack<QWidget *> *widgetStack,@|
  4824. QStack<QLayout *> *layoutStack)
  4825. {
  4826. QDomNodeList children = element.childNodes();
  4827. for(int i = 0; i < children.count(); i++)
  4828. {
  4829. QDomNode current;
  4830. QDomElement currentElement;
  4831. current = children.at(i);
  4832. if(current.isElement())
  4833. {
  4834. currentElement = current.toElement();
  4835. currentElement.setAttribute("trcontext", "configuration");
  4836. if(currentElement.tagName() == "decoration")
  4837. {
  4838. addDecorationToSplitter(currentElement, widgetStack,
  4839. layoutStack);
  4840. }
  4841. else if(currentElement.tagName() == "graph")
  4842. {
  4843. addGraphToSplitter(currentElement, widgetStack, layoutStack);
  4844. }
  4845. else if(currentElement.tagName() == "splitter")
  4846. {
  4847. addSplitterToSplitter(currentElement, widgetStack, layoutStack);
  4848. }
  4849. else if(currentElement.tagName() == "lcdtemperature")
  4850. {
  4851. addTemperatureDisplayToSplitter(currentElement, widgetStack,
  4852. layoutStack);
  4853. }
  4854. else if(currentElement.tagName() == "lcdtimer")
  4855. {
  4856. addTimerDisplayToSplitter(currentElement, widgetStack,
  4857. layoutStack);
  4858. }
  4859. else if(currentElement.tagName() == "measurementtable")
  4860. {
  4861. addZoomLogToSplitter(currentElement, widgetStack, layoutStack);
  4862. }
  4863. else if(currentElement.tagName() == "widget")
  4864. {
  4865. addWidgetToSplitter(currentElement, widgetStack, layoutStack);
  4866. }
  4867. }
  4868. }
  4869. }
  4870. @ Adding a splitter to a splitter is similar to adding it to a layout.
  4871. @<Functions for scripting@>=
  4872. void addSplitterToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  4873. QStack<QLayout *> *layoutStack)
  4874. {
  4875. QSplitter *parent = qobject_cast<QSplitter *>(widgetStack->top());
  4876. QSplitter *splitter = new(QSplitter);
  4877. splitter->setParent(parent);
  4878. parent->addWidget(splitter);
  4879. @<Set up splitter@>@;
  4880. }
  4881. @ Temperature displays are useful to have in an application such as this. At
  4882. present, this code only supports the {\tt id} attribute. It may be useful in the
  4883. future to allow other attributes for changing default attributes of the
  4884. indicator rather than needing to pull the object from script code and set
  4885. changes there.
  4886. @<Functions for scripting@>=
  4887. void addTemperatureDisplayToSplitter(QDomElement element,@|
  4888. QStack<QWidget *> *widgetStack,
  4889. QStack<QLayout *> *)
  4890. {
  4891. TemperatureDisplay *display = new(TemperatureDisplay);
  4892. if(element.hasAttribute("id"))
  4893. {
  4894. display->setObjectName(element.attribute("id"));
  4895. }
  4896. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  4897. splitter->addWidget(display);
  4898. }
  4899. void addTemperatureDisplayToLayout(QDomElement element,@|
  4900. QStack<QWidget *> *,
  4901. QStack<QLayout *> *layoutStack)
  4902. {
  4903. TemperatureDisplay *display = new(TemperatureDisplay);
  4904. if(element.hasAttribute("id"))
  4905. {
  4906. display->setObjectName(element.attribute("id"));
  4907. }
  4908. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4909. layout->addWidget(display);
  4910. }
  4911. @ Timer displays are similarly useful to have. The default format for a timer
  4912. display is {\tt hh:mm:ss}, but this can be changed through the {\tt format}
  4913. attribute of an {\tt <lcdtimer>} element.
  4914. @<Functions for scripting@>=
  4915. void addTimerDisplayToSplitter(QDomElement element,@|
  4916. QStack<QWidget *> *widgetStack,
  4917. QStack<QLayout *> *)
  4918. {
  4919. TimerDisplay *display = new(TimerDisplay);
  4920. if(element.hasAttribute("id"))
  4921. {
  4922. display->setObjectName(element.attribute("id"));
  4923. }
  4924. if(element.hasAttribute("format"))
  4925. {
  4926. display->setDisplayFormat(element.attribute("format"));
  4927. }
  4928. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  4929. splitter->addWidget(display);
  4930. }
  4931. void addTimerDisplayToLayout(QDomElement element,@|
  4932. QStack<QWidget *> *,
  4933. QStack<QLayout *> *layoutStack)
  4934. {
  4935. TimerDisplay *display = new(TimerDisplay);
  4936. if(element.hasAttribute("id"))
  4937. {
  4938. display->setObjectName(element.attribute("id"));
  4939. }
  4940. if(element.hasAttribute("format"))
  4941. {
  4942. display->setDisplayFormat(element.attribute("format"));
  4943. }
  4944. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4945. layout->addWidget(display);
  4946. }
  4947. @ When multiple timer or temperature displays are required, it can be useful to
  4948. provide a label to indicate just what is being measured.
  4949. @<Functions for scripting@>=
  4950. void addDecorationToLayout(QDomElement element, QStack<QWidget *> *,@|
  4951. QStack<QLayout *> *layoutStack)
  4952. {
  4953. @<Set up decoration@>@;
  4954. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  4955. layout->addWidget(decoration);
  4956. }
  4957. void addDecorationToSplitter(QDomElement element,
  4958. QStack<QWidget *> *widgetStack,
  4959. QStack<QLayout *> *)
  4960. {
  4961. @<Set up decoration@>@;
  4962. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  4963. splitter->addWidget(decoration);
  4964. }
  4965. @ The decoration needs a label text, an orientation, and the widget to be
  4966. labeled.
  4967. @<Set up decoration@>=
  4968. QString labelText =
  4969. QCoreApplication::translate("configuration",
  4970. element.attribute("name").toUtf8().data());
  4971. Qt::Orientations@, orientation = Qt::Horizontal;
  4972. if(element.hasAttribute("type"))
  4973. {
  4974. if(element.attribute("type") == "horizontal")
  4975. {
  4976. orientation = Qt::Horizontal;
  4977. }
  4978. else if(element.attribute("type") == "vertical")
  4979. {
  4980. orientation = Qt::Vertical;
  4981. }
  4982. }
  4983. @<Find widget to decorate@>@;
  4984. WidgetDecorator *decoration = new WidgetDecorator(theWidget, labelText,
  4985. orientation);
  4986. if(element.hasAttribute("id"))
  4987. {
  4988. decoration->setObjectName(element.attribute("id"));
  4989. }
  4990. @ The widget to decorate should be found as a child of the {\tt <decoration>}
  4991. element.
  4992. @<Find widget to decorate@>=
  4993. QWidget *theWidget = NULL;
  4994. QDomNodeList children = element.childNodes();
  4995. for(int i = 0; i < children.count(); i++)
  4996. {
  4997. QDomNode item = children.at(i);
  4998. if(item.isElement())
  4999. {
  5000. QDomElement itemElement = item.toElement();
  5001. if(itemElement.tagName() == "lcdtemperature")
  5002. {
  5003. TemperatureDisplay *display = new TemperatureDisplay;
  5004. if(itemElement.hasAttribute("id"))
  5005. {
  5006. display->setObjectName(itemElement.attribute("id"));
  5007. }
  5008. theWidget = display;
  5009. }
  5010. else if(itemElement.tagName() == "lcdtimer")
  5011. {
  5012. TimerDisplay *display = new TimerDisplay;
  5013. if(itemElement.hasAttribute("id"))
  5014. {
  5015. display->setObjectName(itemElement.attribute("id"));
  5016. }
  5017. if(itemElement.hasAttribute("format"))
  5018. {
  5019. display->setDisplayFormat(itemElement.attribute("format"));
  5020. }
  5021. theWidget = display;
  5022. }
  5023. }
  5024. }
  5025. @ As splitters cannot contain layouts directly, there is a need to allow
  5026. otherwise empty widgets to be included in a splitter for cases where a splitter
  5027. should manage several widgets together as a group. A row of annotation buttons
  5028. is an example of such a layout.
  5029. When splitters are used as a way to hide optional features it sometimes has the
  5030. effect of forcing a window to stay larger than should be required. To fix this,
  5031. it is possible to set the \tt{ignoreSizePolicy} attribute to true. While this
  5032. does solve the window size issue, this technique is inconsistent with generally
  5033. expected behavior and its use should generally be discouraged.
  5034. @<Functions for scripting@>=
  5035. void addWidgetToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  5036. QStack<QLayout *> *layoutStack)
  5037. {
  5038. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  5039. QWidget *widget = new QWidget;
  5040. if(element.hasAttribute("id"))
  5041. {
  5042. widget->setObjectName(element.attribute("id"));
  5043. }
  5044. if(element.hasAttribute("ignoreSizePolicy"))
  5045. {
  5046. if(element.attribute("ignoreSizePolicy") == "true")
  5047. {
  5048. widget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  5049. }
  5050. }
  5051. splitter->addWidget(widget);
  5052. if(element.hasChildNodes())
  5053. {
  5054. widgetStack->push(widget);
  5055. populateWidget(element, widgetStack, layoutStack);
  5056. widgetStack->pop();
  5057. }
  5058. }
  5059. void populateWidget(QDomElement element, QStack<QWidget *> *widgetStack,@|
  5060. QStack<QLayout *> *layoutStack)
  5061. {
  5062. QDomNodeList children = element.childNodes();
  5063. for(int i = 0; i < children.count(); i++)
  5064. {
  5065. QDomNode current;
  5066. QDomElement currentElement;
  5067. current = children.at(i);
  5068. if(current.isElement())
  5069. {
  5070. currentElement = current.toElement();
  5071. if(currentElement.tagName() == "layout")
  5072. {
  5073. currentElement.setAttribute("trcontext", "configuration");
  5074. addLayoutToWidget(currentElement, widgetStack, layoutStack);
  5075. }
  5076. }
  5077. }
  5078. }
  5079. @ There are two types of buttons that can be added to a layout. There are normal
  5080. push buttons and there are annotation buttons. Other button types may be added
  5081. in the future.
  5082. @<Functions for scripting@>=
  5083. void addButtonToLayout(QDomElement element, QStack<QWidget *> *,@|
  5084. QStack<QLayout *> *layoutStack)
  5085. {
  5086. QAbstractButton *button = NULL;
  5087. QString text =
  5088. QCoreApplication::translate("configuration",
  5089. element.attribute("name").toUtf8().data());
  5090. if(element.hasAttribute("type"))
  5091. {
  5092. QString type = element.attribute("type");
  5093. if(type == "annotation")
  5094. {
  5095. AnnotationButton *abutton = new AnnotationButton(text);
  5096. if(element.hasAttribute("annotation"))
  5097. {
  5098. abutton->setAnnotation(element.attribute("annotation"));
  5099. }
  5100. if(element.hasAttribute("series"))
  5101. {
  5102. abutton->setTemperatureColumn(element.attribute("series").
  5103. toInt());
  5104. }
  5105. if(element.hasAttribute("column"))
  5106. {
  5107. abutton->setAnnotationColumn(element.attribute("column").
  5108. toInt());
  5109. }
  5110. button = abutton;
  5111. }
  5112. else if(type == "check")
  5113. {
  5114. button = new QCheckBox(text);
  5115. }
  5116. else if(type == "push")
  5117. {
  5118. button = new QPushButton(text);
  5119. }
  5120. }
  5121. if(element.hasAttribute("id"))
  5122. {
  5123. button->setObjectName(element.attribute("id"));
  5124. }
  5125. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5126. layout->addWidget(button);
  5127. }
  5128. @ While annotation buttons are useful for many batch notes, a spin box is
  5129. sometimes a better input choice. There are several attributes that can be set on
  5130. a spin box. These include text to be included in the annotation before and after
  5131. the value of the spin box, the temperature and annotation columns, the range of
  5132. values available in the spin box, the precision of allowed values, and the
  5133. amount by which increment and decrement operations change the value.
  5134. @<Functions for scripting@>=
  5135. void addSpinBoxToLayout(QDomElement element, QStack<QWidget *> *,@|
  5136. QStack<QLayout *> *layoutStack)
  5137. {
  5138. AnnotationSpinBox *box = new AnnotationSpinBox("", "", NULL);
  5139. if(element.hasAttribute("pretext"))
  5140. {
  5141. box->setPretext(QCoreApplication::translate(
  5142. "configuration",
  5143. element.attribute("pretext").toUtf8().data()));
  5144. }
  5145. if(element.hasAttribute("posttext"))
  5146. {
  5147. box->setPosttext(QCoreApplication::translate(
  5148. "configuration",
  5149. element.attribute("posttext").toUtf8().data()));
  5150. }
  5151. if(element.hasAttribute("series"))
  5152. {
  5153. box->setTemperatureColumn(element.attribute("series").toInt());
  5154. }
  5155. if(element.hasAttribute("column"))
  5156. {
  5157. box->setAnnotationColumn(element.attribute("column").toInt());
  5158. }
  5159. if(element.hasAttribute("min"))
  5160. {
  5161. box->setMinimum(element.attribute("min").toDouble());
  5162. }
  5163. if(element.hasAttribute("max"))
  5164. {
  5165. box->setMaximum(element.attribute("max").toDouble());
  5166. }
  5167. if(element.hasAttribute("decimals"))
  5168. {
  5169. box->setDecimals(element.attribute("decimals").toInt());
  5170. }
  5171. if(element.hasAttribute("step"))
  5172. {
  5173. box->setSingleStep(element.attribute("step").toDouble());
  5174. }
  5175. if(element.hasAttribute("id"))
  5176. {
  5177. box->setObjectName(element.attribute("id"));
  5178. }
  5179. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5180. layout->addWidget(box);
  5181. }
  5182. @ Previously, in order to change a |ZoomLog| from the default set of columns,
  5183. script code would need to alter the column set. While this works fine on a Mac,
  5184. this did not work very well under Windows. For the current version, I would like
  5185. to remove the need to deal with table columns from the host environment. The
  5186. first step for this is allowing column descriptions in XML. After this, I'@q'@>d like
  5187. to remove the default column set from the widget code and provide some better
  5188. functionality for dealing with additional data sets.
  5189. When creating the |ZoomLog| here, we check for {\tt <column>} child elements
  5190. which specify the names of the columns.
  5191. @<Functions for scripting@>=
  5192. void addZoomLogToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  5193. QStack<QLayout *> *)
  5194. {
  5195. ZoomLog *widget = new ZoomLog;
  5196. if(element.hasAttribute("id"))
  5197. {
  5198. widget->setObjectName(element.attribute("id"));
  5199. }
  5200. if(element.hasChildNodes())
  5201. {
  5202. QDomNodeList children = element.childNodes();
  5203. int column = 0;
  5204. for(int i = 0; i < children.count(); i++)
  5205. {
  5206. QDomNode current;
  5207. QDomElement currentElement;
  5208. current = children.at(i);
  5209. if(current.isElement())
  5210. {
  5211. currentElement = current.toElement();
  5212. if(currentElement.tagName() == "column")
  5213. {
  5214. QString text =
  5215. QCoreApplication::translate(
  5216. "configuration",
  5217. currentElement.text().toUtf8().data());
  5218. widget->setHeaderData(column, text);
  5219. column++;
  5220. }
  5221. }
  5222. }
  5223. }
  5224. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  5225. if(splitter)
  5226. {
  5227. splitter->addWidget(widget);
  5228. }
  5229. else
  5230. {
  5231. qDebug() << "Splitter not found at top of widget stack!";
  5232. }
  5233. }
  5234. @ The last of the widgets needed to duplicate the window provided in previous
  5235. versions of \pn{} is the |GraphView|.
  5236. @<Functions for scripting@>=
  5237. void addGraphToSplitter(QDomElement element, QStack<QWidget *> *widgetStack,
  5238. QStack<QLayout *> *)
  5239. {
  5240. GraphView *view = new GraphView;
  5241. if(element.hasAttribute("id"))
  5242. {
  5243. view->setObjectName(element.attribute("id"));
  5244. }
  5245. QSplitter *splitter = qobject_cast<QSplitter *>(widgetStack->top());
  5246. splitter->addWidget(view);
  5247. }
  5248. @ When interacting with a database, it can be useful to provide a combo box
  5249. populated by the results of a database query. One way to do this is through a
  5250. |SqlComboBox| widget.
  5251. @<Functions for scripting@>=
  5252. void addSqlDropToLayout(QDomElement element, QStack<QWidget *> *,@|
  5253. QStack<QLayout *> *layoutStack)
  5254. {
  5255. SqlComboBox *box = new SqlComboBox();
  5256. if(element.hasAttribute("data"))
  5257. {
  5258. box->setDataColumn(element.attribute("data").toInt());
  5259. }
  5260. if(element.hasAttribute("display"))
  5261. {
  5262. box->setDisplayColumn(element.attribute("display").toInt());
  5263. }
  5264. if(element.hasAttribute("showdata"))
  5265. {
  5266. if(element.attribute("showdata") == "true")
  5267. {
  5268. box->showData(true);
  5269. }
  5270. }
  5271. if(element.hasAttribute("editable"))
  5272. {
  5273. if(element.attribute("editable") == "true")
  5274. {
  5275. box->setEditable(true);
  5276. }
  5277. }
  5278. if(element.hasChildNodes())
  5279. {
  5280. QDomNodeList children = element.childNodes();
  5281. for(int i = 0; i < children.count(); i++)
  5282. {
  5283. QDomNode current;
  5284. QDomElement currentElement;
  5285. current = children.at(i);
  5286. if(current.isElement())
  5287. {
  5288. currentElement = current.toElement();
  5289. if(currentElement.tagName() == "null")
  5290. {
  5291. box->addNullOption();
  5292. }
  5293. else if(currentElement.tagName() == "query")
  5294. {
  5295. box->addSqlOptions(currentElement.text());
  5296. }
  5297. }
  5298. }
  5299. }
  5300. if(element.hasAttribute("id"))
  5301. {
  5302. box->setObjectName(element.attribute("id"));
  5303. }
  5304. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5305. layout->addWidget(box);
  5306. }
  5307. @ The next database aware widget that can be useful to have in \pn{} is a
  5308. {\bf S}QL {\bf A}rray {\bf L}iteral {\bf T}able. As might be apparent from the
  5309. name, this is a table view with an associated model and delegates appropriate
  5310. for creating ordered arrays to pass into a database. Each column represents an
  5311. array of values. The most common use of this is in cases where it is important
  5312. to produce multiple arrays of the same size in which each element of one array
  5313. is related to the element in the same position of another array. For example,
  5314. when roasting coffee there are times when some may want to add more than one
  5315. coffee to the roaster at a time. In order to correctly track the green coffee
  5316. inventory and so that the roasting log may have an accurate record of what is
  5317. really happening, insertions on the roasting log provide two arrays, one
  5318. representing all of the coffees being added to the roaster, the other the amount
  5319. of each of these coffees. The database can then use a trigger function to
  5320. examine these arrays and produce the necessary entries in the use table which in
  5321. turn update the record containing the amount of each green coffee currently in
  5322. stock.
  5323. While a generic |QTableView| is used here, there is a need to add functionality
  5324. specific to using this table with a |SaltModel| when obtaining this widget from
  5325. the host environment. In order to accomodate this, we add a dynamic property to
  5326. the view to identify the type of table in the absense of a unique class name.
  5327. @<Functions for scripting@>=
  5328. void addSaltToLayout(QDomElement element, QStack<QWidget *> *,@|
  5329. QStack<QLayout *> *layoutStack)
  5330. {
  5331. QTableView *view = new QTableView;
  5332. view->setProperty("tabletype", QVariant(QString("SaltTable")));
  5333. SaltModel *model = new SaltModel(element.childNodes().count());
  5334. if(element.hasAttribute("id"))
  5335. {
  5336. view->setObjectName(element.attribute("id"));
  5337. }
  5338. if(element.hasAttribute("editable"))
  5339. {
  5340. if(element.attribute("editable") == "false")
  5341. {
  5342. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  5343. }
  5344. }
  5345. if(element.hasAttribute("selectionBehavior"))
  5346. {
  5347. if(element.attribute("selectionBehavior") == "items")
  5348. {
  5349. view->setSelectionBehavior(QAbstractItemView::SelectItems);
  5350. }
  5351. else if(element.attribute("selectionBehavior") == "rows")
  5352. {
  5353. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  5354. }
  5355. else if(element.attribute("selectionBehavior") == "columns")
  5356. {
  5357. view->setSelectionBehavior(QAbstractItemView::SelectColumns);
  5358. }
  5359. }
  5360. if(element.hasChildNodes())
  5361. {
  5362. QDomNodeList children = element.childNodes();
  5363. int currentColumn = 0;
  5364. for(int i = 0; i < children.count(); i++)
  5365. {
  5366. QDomNode current;
  5367. QDomElement currentElement;
  5368. current = children.at(i);
  5369. if(current.isElement())
  5370. {
  5371. currentElement = current.toElement();
  5372. if(currentElement.tagName() == "column")
  5373. {
  5374. if(currentElement.hasAttribute("name"))
  5375. {
  5376. model->setHeaderData(currentColumn,
  5377. Qt::Horizontal,
  5378. QCoreApplication::translate(
  5379. "configuration",
  5380. currentElement.attribute("name").toUtf8().data()));
  5381. }
  5382. if(currentElement.hasAttribute("delegate"))
  5383. {
  5384. @<Set column delegate from XML attribute@>@;
  5385. }
  5386. currentColumn++;
  5387. }
  5388. }
  5389. }
  5390. }
  5391. view->setModel(model);
  5392. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5393. layout->addWidget(view);
  5394. }
  5395. @ It is often desirable to restrict the allowed values in an entry to either a
  5396. set of specific values or to a particular type of value. Delegates can be set
  5397. on a column to enforce such restrictions.
  5398. @<Set column delegate from XML attribute@>=
  5399. if(currentElement.attribute("delegate") == "sql")
  5400. {
  5401. @<Assign column delegate from SQL@>@;
  5402. }
  5403. else if(currentElement.attribute("delegate") == "numeric")
  5404. {
  5405. @<Assign numeric column delegate@>@;
  5406. }
  5407. else if(currentElement.attribute("delegate") == "positivenumeric")
  5408. {
  5409. @<Assign positive numeric column delegate@>@;
  5410. }
  5411. @ When using a |SaltModel|, there are times where the array values being
  5412. inserted are identification numbers representing some record that already exists
  5413. in the database. For example, the id number representing a green coffee in the
  5414. table of items. In such a case, it is beneficial to provide a delegate capable
  5415. of presenting a human readable list of choices.
  5416. @<Assign column delegate from SQL@>=
  5417. SqlComboBoxDelegate *delegate = new SqlComboBoxDelegate;
  5418. SqlComboBox *widget = new SqlComboBox();
  5419. if(currentElement.hasAttribute("nulltext"))
  5420. {
  5421. widget->setNullText(currentElement.attribute("nulltext"));
  5422. }
  5423. if(currentElement.hasAttribute("nulldata"))
  5424. {
  5425. widget->setNullData(QVariant(currentElement.attribute("nulldata")));
  5426. }
  5427. if(currentElement.hasAttribute("null"))
  5428. {
  5429. if(currentElement.attribute("null") == "true")
  5430. {
  5431. widget->addNullOption();
  5432. }
  5433. }
  5434. if(currentElement.hasAttribute("showdata"))
  5435. {
  5436. if(currentElement.attribute("showdata") == "true")
  5437. {
  5438. widget->showData(true);
  5439. }
  5440. }
  5441. if(currentElement.hasAttribute("data"))
  5442. {
  5443. widget->setDataColumn(currentElement.attribute("data").toInt());
  5444. }
  5445. if(currentElement.hasAttribute("display"))
  5446. {
  5447. widget->setDisplayColumn(currentElement.attribute("display").toInt());
  5448. }
  5449. widget->addSqlOptions(currentElement.text());
  5450. delegate->setWidget(widget);
  5451. view->setItemDelegateForColumn(currentColumn, delegate);
  5452. @ Another common use is allowing numeric values.
  5453. @<Assign numeric column delegate@>=
  5454. NumericDelegate *delegate = new NumericDelegate;
  5455. view->setItemDelegateForColumn(currentColumn, delegate);
  5456. @ It is also possible to restrict allowed numeric values to non-negative
  5457. values.
  5458. @<Assign positive numeric column delegate@>=
  5459. NumericDelegate *delegate = new NumericDelegate(true);
  5460. view->setItemDelegateForColumn(currentColumn, delegate);
  5461. @ The |NumericDelegate| will only set the display value to a number, but it
  5462. will perform mathematical calculations that are entered into the editor as
  5463. well. This allows a person to type something like $13.26+5.06$ with the result
  5464. of the expression ($18.32$) appearing in the table.
  5465. @<Class declarations@>=
  5466. class NumericDelegate : public QItemDelegate@/
  5467. {
  5468. @[Q_OBJECT@]@;
  5469. public:@/
  5470. NumericDelegate(bool positiveOnly = false, QObject *parent = NULL);
  5471. QWidget *createEditor(QWidget *parent,
  5472. const QStyleOptionViewItem &option,@|
  5473. const QModelIndex &index) const;
  5474. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  5475. void setModelData(QWidget *editor, QAbstractItemModel *model,@|
  5476. const QModelIndex &index) const;
  5477. void updateEditorGeometry(QWidget *editor,
  5478. const QStyleOptionViewItem &option,@|
  5479. const QModelIndex &index) const;
  5480. private:
  5481. bool m_positiveOnly;
  5482. };
  5483. @ There is nothing special about the constructor.
  5484. @<NumericDelegate implementation@>=
  5485. NumericDelegate::NumericDelegate(bool positiveOnly, QObject *parent) :
  5486. QItemDelegate(parent), m_positiveOnly(positiveOnly)
  5487. {
  5488. /* Nothing needs to be done here. */
  5489. }
  5490. @ Two roles are used by this delegate. The edit role should contain whatever
  5491. text has been entered in the editor while the display role contain the numeric
  5492. result of any expression that has been entered. Our editor only requires the
  5493. first of these.
  5494. @<NumericDelegate implementation@>=
  5495. void NumericDelegate::setEditorData(QWidget *editor,
  5496. const QModelIndex &index) const
  5497. {
  5498. QString value = index.model()->data(index, Qt::EditRole).toString();
  5499. QLineEdit *line = static_cast<QLineEdit*>(editor);
  5500. line->setText(value);
  5501. }
  5502. @ When editing is finished, the expression text must be saved back to the
  5503. model and the expression should be evaluated to set the display role. We make
  5504. use of the existing scripting engine to evaluate the expression, but only
  5505. preserve the result in the display role if the result of that expression is
  5506. numeric.
  5507. @<NumericDelegate implementation@>=
  5508. void NumericDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  5509. const QModelIndex &index) const
  5510. {
  5511. QLineEdit *line = static_cast<QLineEdit*>(editor);
  5512. model->setData(index, line->text(), Qt::EditRole);
  5513. QScriptEngine *engine = AppInstance->engine;
  5514. engine->pushContext();
  5515. QString script = QString("Number(%1)").arg(line->text());
  5516. QScriptValue result = engine->evaluate(line->text());
  5517. if(result.isNumber())
  5518. {
  5519. if(m_positiveOnly)
  5520. {
  5521. if(result.toNumber() < 0)
  5522. {
  5523. model->setData(index, QVariant(), Qt::DisplayRole);
  5524. }
  5525. else
  5526. {
  5527. model->setData(index, result.toVariant(), Qt::DisplayRole);
  5528. }
  5529. }
  5530. else
  5531. {
  5532. model->setData(index, result.toVariant(), Qt::DisplayRole);
  5533. }
  5534. }
  5535. else
  5536. {
  5537. model->setData(index, QVariant(), Qt::DisplayRole);
  5538. }
  5539. engine->popContext();
  5540. }
  5541. @ There is nothing special about the line edit used for this.
  5542. @<NumericDelegate implementation@>=
  5543. QWidget* NumericDelegate::createEditor(QWidget *parent,
  5544. const QStyleOptionViewItem &,
  5545. const QModelIndex &) const
  5546. {
  5547. return (new QLineEdit(parent));
  5548. }
  5549. @ To ensure that the editor is displayed appropriately, we must pass the
  5550. geometry data to our editor.
  5551. @<NumericDelegate implementation@>=
  5552. void NumericDelegate::updateEditorGeometry(QWidget *editor,
  5553. const QStyleOptionViewItem &option,
  5554. const QModelIndex &) const
  5555. {
  5556. editor->setGeometry(option.rect);
  5557. }
  5558. @ The same general technique is useful for input to a |QLineEdit|, but there is
  5559. no model backing that widget to preserve multiple data roles. One way to get
  5560. this functionality without too much effort is to abuse |QValidator| to evaluate
  5561. expressions.
  5562. @<Class declarations@>=
  5563. class ScriptValidator : public QValidator
  5564. {
  5565. Q_OBJECT
  5566. public:
  5567. ScriptValidator(QValidator *validator, QObject *parent = NULL);
  5568. void fixup(QString &input) const;
  5569. QValidator::State validate(QString &input, int &pos) const;
  5570. private:
  5571. QValidator *v;
  5572. };
  5573. @ The key to this is in the |fixup()| method. Here we over-write input with the
  5574. result of the script evaluation.
  5575. @<ScriptValidator implementation@>=
  5576. void ScriptValidator::fixup(QString &input) const
  5577. {
  5578. QScriptEngine *engine = AppInstance->engine;
  5579. engine->pushContext();
  5580. input = engine->evaluate(input).toString();
  5581. engine->popContext();
  5582. }
  5583. @ This validator is intended to be used in conjunction with another one which
  5584. determines if the result of the expression is acceptable. In this case
  5585. |Invalid| is never returned.
  5586. @<ScriptValidator implementation@>=
  5587. QValidator::State ScriptValidator::validate(QString &input, int &pos) const
  5588. {
  5589. if(v)
  5590. {
  5591. if(v->validate(input, pos) == QValidator::Acceptable)
  5592. {
  5593. return QValidator::Acceptable;
  5594. }
  5595. }
  5596. return QValidator::Intermediate;
  5597. }
  5598. @ The constructor is trivial.
  5599. @<ScriptValidator implementation@>=
  5600. ScriptValidator::ScriptValidator(QValidator *validator, QObject *parent)
  5601. : QValidator(parent), v(validator)
  5602. {
  5603. /* Nothing needs to be done here. */
  5604. }
  5605. @ This is included in typica.cpp.
  5606. @<Class implementations@>=
  5607. @<ScriptValidator implementation@>
  5608. @ Line edits are useful when the user is expected to enter text without a
  5609. predetermined set of values.
  5610. Several attributes are supported on line edits. In addition to the usual
  5611. {\tt id} attribute, there is also a {\tt writable} attribute which, if
  5612. {\tt false}, can be used to create read only text areas which can only be edited
  5613. from script code. A {\tt validator} attribute allows entered text to be
  5614. restricted. This can take one of three values. If the value is {\tt "numeric"},
  5615. input is restricted to numeric values. If the value is {\tt "integer"}, input is
  5616. restricted to integer values. Finally, if the value is {\tt "expression"}, input
  5617. is restricted to text which matches a regular expression specified as the value
  5618. of the {\tt expression} attribute.
  5619. Note that when integer and numeric validators are specified, these are set for
  5620. a |ScriptValidator| to enable some basic expression evaluation.
  5621. @<Functions for scripting@>=
  5622. void addLineToLayout(QDomElement element, QStack<QWidget *> *,@|
  5623. QStack<QLayout *> *layoutStack)
  5624. {
  5625. QLineEdit *widget = new QLineEdit(element.text());
  5626. if(element.hasAttribute("id"))
  5627. {
  5628. widget->setObjectName(element.attribute("id"));
  5629. }
  5630. if(element.hasAttribute("writable"))
  5631. {
  5632. if(element.attribute("writable") == "false")
  5633. {
  5634. widget->setReadOnly(true);
  5635. }
  5636. }
  5637. if(element.hasAttribute("validator"))
  5638. {
  5639. if(element.attribute("validator") == "numeric")
  5640. {
  5641. widget->setValidator(new ScriptValidator(new QDoubleValidator));
  5642. }
  5643. else if(element.attribute("validator") == "integer")
  5644. {
  5645. widget->setValidator(new ScriptValidator(new QIntValidator));
  5646. }
  5647. else if(element.attribute("validator") == "expression" &&
  5648. element.hasAttribute("expression"))
  5649. {
  5650. widget->setValidator(new QRegExpValidator(
  5651. QRegExp(element.attribute("expression")),
  5652. NULL));
  5653. }
  5654. }
  5655. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5656. layout->addWidget(widget);
  5657. }
  5658. @ It is natural for certain database fields to enter potentially large amounts
  5659. of free form text, for example, notes and annotations.
  5660. @<Functions for scripting@>=
  5661. void addTextToLayout(QDomElement element, QStack<QWidget *> *,@|
  5662. QStack<QLayout *> *layoutStack)
  5663. {
  5664. QTextEdit *widget = new QTextEdit;
  5665. if(element.hasAttribute("id"))
  5666. {
  5667. widget->setObjectName(element.attribute("id"));
  5668. }
  5669. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5670. layout->addWidget(widget);
  5671. }
  5672. @ The common use of |SqlQueryView| calls for the possibility of changing the
  5673. query during use. As such, there is little reason to accept attributes other
  5674. than an id for obtaining the view in a script.
  5675. @<Functions for scripting@>=
  5676. void addSqlQueryViewToLayout(QDomElement element,
  5677. QStack<QWidget *> *,@|
  5678. QStack<QLayout *> *layoutStack)
  5679. {
  5680. SqlQueryView *view = new SqlQueryView;
  5681. if(element.hasAttribute("id"))
  5682. {
  5683. view->setObjectName(element.attribute("id"));
  5684. }
  5685. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5686. layout->addWidget(view);
  5687. }
  5688. @ When the user is expected to enter a date, it is appropriate to use a date
  5689. editor. This one provides a calendar.
  5690. @<Functions for scripting@>=
  5691. void addCalendarToLayout(QDomElement element, QStack<QWidget *> *,@|
  5692. QStack<QLayout *> *layoutStack)
  5693. {
  5694. QWidget *widget;
  5695. if(element.hasAttribute("time"))
  5696. {
  5697. if(element.attribute("time") == "true")
  5698. {
  5699. QDateTimeEdit *edit = new QDateTimeEdit;
  5700. edit->setDateTime(QDateTime::currentDateTime());
  5701. edit->setCalendarPopup(true);
  5702. edit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
  5703. widget = qobject_cast<QWidget *>(edit);
  5704. }
  5705. else
  5706. {
  5707. QDateEdit *edit = new QDateEdit;
  5708. edit->setDate(QDate::currentDate());
  5709. edit->setCalendarPopup(true);
  5710. edit->setDisplayFormat("yyyy-MM-dd");
  5711. widget = qobject_cast<QWidget *>(edit);
  5712. }
  5713. }
  5714. else
  5715. {
  5716. QDateEdit *edit = new QDateEdit;
  5717. edit->setDate(QDate::currentDate());
  5718. edit->setCalendarPopup(true);
  5719. edit->setDisplayFormat("yyyy-MM-dd");
  5720. widget = qobject_cast<QWidget *>(edit);
  5721. }
  5722. if(element.hasAttribute("id"))
  5723. {
  5724. widget->setObjectName(element.attribute("id"));
  5725. }
  5726. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5727. layout->addWidget(widget);
  5728. }
  5729. @ Some additional properties are added to this object when it is retrieved by
  5730. the host environment.
  5731. @<Functions for scripting@>=
  5732. void setQDateEditProperties(QScriptValue value, QScriptEngine *engine)
  5733. {
  5734. setQDateTimeEditProperties(value, engine);
  5735. }
  5736. void setQDateTimeEditProperties(QScriptValue value, QScriptEngine *engine)
  5737. {
  5738. setQAbstractSpinBoxProperties(value, engine);
  5739. value.setProperty("setDate", engine->newFunction(QDateTimeEdit_setDate));
  5740. value.setProperty("day", engine->newFunction(QDateTimeEdit_day));
  5741. value.setProperty("month", engine->newFunction(QDateTimeEdit_month));
  5742. value.setProperty("year", engine->newFunction(QDateTimeEdit_year));
  5743. value.setProperty("setToCurrentTime",
  5744. engine->newFunction(QDateTimeEdit_setToCurrentTime));
  5745. }
  5746. @ Certain operations on a |QDateEdit| are easier with a few convenience
  5747. properties that bypass the need to use the built in |date| property. For
  5748. example, an editor that should be set to 1 January of the current year can
  5749. obtain the year and set the date without directly using a |QDate| object.
  5750. @<Functions for scripting@>=
  5751. QScriptValue QDateTimeEdit_setDate(QScriptContext *context, QScriptEngine *)
  5752. {
  5753. QDateTimeEdit *self = getself<QDateTimeEdit *>(context);
  5754. if(context->argumentCount() == 3)
  5755. {
  5756. self->setDate(QDate(argument<int>(0, context),
  5757. argument<int>(1, context),
  5758. argument<int>(2, context)));
  5759. }
  5760. else
  5761. {
  5762. context->throwError("Incorrect number of arguments passed to "
  5763. "QDateTimeEdit::setDate(). This method takes three integer arguments "
  5764. "specifying the year, month, and day.");
  5765. }
  5766. return QScriptValue();
  5767. }
  5768. QScriptValue QDateTimeEdit_day(QScriptContext *context, QScriptEngine *)
  5769. {
  5770. QDateTimeEdit *self = getself<QDateTimeEdit *>(context);
  5771. return QScriptValue(self->date().day());
  5772. }
  5773. QScriptValue QDateTimeEdit_month(QScriptContext *context, QScriptEngine *)
  5774. {
  5775. QDateTimeEdit *self = getself<QDateTimeEdit *>(context);
  5776. return QScriptValue(self->date().month());
  5777. }
  5778. QScriptValue QDateTimeEdit_year(QScriptContext *context, QScriptEngine *)
  5779. {
  5780. QDateTimeEdit *self = getself<QDateTimeEdit *>(context);
  5781. return QScriptValue(self->date().year());
  5782. }
  5783. QScriptValue QDateTimeEdit_setToCurrentTime(QScriptContext *context, QScriptEngine *)
  5784. {
  5785. QDateTimeEdit *self = getself<QDateTimeEdit *>(context);
  5786. self->setDateTime(QDateTime::currentDateTime());
  5787. return QScriptValue();
  5788. }
  5789. @ A few function prototypes are needed for this.
  5790. @<Function prototypes for scripting@>=
  5791. void setQDateEditProperties(QScriptValue value, QScriptEngine *engine);
  5792. void setQDateTimeEditProperties(QScriptValue value, QScriptEngine *engine);
  5793. QScriptValue QDateTimeEdit_setDate(QScriptContext *context,
  5794. QScriptEngine *engine);
  5795. QScriptValue QDateTimeEdit_day(QScriptContext *context, QScriptEngine *engine);
  5796. QScriptValue QDateTimeEdit_month(QScriptContext *context,
  5797. QScriptEngine *engine);
  5798. QScriptValue QDateTimeEdit_year(QScriptContext *context, QScriptEngine *engine);
  5799. QScriptValue QDateTimeEdit_setToCurrentTime(QScriptContext *context, QScriptEngine *engine);
  5800. @ Sometimes it can be useful to allow editing a time or duration value without
  5801. a date field. For this, a |QTimeEdit| can be used.
  5802. @<Functions for scripting@>=
  5803. void addTimeEditToLayout(QDomElement element, QStack<QWidget *> *,@|
  5804. QStack<QLayout *> *layoutStack)
  5805. {
  5806. QTimeEdit *edit = new QTimeEdit;
  5807. if(element.hasAttribute("displayFormat"))
  5808. {
  5809. edit->setDisplayFormat(element.attribute("displayFormat"));
  5810. }
  5811. else
  5812. {
  5813. edit->setDisplayFormat("mm:ss.zzz");
  5814. }
  5815. if(element.hasAttribute("id"))
  5816. {
  5817. edit->setObjectName(element.attribute("id"));
  5818. }
  5819. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  5820. layout->addWidget(edit);
  5821. }
  5822. @ Additional properties are added as a |QTimeEdit| is a |QDateTimeEdit|.
  5823. @<Functions for scripting@>=
  5824. void setQTimeEditProperties(QScriptValue value, QScriptEngine *engine)
  5825. {
  5826. setQDateTimeEditProperties(value, engine);
  5827. }
  5828. @ A function prototype is needed.
  5829. @<Function prototypes for scripting@>=
  5830. void setQTimeEditProperties(QScriptValue value, QScriptEngine *engine);
  5831. @ In order to get to objects created from the XML description, it is necessary
  5832. to provide a function that can be called to retrieve children of a given widget.
  5833. When providing such an object to the script, it is necessary to determine the
  5834. type of that object and add the appropriate properties.
  5835. @<Function prototypes for scripting@>=
  5836. QScriptValue findChildObject(QScriptContext *context, QScriptEngine *engine);
  5837. @ This function must be made available to the scripting engine.
  5838. @<Set up the scripting engine@>=
  5839. engine->globalObject().setProperty("findChildObject",
  5840. engine->newFunction(findChildObject));
  5841. @ This function takes a script value representing some object which may have
  5842. been created from an XML description and a string containing the name of the
  5843. requested child element.
  5844. @<Functions for scripting@>=
  5845. QScriptValue findChildObject(QScriptContext *context, QScriptEngine *engine)
  5846. {
  5847. QObject *parent = argument<QObject *>(0, context);
  5848. QString name = argument<QString>(1, context);
  5849. QObject *object = parent->findChild<QObject *>(name);
  5850. QScriptValue value;
  5851. if(object)
  5852. {
  5853. value = engine->newQObject(object);
  5854. QString className = object->metaObject()->className();
  5855. @<Set object properties based on class name@>@;
  5856. }
  5857. return value;
  5858. }
  5859. @ Properties are added for a large number of class types.
  5860. @<Set object properties based on class name@>=
  5861. if(className == "TemperatureDisplay")
  5862. {
  5863. setTemperatureDisplayProperties(value, engine);
  5864. }
  5865. else if(className == "TimerDisplay")
  5866. {
  5867. setTimerDisplayProperties(value, engine);
  5868. }
  5869. else if(className == "QAction")
  5870. {
  5871. setQActionProperties(value, engine);
  5872. }
  5873. else if(className == "QBoxLayout")
  5874. {
  5875. setQBoxLayoutProperties(value, engine);
  5876. }
  5877. else if(className == "QDateEdit")
  5878. {
  5879. setQDateEditProperties(value, engine);
  5880. }
  5881. else if(className == "QDateTimeEdit")
  5882. {
  5883. setQDateTimeEditProperties(value, engine);
  5884. }
  5885. else if(className == "QFrame")
  5886. {
  5887. setQFrameProperties(value, engine);
  5888. }
  5889. else if(className == "QHBoxLayout")
  5890. {
  5891. setQBoxLayoutProperties(value, engine);
  5892. }
  5893. else if(className == "QLCDNumber")
  5894. {
  5895. setQLCDNumberProperties(value, engine);
  5896. }
  5897. else if(className == "QMenu")
  5898. {
  5899. setQMenuProperties(value, engine);
  5900. }
  5901. else if(className == "QMenuBar")
  5902. {
  5903. setQMenuBarProperties(value, engine);
  5904. }
  5905. else if(className == "QPushButton")
  5906. {
  5907. setQPushButtonProperties(value, engine);
  5908. }
  5909. else if(className == "QSplitter")
  5910. {
  5911. setQSplitterProperties(value, engine);
  5912. }
  5913. else if(className == "QTableView")
  5914. {
  5915. if(object->property("tabletype").isValid())
  5916. {
  5917. if(object->property("tabletype").toString() == "SaltTable")
  5918. {
  5919. setSaltTableProperties(value, engine);
  5920. }
  5921. }
  5922. }
  5923. else if(className == "QVBoxLayout")
  5924. {
  5925. setQBoxLayoutProperties(value, engine);
  5926. }
  5927. else if(className == "QWidget")
  5928. {
  5929. setQWidgetProperties(value, engine);
  5930. }
  5931. else if(className == "ScriptQMainWindow")
  5932. {
  5933. setQMainWindowProperties(value, engine);
  5934. }
  5935. else if(className == "SqlComboBox")
  5936. {
  5937. setSqlComboBoxProperties(value, engine);
  5938. }
  5939. else if(className == "SqlQueryView")
  5940. {
  5941. setSqlQueryViewProperties(value, engine);
  5942. }
  5943. else if(className == "ZoomLog")
  5944. {
  5945. setZoomLogProperties(value, engine);
  5946. }
  5947. else if(className == "QTextEdit")
  5948. {
  5949. setQTextEditProperties(value, engine);
  5950. }
  5951. else if(className == "QWebView")
  5952. {
  5953. setQWebViewProperties(value, engine);
  5954. }
  5955. else if(className == "QLineEdit")
  5956. {
  5957. setQLineEditProperties(value, engine);
  5958. }
  5959. else if(className == "QSvgWidget")
  5960. {
  5961. setQSvgWidgetProperties(value, engine);
  5962. }
  5963. else if(className == "QTabBar")
  5964. {
  5965. setQTabBarProperties(value, engine);
  5966. }
  5967. else if(className == "PrinterSelector")
  5968. {
  5969. setQComboBoxProperties(value, engine);
  5970. }
  5971. @ In the list of classes, the SaltTable entry is for a class which does not
  5972. strictly exist on its own. It is, however, useful to provide some custom
  5973. properties when passing such an object to the host environment.
  5974. @<Function prototypes for scripting@>=
  5975. void setSaltTableProperties(QScriptValue value, QScriptEngine *engine);
  5976. QScriptValue SaltTable_bindableColumnArray(QScriptContext *context,
  5977. QScriptEngine *engine);
  5978. QScriptValue SaltTable_bindableQuotedColumnArray(QScriptContext *context,
  5979. QScriptEngine *engine);
  5980. QScriptValue SaltTable_columnSum(QScriptContext *context,
  5981. QScriptEngine *engine);
  5982. QScriptValue SaltTable_columnArray(QScriptContext *context,
  5983. QScriptEngine *engine);
  5984. QScriptValue SaltTable_data(QScriptContext *context, QScriptEngine *engine);
  5985. QScriptValue SaltTable_model(QScriptContext *context, QScriptEngine *engine);
  5986. QScriptValue SaltTable_quotedColumnArray(QScriptContext *context,
  5987. QScriptEngine *engine);
  5988. QScriptValue SaltTable_setData(QScriptContext *context, QScriptEngine *engine);
  5989. QScriptValue SaltTable_clear(QScriptContext *context, QScriptEngine *engine);
  5990. QScriptValue SaltTable_removeRow(QScriptContext *context, QScriptEngine *engine);
  5991. QScriptValue SaltTable_findData(QScriptContext *context, QScriptEngine *engine);
  5992. @ There are times when it is useful to obtain the sum of values in a column of
  5993. a SaltTable object. For example, when a column represents the weight of the
  5994. green coffee used in a batch, a sum of that column provides the total weight of
  5995. the batch which, when presented to the user, can be used to catch errors in
  5996. measuring batches or entering data.
  5997. @<Functions for scripting@>=
  5998. QScriptValue SaltTable_columnSum(QScriptContext *context, QScriptEngine *engine)
  5999. {
  6000. QTableView *self = getself<QTableView *>(context);
  6001. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6002. QString datum;
  6003. double total = 0.0;
  6004. int column = argument<int>(0, context);
  6005. int role = argument<int>(1, context);
  6006. for(int i = 0; i < model->rowCount(); i++)
  6007. {
  6008. datum = model->data(model->index(i, column), role).toString();
  6009. if(!datum.isEmpty())
  6010. {
  6011. total += datum.toDouble();
  6012. }
  6013. }
  6014. return QScriptValue(engine, total);
  6015. }
  6016. @ Another common use of the SaltTable is producing the text for an array literal
  6017. to pass into a SQL query. The |SaltModel| used by this table makes this simple.
  6018. There are four functions for this functionality for different use cases.
  6019. @<Functions for scripting@>=
  6020. QScriptValue SaltTable_columnArray(QScriptContext *context,
  6021. QScriptEngine *engine)
  6022. {
  6023. QTableView *self = getself<QTableView *>(context);
  6024. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6025. int column = argument<int>(0, context);
  6026. int role = argument<int>(1, context);
  6027. QString literal = model->arrayLiteral(column, role);
  6028. return QScriptValue(engine, literal);
  6029. }
  6030. QScriptValue SaltTable_quotedColumnArray(QScriptContext *context,
  6031. QScriptEngine *engine)
  6032. {
  6033. QTableView *self = getself<QTableView *>(context);
  6034. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6035. int column = argument<int>(0, context);
  6036. int role = argument<int>(1, context);
  6037. QString literal = model->quotedArrayLiteral(column, role);
  6038. return QScriptValue(engine, literal);
  6039. }
  6040. QScriptValue SaltTable_bindableColumnArray(QScriptContext *context,
  6041. QScriptEngine *engine)
  6042. {
  6043. QTableView *self = getself<QTableView *>(context);
  6044. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6045. int column = argument<int>(0, context);
  6046. int role = argument<int>(1, context);
  6047. QString literal = model->arrayLiteral(column, role);
  6048. literal.chop(1);
  6049. literal = literal.remove(0, 1);
  6050. return QScriptValue(engine, literal);
  6051. }
  6052. QScriptValue SaltTable_bindableQuotedColumnArray(QScriptContext *context,
  6053. QScriptEngine *engine)
  6054. {
  6055. QTableView *self = getself<QTableView *>(context);
  6056. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6057. int column = argument<int>(0, context);
  6058. int role = argument<int>(1, context);
  6059. QString literal = model->quotedArrayLiteral(column, role);
  6060. literal.chop(1);
  6061. literal = literal.remove(0, 1);
  6062. return QScriptValue(engine, literal);
  6063. }
  6064. @ In order to obtain signals related to changes in the model, we need a way to
  6065. get to the model from the host environment.
  6066. @<Functions for scripting@>=
  6067. QScriptValue SaltTable_model(QScriptContext *context, QScriptEngine *engine)
  6068. {
  6069. QTableView *self = getself<QTableView *>(context);
  6070. QScriptValue value = engine->newQObject(self->model());
  6071. return value;
  6072. }
  6073. @ While this table was originally intended strictly for user input, there are a
  6074. few use cases in which it is useful to allow scripts to set the values in the
  6075. table. This can be done with |setData|. This method takes four arguments: the
  6076. row and column of the table being set, the value to set, and the role of the
  6077. data being set.
  6078. @<Functions for scripting@>=
  6079. QScriptValue SaltTable_setData(QScriptContext *context, QScriptEngine *)
  6080. {
  6081. QTableView *self = getself<QTableView *>(context);
  6082. int row = argument<int>(0, context);
  6083. int column = argument<int>(1, context);
  6084. QVariant value = argument<QVariant>(2, context);
  6085. int role = argument<int>(3, context);
  6086. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6087. QModelIndex cell = model->index(row, column);
  6088. model->setData(cell, value, role);
  6089. self->update(cell);
  6090. return QScriptValue();
  6091. }
  6092. @ It is sometimes useful to obtain the data from a single cell of the table.
  6093. This can be done with the |data()| property.
  6094. @<Functions for scripting@>=
  6095. QScriptValue SaltTable_data(QScriptContext *context, QScriptEngine *engine)
  6096. {
  6097. QTableView *self = getself<QTableView *>(context);
  6098. int row = argument<int>(0, context);
  6099. int column = argument<int>(1, context);
  6100. int role = argument<int>(2, context);
  6101. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6102. QModelIndex cell = model->index(row, column);
  6103. QVariant value = model->data(cell, role);
  6104. QScriptValue retval = engine->newVariant(value);
  6105. retval.setProperty("value", QScriptValue(value.toString()));
  6106. return retval;
  6107. }
  6108. @ There are times when it is useful to clear the content of a table. This is
  6109. used, for example, in the green coffees table after changing the roasted coffee
  6110. item to eliminate excess rows in the case where the previously selected item
  6111. was a pre-roast blend.
  6112. @<Functions for scripting@>=
  6113. QScriptValue SaltTable_clear(QScriptContext *context, QScriptEngine *)
  6114. {
  6115. QTableView *self = getself<QTableView *>(context);
  6116. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6117. model->clear();
  6118. return QScriptValue();
  6119. }
  6120. @ It is sometimes useful to remove a row from a table. This is done in the new
  6121. batch window when the coffee for a row is set to a NULL item.
  6122. @<Functions for scripting@>=
  6123. QScriptValue SaltTable_removeRow(QScriptContext *context, QScriptEngine *engine)
  6124. {
  6125. QTableView *self = getself<QTableView *>(context);
  6126. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6127. int row = argument<int>(0, context);
  6128. return engine->newVariant(model->removeRow(row));
  6129. }
  6130. @ To remove the correct row, it is sometimes useful to query the table for
  6131. special values. This is done with the |findData()| method on the underlying
  6132. model.
  6133. @<Functions for scripting@>=
  6134. QScriptValue SaltTable_findData(QScriptContext *context, QScriptEngine *engine)
  6135. {
  6136. QTableView *self = getself<QTableView *>(context);
  6137. SaltModel *model = qobject_cast<SaltModel *>(self->model());
  6138. QVariant value = argument<QVariant>(0, context);
  6139. int column = argument<int>(1, context);
  6140. return engine->newVariant(model->findData(value, column));
  6141. }
  6142. @ These functions need to be added as properties of the table when it is passed
  6143. to the host environment.
  6144. @<Functions for scripting@>=
  6145. void setSaltTableProperties(QScriptValue value, QScriptEngine *engine)
  6146. {
  6147. setQWidgetProperties(value, engine);
  6148. value.setProperty("columnArray",
  6149. engine->newFunction(SaltTable_columnArray));
  6150. value.setProperty("quotedColumnArray",
  6151. engine->newFunction(SaltTable_quotedColumnArray));
  6152. value.setProperty("bindableColumnArray",
  6153. engine->newFunction(SaltTable_bindableColumnArray));
  6154. value.setProperty("bindableQuotedColumnArray",
  6155. engine->newFunction(SaltTable_bindableQuotedColumnArray));
  6156. value.setProperty("columnSum", engine->newFunction(SaltTable_columnSum));
  6157. value.setProperty("data", engine->newFunction(SaltTable_data));
  6158. value.setProperty("model", engine->newFunction(SaltTable_model));
  6159. value.setProperty("setData", engine->newFunction(SaltTable_setData));
  6160. value.setProperty("clear", engine->newFunction(SaltTable_clear));
  6161. value.setProperty("removeRow", engine->newFunction(SaltTable_removeRow));
  6162. value.setProperty("findData", engine->newFunction(SaltTable_findData));
  6163. }
  6164. @ The |SqlComboBox| is another class that is not constructed from scripts but is
  6165. useful to access from them. A property is added to obtain the current user data
  6166. from the widget.
  6167. @<Function prototypes for scripting@>=
  6168. void setSqlComboBoxProperties(QScriptValue value, QScriptEngine *engine);
  6169. void setQComboBoxProperties(QScriptValue value, QScriptEngine *engine);
  6170. QScriptValue QComboBox_currentData(QScriptContext *context,
  6171. QScriptEngine *engine);
  6172. QScriptValue QComboBox_addItem(QScriptContext *context, QScriptEngine *engine);
  6173. QScriptValue QComboBox_setModel(QScriptContext *context, QScriptEngine *engine);
  6174. QScriptValue QComboBox_findText(QScriptContext *context, QScriptEngine *engine);
  6175. QScriptValue QComboBox_findData(QScriptContext *context, QScriptEngine *engine);
  6176. @ These functions should seem familiar by now.
  6177. @<Functions for scripting@>=
  6178. void setSqlComboBoxProperties(QScriptValue value, QScriptEngine *engine)
  6179. {
  6180. setQComboBoxProperties(value, engine);
  6181. }
  6182. void setQComboBoxProperties(QScriptValue value, QScriptEngine *engine)
  6183. {
  6184. setQWidgetProperties(value, engine);
  6185. value.setProperty("currentData",
  6186. engine->newFunction(QComboBox_currentData));
  6187. value.setProperty("addItem", engine->newFunction(QComboBox_addItem));
  6188. value.setProperty("setModel", engine->newFunction(QComboBox_setModel));
  6189. value.setProperty("findText", engine->newFunction(QComboBox_findText));
  6190. value.setProperty("findData", engine->newFunction(QComboBox_findData));
  6191. }
  6192. QScriptValue QComboBox_currentData(QScriptContext *context,
  6193. QScriptEngine *engine)
  6194. {
  6195. QComboBox *self = getself<QComboBox *>(context);
  6196. return QScriptValue(engine,
  6197. self->itemData(self->currentIndex()).toString());
  6198. }
  6199. QScriptValue QComboBox_addItem(QScriptContext *context, QScriptEngine *)
  6200. {
  6201. QComboBox *self = getself<QComboBox *>(context);
  6202. self->addItem(argument<QString>(0, context));
  6203. return QScriptValue();
  6204. }
  6205. QScriptValue QComboBox_setModel(QScriptContext *context, QScriptEngine *)
  6206. {
  6207. QComboBox *self = getself<QComboBox *>(context);
  6208. self->setModel(argument<QAbstractItemModel *>(0, context));
  6209. return QScriptValue();
  6210. }
  6211. QScriptValue QComboBox_findText(QScriptContext *context, QScriptEngine *engine)
  6212. {
  6213. QComboBox *self = getself<QComboBox *>(context);
  6214. return QScriptValue(engine, self->findText(argument<QString>(0, context)));
  6215. }
  6216. QScriptValue QComboBox_findData(QScriptContext *context, QScriptEngine *engine)
  6217. {
  6218. QComboBox *self = getself<QComboBox *>(context);
  6219. return QScriptValue(engine, self->findData(argument<QVariant>(0, context)));
  6220. }
  6221. @i abouttypica.w
  6222. @** A representation of temperature measurements.
  6223. \noindent Most of the information in a roast log will be temperature
  6224. measurements. These measurements are made of two components: the measured
  6225. temperature and the time at which that measurement was taken.
  6226. Measurement times are represented as instances of |QTime|.
  6227. @i units.w
  6228. @ We will require the |units.h| header.
  6229. @<Header files to include@>=
  6230. #include "units.h"
  6231. @i measurement.w
  6232. @** The Main Measurement Pipeline.
  6233. \noindent Measurements are sent through \pn{} in a way similar to liquid moving
  6234. through a series of connected pipes. \pn{} is not something that you just dump
  6235. measurements on. It'@q'@>s not a big truck\nfnote{Senator Ted Stevens (R-Alaska) on
  6236. network neutrality, June 28, 2006\par
  6237. \hbox{\indent\pdfURL{http://media.publicknowledge.org/stevens-on-nn.mp3}%
  6238. {http://media.publicknowledge.org/stevens-on-nn.mp3}}}. In most cases the
  6239. connections between classes (represented by arrows in Figure \secno) are made
  6240. with Qt'@q'@>s signals and slots mechanism\nfnote{Qt 4.4.3: Signals and Slots\par
  6241. \hbox{\indent\pdfURL{http://doc.trolltech.com/4.4/signalsandslots.html}%
  6242. {http://doc.trolltech.com/4.4/signalsandslots.html}}}, but
  6243. these
  6244. connections can
  6245. also be made through direct function calls as is the case with the connection
  6246. between |ZoomLog| and |MeasurementModel|.
  6247. \medskip
  6248. \includegraphics{pipes}
  6249. \smallskip
  6250. \centerline{Figure \secno: Example Flow of Measurement objects in \pn}
  6251. \medskip
  6252. Please note that Figure \secno~is representative of a typical configuration. Now
  6253. that the flow of measurements through \pn{} is determined by a script specified
  6254. by the user, whatever pipeline is needed can be specified at run time.
  6255. @* The DAQ class.
  6256. \noindent The |DAQ| class represents a piece of hardware that allows the
  6257. computer to read measurements from one or more thermocouples. Presently this
  6258. class is only handles continuous sampling on devices from National Instruments.
  6259. It should be simple to modify this class to handle similar devices from other
  6260. vendors.
  6261. Each device is represented by a single instance of this class. Multiple channels
  6262. can be used on a device if the device supports it.
  6263. Two enumerations are declared in this class to be used as arguments to
  6264. |newChannel()|. The first is used to set the measurement unit for the channel.
  6265. As the measurements themselves do not carry this information, it is important to
  6266. keep track of this information. The values come from {\tt nidaqmxbase.h} and can
  6267. be used to select among Fahrenheit, Celsius, Kelvin, and Rankine. The
  6268. second enumeration, |ThermocoupleType|, should be used to specify the type of
  6269. thermocouple connected to the device. If this does not match reality, the
  6270. measurements will not be correct. The meanings of the values should be obvious
  6271. from the names.
  6272. \danger When this class was originally written the method of thread handling
  6273. used was considered appropriate. New functionality in |QThread| has made this
  6274. no longer the case. This class is currently planned for depreciation once a
  6275. replacement class hierarchy more suited to multiple hardware types is available
  6276. however if this is not ready soon it may be beneficial to rewrite this class to
  6277. conform to current best practices.\endanger
  6278. @<Class declarations@>=
  6279. class Channel;
  6280. class DAQImplementation;@/
  6281. class DAQ : public QObject@;@/
  6282. {@t\1@>@/
  6283. Q_OBJECT@/
  6284. Q_ENUMS(ThermocoupleType)@;@/
  6285. DAQImplementation *imp;@/
  6286. @t\4@>private slots@t\kern-3pt@>:@/
  6287. void threadFinished();
  6288. public:@;
  6289. DAQ(QString device, const QString &driver = QString("nidaqmxbase"));
  6290. ~DAQ();@/
  6291. Channel* newChannel(int units, int thermocouple);@/
  6292. @[Q_INVOKABLE@,@, void@]@, setClockRate(double Hz);@t\2\2@>@/
  6293. @[Q_INVOKABLE@,@, void@]@, start();@t\2\2@>@/
  6294. @[Q_INVOKABLE@,@, void@]@, stop();@t\2\2@>@/
  6295. enum ThermocoupleType@/
  6296. {
  6297. @!TypeJ = 10072,
  6298. @!TypeK = 10073,
  6299. @!TypeN = 10077,
  6300. @!TypeR = 10082,
  6301. @!TypeS = 10085,
  6302. @!TypeT = 10086,
  6303. @!TypeB = 10047,
  6304. @!TypeE = 10055
  6305. };@t\2@>@/
  6306. }@t\kern-3pt@>;
  6307. @ The |DAQ| class has as a private member an instance of a class called
  6308. |DAQImplementation|. The two classes together create and run a new thread of
  6309. execution. This thread spends most of its time blocking while waiting for a new
  6310. measurement to become available. When a new measurement is available, that
  6311. measurement is passed to the appropriate channel which in turn passes it to any
  6312. interested object.
  6313. @<Class declarations@>=
  6314. class DAQImplementation : public QThread@;@/
  6315. {@;
  6316. Q_OBJECT@;@/
  6317. public:@;
  6318. DAQImplementation(const QString &driverinfo);
  6319. ~DAQImplementation();
  6320. void run();
  6321. void measure();
  6322. @<Library function pointers@>@;
  6323. @<DAQImplementation member data@>@;
  6324. }@+@t\kern-3pt@>;
  6325. @ In order to solve some minor problems, NI-DAQmxBase is no longer linked at
  6326. compile time. Rather, this is now linked at runtime through a |QLibrary| object.
  6327. In order to use functions from this library, these functions must be stored in
  6328. function pointers. Fortunately, all of these functions can be expressed with the
  6329. same pointer type. Unfortunately, this way of doing things offers very little
  6330. debugging information in the event that something is not quite right.
  6331. @<Library function pointers@>=
  6332. typedef int (*daqfp)(...);
  6333. daqfp read;
  6334. daqfp errorInfo;
  6335. daqfp startTask;
  6336. daqfp createTask;
  6337. daqfp createChannel;
  6338. daqfp setClock;
  6339. daqfp stopTask;
  6340. daqfp clearTask;
  6341. daqfp resetDevice;
  6342. daqfp waitForMeasurement;
  6343. @ |DAQImplementation| also maintains information about the device and the
  6344. channels the measurements are sent to.
  6345. @<DAQImplementation member data@>=
  6346. bool useBase;
  6347. QString device;
  6348. QVector<Channel*> channelMap;
  6349. unsigned int handle;@/
  6350. int error;
  6351. int channels;
  6352. bool ready;
  6353. QLibrary driver;
  6354. QVector<Units::Unit> unitMap;
  6355. @ Most of the interesting work associated with the |DAQ| class is handled in
  6356. the |measure()| method of |DAQImplementation|. This function will block until a
  6357. measurement is available. Once |buffer| is filled by |DAQmxBaseReadAnalogF64()|
  6358. that function returns and new |Measurement| objects are created based on the
  6359. information in the buffer. These measurements are sent to |Channel| objects
  6360. tracked by |channelMap|.
  6361. Up until version 1.0.7 there was a bug in this code that would prevent the code
  6362. from working when more than one channel is requested. This has been corrected.
  6363. With version 1.0.9, time measurement is moved out of the loop, reducing the
  6364. number of calls in cases of more than 1 measurement and ensuring that all
  6365. simultaneously obtained measurements have the same time stamp.
  6366. @<DAQ Implementation@>=
  6367. void DAQImplementation::measure()@t\2@>@/
  6368. @t\4@>{@/
  6369. int samplesRead = 0;
  6370. double buffer[channels];
  6371. error = read((unsigned int)(handle), (signed long)(1), (double)(10.0),@|
  6372. (unsigned long)(0), buffer, (unsigned long)(channels),@|
  6373. &samplesRead, (signed long)(0));@/
  6374. if(error)@/
  6375. @t\1@>{@/
  6376. ready = false;@t\2@>@/
  6377. }
  6378. else@/
  6379. {
  6380. if(samplesRead)@/
  6381. {
  6382. QTime time = QTime::currentTime();@/
  6383. for(int i = 0; i < samplesRead; i++)@/
  6384. {
  6385. for(int j = 0; j < channels; j++)@/
  6386. {
  6387. double measuredValue = buffer[j+(i*channels)];
  6388. Measurement measure(measuredValue, time,
  6389. unitMap[j]);
  6390. channelMap[@,j]->input(measure);
  6391. }
  6392. }
  6393. }
  6394. }
  6395. @t\4@>}
  6396. @ It was noted that |DAQmxBaseReadAnalogF64()| blocks until it is able to fill
  6397. the |buffer| passed to it. To prevent this behavior from having adverse effects
  6398. on the rest of the program, measure is called from a loop running in its own
  6399. thread of execution. When the thread is started, it begins its execution from
  6400. the |run()| method of |DAQImplementation| which overrides the |run()| method of
  6401. |QThread|. Here the priority of the thread is set in an attempt to cut down on
  6402. the variation in time between recorded measurements.
  6403. The while loop is controlled by |ready| which is set to |false| when there is an
  6404. error in collecting a measurement or when the user wants to exit the program. It
  6405. could also be set to |false| when the |DAQ| is reconfigured.
  6406. @<DAQ Implementation@>=
  6407. void DAQImplementation::run()
  6408. {
  6409. setPriority(QThread::TimeCriticalPriority);
  6410. while(ready)
  6411. {
  6412. measure();
  6413. }
  6414. }
  6415. @ When this loop exits, |DAQImplementation| emits a finished signal to indicate
  6416. that the thread is no longer running. This could be due to perfectly normal
  6417. conditions, but there could also be a problem that the user must be informed of.
  6418. That signal is connected to a function that checks for error conditions and
  6419. reports them if needed.
  6420. @<DAQ Implementation@>=
  6421. void DAQ::threadFinished()
  6422. {
  6423. if(imp->error)
  6424. {
  6425. @<Display DAQ Error@>@;
  6426. }
  6427. }
  6428. @ Errors are displayed with a |QMessageBox|. NIDAQmxBase provides the message
  6429. strings for these errors, but this should probably change as these error strings
  6430. are generally completely unrelated to what the problem really is. For example,
  6431. ``Error: -1'' usually means that the device is not plugged in.
  6432. ``Error: -200170'' usually means that \pn{} or another program using the device
  6433. did not exit cleanly. A table of replacement warning messages should be added to
  6434. this program.
  6435. \bigskip
  6436. \settabs 5 \columns
  6437. \+Error Code & NIDAQmxBase Text & & Likely Cause\cr
  6438. \+\hrulefill & \hrulefill & \hrulefill & \hrulefill & \hrulefill & \hrulefill\cr
  6439. \+ -1 & Not implemented for this device & & The device is not plugged in.\cr
  6440. \+ & type. & \cr
  6441. \+ -200170 & Physical channel specified & & The program did not exit cleanly\cr
  6442. \+ & does not exist on this device. & & or another program is using the\cr
  6443. \+ & Refer to the documentation for & & device.\cr
  6444. \+ & channels available on this device.\cr
  6445. \+ -1073807194 & {\it{(No text)}} & & The device has been unplugged.\cr
  6446. \medskip
  6447. \centerline{Table \secno: Error codes, text, and what they really mean.}
  6448. \bigskip
  6449. There are two calls to |DAQmxBaseGetExtendedErrorInfo()| to obtain the error
  6450. messages. The first is used just to obtain the length of the error string. That
  6451. length is then used to allocate space for the error message. The second call
  6452. fills that string. This isn'@q'@>t allowed by ISO \CPLUSPLUS/\nfnote{%
  6453. \CPLUSPLUS/Dynamic Arrays (Crowl and Austern, May 16, 2008)\par
  6454. \hbox{\indent\pdfURL{%
  6455. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2648.html}{%
  6456. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2648.html}}} but it
  6457. works with gcc. If this is a problem, the first call can be
  6458. removed and the buffer can be set large enough to fit the largest error message
  6459. that will be produced. Heap allocation could be used, but then we need to
  6460. remember to free the memory allocated to the string. Alternately, we can get rid
  6461. of \CEE/ style strings and write our own error messages. This would be the
  6462. preferred correction.
  6463. @<Display DAQ Error@>=
  6464. imp->ready = false;
  6465. QMessageBox warning;
  6466. warning.setStandardButtons(QMessageBox::Cancel);
  6467. warning.setIcon(QMessageBox::Warning);
  6468. warning.setText(QString(tr("Error: %1")).arg(imp->error));
  6469. unsigned long bytes = imp->errorInfo(NULL, 0);
  6470. char string[bytes];
  6471. imp->errorInfo(string, bytes);
  6472. warning.setInformativeText(QString(string));
  6473. warning.setWindowTitle(QString(PROGRAM_NAME));
  6474. warning.exec();
  6475. @ Starting the thread is almost as simple as ending it. The hardware is
  6476. instructed to begin taking measurements. If there is an error, this is handled.
  6477. Otherwise, the finished signal from |DAQImplementation| is connected to
  6478. |threadFinished()| and the new thread is started. The call to |imp->start()|
  6479. starts a new thread and passes control of that thread to |imp->run()|. The main
  6480. thread of execution returns without waiting for the new thread to do anything.
  6481. The call to |DAQmxBaseStartTask()| requires some time before the first
  6482. measurement is available. This is one of the reasons we start gathering
  6483. measurements before we really need them and continue to collect them until it is
  6484. time to exit the program.
  6485. @<DAQ Implementation@>=
  6486. void DAQ::start()
  6487. {
  6488. if(imp->ready)
  6489. {
  6490. imp->error = imp->startTask(imp->handle);
  6491. if(imp->error)
  6492. {
  6493. @<Display DAQ Error@>@;
  6494. }
  6495. else
  6496. {
  6497. connect(imp, SIGNAL(finished()), this, SLOT(threadFinished()));
  6498. imp->start();
  6499. }
  6500. }
  6501. }
  6502. void DAQ::stop()
  6503. {
  6504. if(imp->useBase)
  6505. {
  6506. imp->ready = @[false@];
  6507. imp->wait(ULONG_MAX);
  6508. imp->stopTask(imp->handle);
  6509. }
  6510. else
  6511. {
  6512. imp->ready = @[false@];
  6513. imp->error = imp->stopTask(imp->handle);
  6514. if(imp->error)
  6515. {
  6516. @<Display DAQ Error@>@;
  6517. }
  6518. imp->error = imp->clearTask(imp->handle);
  6519. if(imp->error)
  6520. {
  6521. @<Display DAQ Error@>@;
  6522. }
  6523. }
  6524. }
  6525. @ Setting up the DAQ begins by constructing a new |DAQ| object. The constructor
  6526. takes as its argument a string indicating the name of the device and will
  6527. typically be something like |"Dev1"|. This creates a new task which is required
  6528. for the setup that follows once a new |DAQ| is created.
  6529. @<DAQ Implementation@>=
  6530. DAQ::DAQ(QString device, const QString &driver) : imp(new DAQImplementation(driver))@/
  6531. @t\4\4@>{@/
  6532. imp->device = device;
  6533. imp->error = imp->createTask(device.toAscii().data(), &(imp->handle));
  6534. if(imp->error)@/
  6535. {
  6536. @<Display DAQ Error@>@;
  6537. }
  6538. else@/
  6539. @t\1@>{@/
  6540. imp->ready = true;@t\2@>@/
  6541. }@/
  6542. @t\4\4@>}
  6543. @ Once the |DAQ| is created, one or more channels can be added to that |DAQ|.
  6544. All |Channel| objects are created by the |DAQ| class and are managed by the
  6545. |DAQ| class. When a new channel is created, a pointer is passed back allowing
  6546. other classes to connect to the channel. Measurements cannot be read from the
  6547. |DAQ| directly. They must at some point pass through a channel.
  6548. @<DAQ Implementation@>=
  6549. Channel* DAQ::newChannel(int units, int thermocouple)
  6550. {
  6551. Channel *retval = new Channel();
  6552. imp->channelMap[imp->channels] = retval;
  6553. imp->unitMap[imp->channels] = (Units::Unit)units;
  6554. imp->channels++;
  6555. if(imp->ready)
  6556. {
  6557. if(imp->useBase)
  6558. {
  6559. imp->error = imp->createChannel(imp->handle,
  6560. QString("%1/ai%2").arg(imp->device).
  6561. arg(imp->channels - 1).
  6562. toAscii().data(),
  6563. "", (double)(-1.0), (double)(100.0),
  6564. (signed long)(units),
  6565. (signed long)(thermocouple),
  6566. (signed long)(10200), (double)(0),
  6567. "");
  6568. }
  6569. else
  6570. {
  6571. imp->error = imp->createChannel(imp->handle,
  6572. QString("%1/ai%2").arg(imp->device).
  6573. arg(imp->channels - 1).
  6574. toAscii().data(),
  6575. "", (double)(50.0), (double)(500.0),
  6576. (signed long)(units),
  6577. (signed long)(thermocouple),
  6578. (signed long)(10200), (double)(0),
  6579. "");
  6580. }
  6581. if(imp->error)
  6582. {
  6583. @<Display DAQ Error@>@;
  6584. }
  6585. }
  6586. return retval;
  6587. }
  6588. @ Once the channels are created, it is necessary to set the clock rate of the
  6589. DAQ. The clock rate chosen must be supported by the hardware. The clock rates
  6590. supported by the hardware may be altered by the number of channels in use.
  6591. The amount of time between measurements may vary slightly. A test configuration
  6592. at Wilson's Coffee \char'046~Tea used a 4Hz clock rate and provides measurements
  6593. every 251$\pm$1ms with 80\% of measurements spaced 251ms apart.
  6594. @<DAQ Implementation@>=
  6595. void DAQ::setClockRate(double Hz)
  6596. {
  6597. if(imp->ready)
  6598. {
  6599. imp->error = imp->setClock(imp->handle, "OnboardClock", Hz,
  6600. (signed long)(10280), (signed long)(10123),
  6601. (unsigned long long)(1));
  6602. if(imp->error)
  6603. {
  6604. @<Display DAQ Error@>@;
  6605. }
  6606. }
  6607. }
  6608. @ Before the program exits, the |DAQ| should be deleted. The destructor
  6609. instructs the measurement thread to stop, waits for it to finish, and resets the
  6610. device. If this is not done, an error would be issued the next time a program
  6611. attempted to use the device.
  6612. @<DAQ Implementation@>=
  6613. DAQ::~DAQ()@/
  6614. {
  6615. if(imp->useBase)
  6616. {
  6617. imp->resetDevice(imp->device.toAscii().data());
  6618. imp->clearTask(imp->handle);
  6619. }
  6620. else
  6621. {
  6622. if(imp->ready)
  6623. {
  6624. imp->ready = @[false@];
  6625. imp->wait(ULONG_MAX);
  6626. imp->stopTask(imp->handle);
  6627. imp->resetDevice(imp->device.toAscii().data());
  6628. imp->clearTask(imp->handle);
  6629. }
  6630. }
  6631. delete imp;
  6632. }
  6633. @ This just leaves the constructor and destructor for |DAQImplementation|. The
  6634. way the program is currently written, the number of channels available on the
  6635. |DAQ| is limited to 4. If a known larger number is required, the value here can
  6636. simply be set larger, however the best long term solution would be to modify
  6637. |newChannel()| to resize |channelMap| as more channels are added.
  6638. The constructor handles loading NI-DAQmxBase and preparing function pointers for
  6639. the symbols used in \pn{}.
  6640. @<DAQ Implementation@>=
  6641. DAQImplementation::DAQImplementation(const QString &driverinfo)
  6642. : QThread(NULL), channelMap(4), handle(0), error(0), channels(0), ready(false),
  6643. unitMap(4)@/
  6644. {
  6645. if(driverinfo == "nidaqmxbase")
  6646. {
  6647. useBase = true;
  6648. }
  6649. else
  6650. {
  6651. useBase = false;
  6652. }
  6653. if(useBase)
  6654. {
  6655. driver.setFileName("nidaqmxbase.framework/nidaqmxbase");
  6656. if(!driver.load())
  6657. {
  6658. driver.setFileName("nidaqmxbase");
  6659. if(!driver.load())
  6660. {
  6661. QMessageBox::critical(NULL, tr("Typica: Driver not found"),
  6662. tr("Failed to find nidaqmxbase. Please install it."));
  6663. QApplication::quit();
  6664. }
  6665. }
  6666. }
  6667. else
  6668. {
  6669. driver.setFileName("nicaiu");
  6670. if(!driver.load())
  6671. {
  6672. QMessageBox::critical(NULL, tr("Typica: Driver not found"),
  6673. tr("Failed to find nidaqmx. Please install it."));
  6674. QApplication::quit();
  6675. }
  6676. }
  6677. if(useBase)
  6678. {
  6679. if((createTask = (daqfp) driver.resolve("DAQmxBaseCreateTask")) == 0 || @|
  6680. (startTask = (daqfp) driver.resolve("DAQmxBaseStartTask")) == 0 || @|
  6681. (stopTask = (daqfp) driver.resolve("DAQmxBaseStopTask")) == 0 || @|
  6682. (clearTask = (daqfp) driver.resolve("DAQmxBaseClearTask")) == 0 || @|
  6683. (createChannel = (daqfp) driver.resolve("DAQmxBaseCreateAIThrmcplChan"))
  6684. == 0 || @|
  6685. (setClock = (daqfp) driver.resolve("DAQmxBaseCfgSampClkTiming")) ==
  6686. 0 || @|
  6687. (read = (daqfp) driver.resolve("DAQmxBaseReadAnalogF64")) == 0 || @|
  6688. (errorInfo = (daqfp) driver.resolve("DAQmxBaseGetExtendedErrorInfo")) ==
  6689. 0 || @|
  6690. (resetDevice = (daqfp) driver.resolve("DAQmxBaseResetDevice")) == 0)@/
  6691. {
  6692. waitForMeasurement = NULL;
  6693. QMessageBox::critical(NULL, tr("Typica: Link error"),
  6694. tr("Failed to link a required symbol in NI-DAQmxBase."));
  6695. QApplication::quit();
  6696. }
  6697. }
  6698. else
  6699. {
  6700. if((createTask = (daqfp)driver.resolve("DAQmxCreateTask")) == 0 || @|
  6701. (startTask = (daqfp)driver.resolve("DAQmxStartTask")) == 0 || @|
  6702. (stopTask = (daqfp)driver.resolve("DAQmxStopTask")) == 0 || @|
  6703. (clearTask = (daqfp)driver.resolve("DAQmxClearTask")) == 0 || @|
  6704. (createChannel = (daqfp)driver.resolve("DAQmxCreateAIThrmcplChan"))
  6705. == 0 || @|
  6706. (setClock = (daqfp)driver.resolve("DAQmxCfgSampClkTiming")) == 0 || @|
  6707. (read = (daqfp)driver.resolve("DAQmxReadAnalogF64")) == 0 || @|
  6708. (errorInfo = (daqfp)driver.resolve("DAQmxGetExtendedErrorInfo")) ==
  6709. 0 || @|
  6710. (resetDevice = (daqfp)driver.resolve("DAQmxResetDevice")) == 0 ||
  6711. (waitForMeasurement = (daqfp)driver.resolve("DAQmxWaitUntilTaskDone")) == 0)
  6712. {
  6713. QMessageBox::critical(NULL, tr("Typica: Link error"),
  6714. tr("Failed to link a required symbol in NI-DAQmx."));
  6715. QApplication::quit();
  6716. }
  6717. }
  6718. }
  6719. DAQImplementation::~DAQImplementation()
  6720. {
  6721. driver.unload();
  6722. }
  6723. @ When exposing the |DAQ| class to the scripting engine, we need to provide a
  6724. constructor that can be called from a script and we need a way to call
  6725. |DAQ::newChannel()|. Other methods that are useful when called from a script are
  6726. made available automatically with the |Q_INVOKABLE| macro, however this does not
  6727. work for methods such as |newChannel()| which return a pointer to a |Channel|
  6728. object.
  6729. @<Function prototypes for scripting@>=
  6730. QScriptValue constructDAQ(QScriptContext *context, QScriptEngine *engine);
  6731. QScriptValue DAQ_newChannel(QScriptContext *context, QScriptEngine *engine);
  6732. void setDAQProperties(QScriptValue value, QScriptEngine *engine);
  6733. @ These functions and the values from |Units::Unit| must be made available to
  6734. the host environment. The latter because this was widely used in configurations
  6735. before this enumeration was removed from the |DAQ| class. As these properties
  6736. must be available without an instance, the properties must be added here.
  6737. @<Set up the scripting engine@>=
  6738. constructor = engine->newFunction(constructDAQ);
  6739. value = engine->newQMetaObject(&DAQ::staticMetaObject, constructor);
  6740. value.setProperty("Fahrenheit", Units::Fahrenheit);
  6741. value.setProperty("Celsius", Units::Celsius);
  6742. value.setProperty("Kelvin", Units::Kelvin);
  6743. value.setProperty("Rankine", Units::Rankine);
  6744. engine->globalObject().setProperty("DAQ", value);
  6745. @ When creating a new |DAQ|, we make sure that it is owned by the script engine.
  6746. This is necessary to ensure that the destructor is called before \pn{} exits.
  6747. Just as the constructor requires an argument that specifies the device name, the
  6748. constructor available from a script also requires this argument.
  6749. @<Functions for scripting@>=
  6750. QScriptValue constructDAQ(QScriptContext *context, QScriptEngine *engine)
  6751. {
  6752. QScriptValue object;
  6753. if(context->argumentCount() == 1)
  6754. {
  6755. object = engine->newQObject(new DAQ(argument<QString>(0, context)),
  6756. QScriptEngine::ScriptOwnership);
  6757. setDAQProperties(object, engine);
  6758. }
  6759. else if(context->argumentCount() == 2)
  6760. {
  6761. object = engine->newQObject(new DAQ(argument<QString>(0, context),
  6762. argument<QString>(1, context)),
  6763. QScriptEngine::ScriptOwnership);
  6764. setDAQProperties(object, engine);
  6765. }
  6766. else
  6767. {
  6768. context->throwError("Incorrect number of arguments passed to DAQ"@|
  6769. "constructor. The DAQ constructor takes one"@|
  6770. "string as an argument specifying a device name."@|
  6771. "Example: Dev1");
  6772. }
  6773. return object;
  6774. }
  6775. @ As |DAQ| inherits |QObject|, we add the |newChannel()| property after adding
  6776. any |QObject| properties.
  6777. @<Functions for scripting@>=
  6778. void setDAQProperties(QScriptValue value, QScriptEngine *engine)
  6779. {
  6780. setQObjectProperties(value, engine);
  6781. value.setProperty("newChannel", engine->newFunction(DAQ_newChannel));
  6782. }
  6783. @ The |newChannel()| method method also requires that two arguments are provided
  6784. by the script.
  6785. @<Functions for scripting@>=
  6786. QScriptValue DAQ_newChannel(QScriptContext *context, QScriptEngine *engine)
  6787. {
  6788. DAQ *self = getself<@[DAQ *@]>(context);
  6789. QScriptValue object;
  6790. if(self)
  6791. {
  6792. object =
  6793. engine->newQObject(self->newChannel(argument<int>(0, context),@|
  6794. argument<int>(1, context)));
  6795. setChannelProperties(object, engine);
  6796. }
  6797. return object;
  6798. }
  6799. @ Sometimes it can be useful to test other parts of the program (for example,
  6800. when developing new scripts) when the DAQ hardware is not available. In these
  6801. cases, it is possible to temporarily use the |FakeDAQ| class. This class mimics
  6802. the |DAQ| class, but just makes up the measurements sent to the rest of the
  6803. program.
  6804. @<Class declarations@>=
  6805. class FakeDAQImplementation : public QThread@/
  6806. {@/
  6807. Q_OBJECT@;
  6808. public:@/
  6809. FakeDAQImplementation();
  6810. ~FakeDAQImplementation();
  6811. void run();
  6812. void measure();
  6813. QVector<Channel *> channelMap;
  6814. int channels;
  6815. bool ready;
  6816. double clockRate;
  6817. };@/
  6818. class FakeDAQ : public QObject@/
  6819. {@/
  6820. Q_OBJECT@;
  6821. FakeDAQImplementation *imp;
  6822. public:@/
  6823. FakeDAQ(QString device);
  6824. ~FakeDAQ();
  6825. Channel *newChannel(int units, int thermocouple);@/
  6826. @[Q_INVOKABLE@,@, void@]@, setClockRate(double Hz);@t\2\2@>@/
  6827. @[Q_INVOKABLE@,@, void@]@, start();@t\2\2@>@/
  6828. };
  6829. @ Just as in the |DAQ| class, most of the interesting stuff happens in
  6830. |measure()|. For each invokation of the method, we sleep for some amount of time
  6831. based on the clock rate then create a |Measurement| object at random for each
  6832. |Channel| that has been created.
  6833. @<FakeDAQ Implementation@>=
  6834. void FakeDAQImplementation::measure()
  6835. {
  6836. msleep((int)(1000/clockRate));
  6837. QTime time = QTime::currentTime();
  6838. for(int i = 0; i < channels; i++)
  6839. {
  6840. Measurement measure(qrand() % 500, time);
  6841. channelMap[i]->input(measure);
  6842. }
  6843. }
  6844. @ To call |measure|, we need to flesh out the rest of |FakeDAQImplementation|.
  6845. @<FakeDAQ Implementation@>=
  6846. void FakeDAQImplementation::run()
  6847. {
  6848. setPriority(QThread::TimeCriticalPriority);
  6849. while(ready)
  6850. {
  6851. measure();
  6852. }
  6853. }
  6854. FakeDAQImplementation::FakeDAQImplementation() : QThread(NULL), channelMap(4),
  6855. channels(0), ready(false), clockRate(1)@/
  6856. {
  6857. /* Nothing has to be done here. */
  6858. }
  6859. FakeDAQImplementation::~FakeDAQImplementation()
  6860. {
  6861. /* Nothing has to be done here. */
  6862. }
  6863. @ Next we need an implementation for the |FakeDAQ| class. This is simplified by
  6864. the fact that we are just using a random number generator to generate
  6865. measurements rather than special hardware for obtaining measurements.
  6866. @<FakeDAQ Implementation@>=
  6867. void FakeDAQ::start()
  6868. {
  6869. if(imp->ready)
  6870. {
  6871. imp->start();
  6872. }
  6873. }@#
  6874. FakeDAQ::FakeDAQ(QString) : imp(new FakeDAQImplementation())@t\2\2@>@/
  6875. {@t\1@>@/
  6876. imp->ready = true;@t\2@>@/
  6877. }@#
  6878. Channel* FakeDAQ::newChannel(int, int)
  6879. {
  6880. Channel *retval;
  6881. if(imp->ready)
  6882. {
  6883. retval = new Channel();
  6884. imp->channelMap[imp->channels] = retval;
  6885. imp->channels++;
  6886. }
  6887. else
  6888. {
  6889. return NULL;
  6890. }
  6891. return retval;
  6892. }
  6893. void FakeDAQ::setClockRate(double Hz)
  6894. {
  6895. if(imp->ready)
  6896. {
  6897. imp->clockRate = Hz;
  6898. }
  6899. }@#
  6900. FakeDAQ::~FakeDAQ()@t\2\2@>@/
  6901. {@t\1@>@/
  6902. imp->ready = false;
  6903. imp->wait(ULONG_MAX);
  6904. delete imp;@t\2@>@/
  6905. }
  6906. @ As the entire purpose of the |FakeDAQ| class is for testing purposes from
  6907. within the scripting engine, we need to make it available to the scripting
  6908. engine. This is done in a manner very similar to how the |DAQ| class is handled.
  6909. @<Function prototypes for scripting@>=
  6910. QScriptValue constructFakeDAQ(QScriptContext *context, QScriptEngine *engine);
  6911. QScriptValue FakeDAQ_newChannel(QScriptContext *context, QScriptEngine *engine);
  6912. void setFakeDAQProperties(QScriptValue value, QScriptEngine *engine);
  6913. @ The scripting engine is informed of the constructor.
  6914. @<Set up the scripting engine@>=
  6915. constructor = engine->newFunction(constructFakeDAQ);
  6916. value = engine->newQMetaObject(&FakeDAQ::staticMetaObject, constructor);
  6917. engine->globalObject().setProperty("FakeDAQ", value);
  6918. @ The constructor sets a property to allow calling |newChannel()| on a |FakeDAQ|
  6919. created from a script.
  6920. @<Functions for scripting@>=
  6921. QScriptValue constructFakeDAQ(QScriptContext *context,
  6922. QScriptEngine *engine)
  6923. {
  6924. QScriptValue object;
  6925. if(context->argumentCount() == 1)
  6926. {
  6927. object =
  6928. engine->newQObject(new FakeDAQ(argument<QString>(0, context)),
  6929. QScriptEngine::ScriptOwnership);
  6930. setFakeDAQProperties(object, engine);
  6931. }
  6932. else
  6933. {
  6934. context->throwError("Incorrect number of arguments passed to DAQ"@|
  6935. "constructor. The DAQ constructor takes one"@|
  6936. "string as an argument specifying a device name."@|
  6937. "Example: Dev1");
  6938. }
  6939. return object;
  6940. }
  6941. void setFakeDAQProperties(QScriptValue value, QScriptEngine *engine)
  6942. {
  6943. setQObjectProperties(value, engine);
  6944. value.setProperty("newChannel", engine->newFunction(FakeDAQ_newChannel));
  6945. }
  6946. QScriptValue FakeDAQ_newChannel(QScriptContext *context, QScriptEngine *engine)
  6947. {
  6948. FakeDAQ *self = getself<@[FakeDAQ *@]>(context);
  6949. QScriptValue object;
  6950. if(self)
  6951. {
  6952. object =
  6953. engine->newQObject(self->newChannel(argument<int>(0, context),@|
  6954. argument<int>(1, context)));
  6955. setChannelProperties(object, engine);
  6956. }
  6957. return object;
  6958. }
  6959. @* The Channel class.
  6960. \noindent |Channel| is a simple class. It is a subclass of |QObject| so it can
  6961. use Qt'@q'@>s signals and slots mechanism. Any object that is interested in
  6962. measurements from a channel can connect to the |newData| signal the channel
  6963. emits. Any number of objects can make this connection and each will receive a
  6964. copy of the measurement.
  6965. |Channel| objects should only be created by the |DAQ| class.
  6966. @<Class declarations@>=
  6967. class Channel : public QObject@;@/
  6968. {@t\1@>@/
  6969. Q_OBJECT@/
  6970. public:@;
  6971. Channel();
  6972. ~Channel();@/
  6973. @t\4@>public slots@t\kern-3pt@>:@;
  6974. void input(Measurement measurement);@/
  6975. signals:@;
  6976. void newData(Measurement);@t\2@>@/
  6977. };
  6978. @ The implementation of this class is trivial.
  6979. @<Channel Implementation@>=
  6980. Channel::Channel() : QObject(NULL)@/
  6981. {
  6982. /* Nothing has to be done here. */
  6983. }
  6984. Channel::~Channel()
  6985. {
  6986. /* Nothing has to be done here. */
  6987. }
  6988. void Channel::input(Measurement measurement)
  6989. {
  6990. emit newData(measurement);
  6991. }
  6992. @ A function is provided for use when a channel is created by a DAQ from a
  6993. script.
  6994. @<Function prototypes for scripting@>=
  6995. void setChannelProperties(QScriptValue value, QScriptEngine *engine);
  6996. @ The implementation is trivial.
  6997. @<Functions for scripting@>=
  6998. void setChannelProperties(QScriptValue value, QScriptEngine *engine)
  6999. {
  7000. setQObjectProperties(value, engine);
  7001. }
  7002. @* Calibration and Unit Conversion.
  7003. \noindent One of the planned features for \pn{} is support for hardware that
  7004. collects non-temperature measurements. This is frequently handled with analog
  7005. voltage signals which are proportional to some range in a meaningful unit. Some
  7006. hardware also requires calibration in software. In many cases both of these can
  7007. be handled at the same time with a single mapping in the form:
  7008. $$f(x) = L_1 + (x - L_2){U_1 - L_1\over{U_2 - L_2}}$$
  7009. \noindent where $L_1$ is the logical lower bound, $L_2$ is the measured lower
  7010. bound, $U_1$ is the logical upper bound, $U_2$ is the measured upper bound, and
  7011. $x$ is the value we wish to map from the range $\lbrack L_2, U_2 \rbrack$ to
  7012. the range $\lbrack L_1, U_1 \rbrack$.
  7013. Some use cases require a closed range but others require that this constraint
  7014. is loosened to allow extrapolation. Both are provided by this class.
  7015. Starting in \pn{} 1.6 this class has both the |measurement| and the
  7016. |newData| signals. This allows a |LinearCalibrator| to be treated like a
  7017. |Channel| when used with a |DataqSdkDevice|.
  7018. @<Class declarations@>=
  7019. class LinearCalibrator : public QObject@/
  7020. {@/
  7021. @[Q_OBJECT@]@;
  7022. @[Q_PROPERTY(double measuredLower READ measuredLower
  7023. WRITE setMeasuredLower)@]@;
  7024. @[Q_PROPERTY(double measuredUpper READ measuredUpper
  7025. WRITE setMeasuredUpper)@]@;
  7026. @[Q_PROPERTY(double mappedLower READ mappedLower WRITE setMappedLower)@]@;
  7027. @[Q_PROPERTY(double mappedUpper READ mappedUpper WRITE setMappedUpper)@]@;
  7028. @[Q_PROPERTY(bool closedRange READ isClosedRange WRITE setClosedRange)@]@;
  7029. @[Q_PROPERTY(double sensitivity READ sensitivity WRITE setSensitivity)@]@;
  7030. public:@/
  7031. LinearCalibrator(QObject *parent = NULL);
  7032. double measuredLower();
  7033. double measuredUpper();
  7034. double mappedLower();
  7035. double mappedUpper();
  7036. bool isClosedRange();
  7037. double sensitivity();
  7038. @t\4@>@[public slots@t\kern-3pt@>:@]@;
  7039. void setMeasuredLower(double lower);
  7040. void setMeasuredUpper(double upper);
  7041. void setMappedLower(double lower);
  7042. void setMappedUpper(double upper);
  7043. void setClosedRange(bool closed);
  7044. void setSensitivity(double sensitivity);
  7045. Measurement newMeasurement(Measurement measure);
  7046. @t\4@>@[signals:@]@;
  7047. void measurement(Measurement measure);
  7048. void newData(Measurement measure);
  7049. private:@/
  7050. double Lo1;
  7051. double Lo2;
  7052. double Up1;
  7053. double Up2;
  7054. double sensitivitySetting;
  7055. bool clamp;
  7056. };
  7057. @ When the measured range and the mapped range are identical and the range is
  7058. open, we have an identity mapping. This is the default state in a newly
  7059. constructed |LinearCalibrator| which should quickly be changed.
  7060. @<LinearCalibrator Implementation@>=
  7061. LinearCalibrator::LinearCalibrator(QObject *parent) :
  7062. QObject(parent), Lo1(0), Lo2(0), Up1(1), Up2(1), sensitivitySetting(0.0), clamp(false)@/
  7063. {
  7064. connect(this, SIGNAL(measurement(Measurement)), this, SIGNAL(newData(Measurement)));
  7065. }
  7066. @ The functional portion of the class is in the |newMeasurement()| slot. This
  7067. will receive measurements as they come in and emit a |measurement()| signal for
  7068. each with the calibration and unit adjustment performed.
  7069. This method also handles any rounding needed if there has been a call to
  7070. |setSensitivity()|.
  7071. @<LinearCalibrator Implementation@>=
  7072. Measurement LinearCalibrator::newMeasurement(Measurement measure)
  7073. {
  7074. double outval = Lo1 + (measure.temperature() - Lo2) * (Up1 - Lo1)/(Up2 - Lo2);
  7075. if(clamp)
  7076. {
  7077. if(outval < Lo1)
  7078. {
  7079. outval = Lo1;
  7080. }
  7081. else if(outval > Up1)
  7082. {
  7083. outval = Up1;
  7084. }
  7085. }
  7086. if(sensitivitySetting >= 0.05)
  7087. {
  7088. int temp = qRound(outval/sensitivitySetting);
  7089. outval = temp * sensitivitySetting;
  7090. }
  7091. Measurement adjusted(outval, measure.time(), measure.scale());
  7092. emit measurement(adjusted);
  7093. return adjusted;
  7094. }
  7095. @ The rest of the class consists of trivial accessor methods.
  7096. @<LinearCalibrator Implementation@>=
  7097. double LinearCalibrator::measuredLower()
  7098. {
  7099. return Lo2;
  7100. }
  7101. double LinearCalibrator::measuredUpper()
  7102. {
  7103. return Up2;
  7104. }
  7105. double LinearCalibrator::mappedLower()
  7106. {
  7107. return Lo1;
  7108. }
  7109. double LinearCalibrator::mappedUpper()
  7110. {
  7111. return Up1;
  7112. }
  7113. bool LinearCalibrator::isClosedRange()
  7114. {
  7115. return clamp;
  7116. }
  7117. void LinearCalibrator::setMeasuredLower(double lower)
  7118. {
  7119. Lo2 = lower;
  7120. }
  7121. void LinearCalibrator::setMeasuredUpper(double upper)
  7122. {
  7123. Up2 = upper;
  7124. }
  7125. void LinearCalibrator::setMappedLower(double lower)
  7126. {
  7127. Lo1 = lower;
  7128. }
  7129. void LinearCalibrator::setMappedUpper(double upper)
  7130. {
  7131. Up1 = upper;
  7132. }
  7133. void LinearCalibrator::setClosedRange(bool closed)
  7134. {
  7135. clamp = closed;
  7136. }
  7137. void LinearCalibrator::setSensitivity(double sensitivity)
  7138. {
  7139. sensitivitySetting = sensitivity;
  7140. }
  7141. double LinearCalibrator::sensitivity()
  7142. {
  7143. return sensitivitySetting;
  7144. }
  7145. @ Finally, we make this class available to the scripting engine. Two functions
  7146. are required for this.
  7147. @<Function prototypes for scripting@>=
  7148. QScriptValue constructLinearCalibrator(QScriptContext *context,
  7149. QScriptEngine *engine);
  7150. void setLinearCalibratorProperties(QScriptValue value, QScriptEngine *engine);
  7151. @ The scripting engine is informed of the constructor.
  7152. @<Set up the scripting engine@>=
  7153. constructor = engine->newFunction(constructLinearCalibrator);
  7154. value = engine->newQMetaObject(&LinearCalibrator::staticMetaObject,
  7155. constructor);
  7156. engine->globalObject().setProperty("LinearCalibrator", value);
  7157. @ The implementation of these functions is typical for this application.
  7158. @<Functions for scripting@>=
  7159. QScriptValue constructLinearCalibrator(QScriptContext *, QScriptEngine *engine)
  7160. {
  7161. QScriptValue object = engine->newQObject(new LinearCalibrator(NULL));
  7162. setLinearCalibratorProperties(object, engine);
  7163. return object;
  7164. }
  7165. void setLinearCalibratorProperties(QScriptValue value, QScriptEngine *engine)
  7166. {
  7167. setQObjectProperties(value, engine);
  7168. }
  7169. @* Linear Spline Interpolation.
  7170. \noindent While linear interpolation is adequate for many purposes, it fails
  7171. when a nonlinear mapping is required. The primary use case is to calibrate
  7172. multiple coffee roasters so that roast profiles can be shared among coffee
  7173. roasters with sufficiently similar heat transfer characteristics even if
  7174. differences in the measurement hardware result in different measured values.
  7175. By recording measured values at several points known to be equivalent due to
  7176. easily observable physical changes in the coffee it is possible to use linear
  7177. spline interpolation to produce a data series that approximates on one machine
  7178. the measurements that would have been produced at another. Acceptable results
  7179. may be available with surprisingly few data points.
  7180. It was originally suspected that cubic spline interpolation would produce a
  7181. more accurate mapping, but testing with linear spline interpolation produced
  7182. results good enough that more complex mappings were not needed. Cubic spline
  7183. interpolation may still be implemented in the future, but it is a low
  7184. priority.
  7185. @<Class declarations@>=
  7186. class LinearSplineInterpolator : public QObject@/
  7187. {@/
  7188. @[Q_OBJECT@]@;@/
  7189. public:@/
  7190. LinearSplineInterpolator(QObject *parent = NULL);
  7191. @[Q_INVOKABLE@,@, void@]@, add_pair(double source, double destination);@t\2\2@>@/
  7192. @[public slots@]:@/
  7193. Measurement newMeasurement(Measurement measure);
  7194. @[signals@]:@/
  7195. void newData(Measurement measure);
  7196. private:@/
  7197. void make_interpolators();
  7198. QMap<double, double> *pairs;
  7199. QList<LinearCalibrator *> *interpolators;@/
  7200. };
  7201. @ We take advantage of the fact that iterating over a QMap always returns
  7202. entries in key order. When new measurement pairs are specified, the
  7203. interpolators are regenerated. There is significant room for performance
  7204. improvement.
  7205. @<LinearSplineInterpolator Implementation@>=
  7206. void LinearSplineInterpolator::add_pair(double source, double destination)
  7207. {
  7208. pairs->insert(source, destination);
  7209. make_interpolators();
  7210. }
  7211. void LinearSplineInterpolator::make_interpolators()
  7212. {
  7213. if(pairs->size() > 1)
  7214. {
  7215. while(interpolators->size() > 0)
  7216. {
  7217. LinearCalibrator *removed = interpolators->takeFirst();
  7218. removed->deleteLater();
  7219. }
  7220. QMap<double, double>::const_iterator i = pairs->constBegin();
  7221. QMap<double, double>::const_iterator j = i + 1;
  7222. while(j != pairs->constEnd())
  7223. {
  7224. LinearCalibrator *segment = new LinearCalibrator();
  7225. segment->setMeasuredLower(i.key());
  7226. segment->setMappedLower(i.value());
  7227. segment->setMeasuredUpper(j.key());
  7228. segment->setMappedUpper(j.value());
  7229. segment->setClosedRange(false);
  7230. interpolators->append(segment);
  7231. connect(segment, SIGNAL(measurement(Measurement)), this, SIGNAL(newData(Measurement)));
  7232. i++;
  7233. j++;
  7234. }
  7235. }
  7236. }
  7237. LinearSplineInterpolator::LinearSplineInterpolator(QObject *parent) :
  7238. QObject(parent), pairs(new QMap<double, double>),
  7239. interpolators(new QList<LinearCalibrator *>)
  7240. {
  7241. /* Nothing needs to be done here. */
  7242. }
  7243. Measurement LinearSplineInterpolator::newMeasurement(Measurement measure)
  7244. {
  7245. QMap<double, double>::const_iterator i = pairs->constBegin();
  7246. int index = -1;
  7247. while(i != pairs->constEnd())
  7248. {
  7249. if(measure.temperature() <= i.key())
  7250. {
  7251. break;
  7252. }
  7253. i++;
  7254. index++;
  7255. }
  7256. if(index < 0)
  7257. {
  7258. index = 0;
  7259. }
  7260. if(index >= interpolators->size())
  7261. {
  7262. index = interpolators->size() - 1;
  7263. }
  7264. if(interpolators->at(index) != NULL)
  7265. {
  7266. return interpolators->at(index)->newMeasurement(measure);
  7267. }
  7268. return Measurement();
  7269. }
  7270. @ This is exposed to the scripting environment as usual.
  7271. @<Function prototypes for scripting@>=
  7272. QScriptValue constructLinearSplineInterpolator(QScriptContext *context, QScriptEngine *engine);
  7273. void setLinearSplineInterpolatorProperties(QScriptValue value, QScriptEngine *engine);
  7274. @ As usual.
  7275. @<Set up the scripting engine@>=
  7276. constructor = engine->newFunction(constructLinearSplineInterpolator);
  7277. value = engine->newQMetaObject(&LinearSplineInterpolator::staticMetaObject, constructor);
  7278. engine->globalObject().setProperty("LinearSplineInterpolator", value);
  7279. @ And again as usual.
  7280. @<Functions for scripting@>=
  7281. QScriptValue constructLinearSplineInterpolator(QScriptContext *, QScriptEngine *engine)
  7282. {
  7283. QScriptValue object = engine->newQObject(new LinearSplineInterpolator(NULL));
  7284. setLinearSplineInterpolatorProperties(object, engine);
  7285. return object;
  7286. }
  7287. void setLinearSplineInterpolatorProperties(QScriptValue value, QScriptEngine *engine)
  7288. {
  7289. setQObjectProperties(value, engine);
  7290. }
  7291. @* The TemperatureDisplay class.
  7292. Now that measurements have been generated by the |DAQ| and passed to a
  7293. |Channel|, any object that is interested in these measurements can connect to
  7294. the channel and use the measurements it sends out. So far, the time on each
  7295. measurement is the time at which it was collected. Unfortunately, this is often
  7296. not what we want. It is more useful to have the time relative to some other
  7297. point in time such as the start of the batch.
  7298. Until the measurement time is adjusted, the measurements are really only useful
  7299. to classes that do not care about the measurement time. The |TemperatureDisplay|
  7300. class is such a class. It receives measurements and displays the most recently
  7301. measured temperature.
  7302. This is a specialization of |QLCDNumber|.
  7303. @<Class declarations@>=
  7304. class TemperatureDisplay : public QLCDNumber@/
  7305. {@t\1@>@/
  7306. Q_OBJECT@;
  7307. int unit;
  7308. bool r;
  7309. public:@/
  7310. TemperatureDisplay(QWidget *parent = NULL);
  7311. ~TemperatureDisplay();@/
  7312. @t\4@>public slots@t\kern-3pt@>:@/
  7313. void setValue(Measurement temperature);
  7314. void invalidate();
  7315. void setDisplayUnits(Units::Unit scale);
  7316. void setRelativeMode(bool relative);@t\2@>@/
  7317. };
  7318. @ Starting in version 1.6 this widget is also used for displaying a relative
  7319. temperature value in the form of the rate of change indicator. To calculate
  7320. this correctly in Celsius or Kelvin we must have a way to bypass the
  7321. conversions for absolute measures.
  7322. @<TemperatureDisplay Implementation@>=
  7323. void TemperatureDisplay::setRelativeMode(bool relative)
  7324. {
  7325. r = relative;
  7326. }
  7327. @ Displaying a temperature is a simple matter of taking the temperature
  7328. component from the measurement and converting it to a string. Presently, this
  7329. code assumes that the measurements are in degrees Fahrenheit. If the code
  7330. becomes smarter about measurement units it might be a good idea to change this.
  7331. |QLCDNumber| is capable of displaying more than just numbers, so the call to
  7332. display takes a string which in this case consists of the temperature to the
  7333. $1\over100$th of a degree and might be followed by '@q'@> which will be
  7334. converted to $^\circ$ and the letter F, C, or r to indicate the unit. This
  7335. class should be mofified to allow a user specified precision.
  7336. @<TemperatureDisplay Implementation@>=
  7337. void TemperatureDisplay::setValue(Measurement temperature)
  7338. {
  7339. QString number;
  7340. switch(unit)
  7341. {
  7342. case Units::Fahrenheit:
  7343. display(QString("%1'F").
  7344. arg(number.setNum(temperature.toFahrenheit().temperature(), 'f',
  7345. 2)));
  7346. break;
  7347. case Units::Celsius:
  7348. if(!r) {
  7349. display(QString("%1'C").
  7350. arg(number.setNum(temperature.toCelsius().temperature(), 'f',
  7351. 2)));
  7352. } else {
  7353. number.setNum(temperature.temperature() * (5.0/9.0), 'f', 2);
  7354. display(QString("%1'C").arg(number));
  7355. }
  7356. break;
  7357. case Units::Kelvin:
  7358. if(!r) {
  7359. display(QString("%1").
  7360. arg(number.setNum(temperature.toKelvin().temperature(), 'f',
  7361. 2)));
  7362. } else {
  7363. number.setNum(temperature.temperature() * (5.0/9.0), 'f', 2);
  7364. display(QString("%1").arg(number));
  7365. }
  7366. break;
  7367. case Units::Rankine:
  7368. display(QString("%1'r").
  7369. arg(number.setNum(temperature.toRankine().temperature(), 'f',
  7370. 2)));
  7371. break;
  7372. case Units::Unitless:
  7373. display(QString("%1").arg(number.setNum(temperature.temperature(), 'f', 0)));
  7374. break;
  7375. default:
  7376. switch(temperature.scale())
  7377. {
  7378. case Units::Fahrenheit:
  7379. display(QString("%1'F").
  7380. arg(number.setNum(temperature.temperature(), 'f', 2)));
  7381. break;
  7382. case Units::Celsius:
  7383. display(QString("%1'C").
  7384. arg(number.setNum(temperature.temperature(), 'f', 2)));
  7385. break;
  7386. case Units::Kelvin:
  7387. display(QString("%1").
  7388. arg(number.setNum(temperature.temperature(), 'f', 2)));
  7389. break;
  7390. case Units::Rankine:
  7391. display(QString("%1'r").
  7392. arg(number.setNum(temperature.temperature(), 'f', 2)));
  7393. break;
  7394. case Units::Unitless:
  7395. display(QString("%1").arg(number.setNum(temperature.temperature(), 'f', 0)));
  7396. break;
  7397. default:
  7398. qDebug() << "Warning: Attempting to convert a non-temperature unit to a temperature unit";
  7399. break;
  7400. }
  7401. break;
  7402. }
  7403. }
  7404. @ Before measurements are displayed, we set a more sensible default display
  7405. style and an initial string. These defaults can all be overridden with calls to
  7406. the usual |QLCDNumber| methods.
  7407. \medskip
  7408. \resizebox*{6.3in}{!}{\includegraphics{QLCDNumber.png}}
  7409. \smallskip
  7410. \centerline{Figure \secno: Outline (Qt default) and Filled |QLCDNumber| Example}
  7411. \medskip
  7412. @<TemperatureDisplay Implementation@>=
  7413. TemperatureDisplay::TemperatureDisplay(QWidget *parent) :
  7414. QLCDNumber(8, parent), unit(Units::Fahrenheit), r(false)@/
  7415. {
  7416. setSegmentStyle(Filled);
  7417. display("---.--'F");
  7418. }
  7419. @ While it is not currently used, it would be good to allow an error state to
  7420. be easily discernible from a very stable temperature. Currently, if an error
  7421. occurs that results in the measurement thread exiting, no new measurements will
  7422. be generated and the temperature display will continue to read the most recent
  7423. measured value. If an error signal were emitted, it could be connected to the
  7424. following code to change the display to reflect the fact that the current
  7425. temperature is unknown.
  7426. @<TemperatureDisplay Implementation@>=
  7427. void TemperatureDisplay::invalidate()
  7428. {
  7429. display("---.--'F");
  7430. }
  7431. @ \pn{} supports the display of multiple types of unit. Typically, we use the
  7432. Auto scale which will cause |TemperatureDisplay| objects to display measurements
  7433. in whichever scale the measurement is associated with. Alternately, we can fix
  7434. the scale to a different supported scale and convert measurements to that scale
  7435. prior to display.
  7436. @<TemperatureDisplay Implementation@>=
  7437. void TemperatureDisplay::setDisplayUnits(Units::Unit scale)
  7438. {
  7439. unit = scale;
  7440. }
  7441. @ All that is left to deal with is the empty destructor.
  7442. @<TemperatureDisplay Implementation@>=
  7443. TemperatureDisplay::~TemperatureDisplay()
  7444. {
  7445. /* Nothing has to be done here. */
  7446. }
  7447. @ To use a |TemperatureDisplay| from a script, we need a function to pass a new
  7448. object to the scripting engine.
  7449. @<Function prototypes for scripting@>=
  7450. QScriptValue constructTemperatureDisplay(QScriptContext *context,
  7451. QScriptEngine *engine);
  7452. void setTemperatureDisplayProperties(QScriptValue value, QScriptEngine *engine);
  7453. QScriptValue TemperatureDisplay_setDisplayUnits(QScriptContext *context,
  7454. QScriptEngine *engine);
  7455. @ The scripting engine must be informed of this function.
  7456. @<Set up the scripting engine@>=
  7457. constructor = engine->newFunction(constructTemperatureDisplay);
  7458. value = engine->newQMetaObject(&TemperatureDisplay::staticMetaObject,
  7459. constructor);
  7460. engine->globalObject().setProperty("TemperatureDisplay", value);
  7461. @ The constructor is trivial.
  7462. @<Functions for scripting@>=
  7463. QScriptValue constructTemperatureDisplay(QScriptContext *,
  7464. QScriptEngine *engine)
  7465. {
  7466. QScriptValue object = engine->newQObject(new TemperatureDisplay);
  7467. setTemperatureDisplayProperties(object, engine);
  7468. return object;
  7469. }
  7470. void setTemperatureDisplayProperties(QScriptValue value, QScriptEngine *engine)
  7471. {
  7472. setQLCDNumberProperties(value, engine);
  7473. value.setProperty("setDisplayUnits",
  7474. engine->newFunction(TemperatureDisplay_setDisplayUnits));
  7475. }
  7476. @ There seems to be a bad interaction when enumerated value types as used as
  7477. the argument to slot methods called through QtScript. Script code that attempts
  7478. to make use of the enumeration appears to get the value without any type
  7479. information. When attempting to use that value as an argument the meta-object
  7480. system cannot find an appropriate match and the script just hangs silently.
  7481. The solution is to wrap such methods in the script bindings and explicitly cast
  7482. the argument value to the enumerated type. This looks stupid but it works.
  7483. @<Functions for scripting@>=
  7484. QScriptValue TemperatureDisplay_setDisplayUnits(QScriptContext *context, QScriptEngine *)
  7485. {
  7486. TemperatureDisplay *self = getself<@[TemperatureDisplay *@]>(context);
  7487. self->setDisplayUnits((Units::Unit)argument<int>(0, context));
  7488. return QScriptValue();
  7489. }
  7490. @* The MeasurementTimeOffset class.
  7491. When a |DAQ| object creates a |Measurement| object, the time component is a
  7492. system time. In most cases, the system time is not interesting and a more useful
  7493. time would be relative to the start of a process. This class should be used as a
  7494. filter, taking measurements with a system time stamp and producing measurements
  7495. with a relative time.
  7496. @<Class declarations@>=
  7497. class MeasurementTimeOffset : public QObject@/
  7498. {@t\1@>@/
  7499. Q_OBJECT@;
  7500. QTime epoch;
  7501. QTime previous;
  7502. bool hasPrevious;@/
  7503. public:@;
  7504. MeasurementTimeOffset(QTime zero);
  7505. QTime zeroTime();@/
  7506. @t\4@>public slots@t\kern-3pt@>:@;
  7507. void newMeasurement(Measurement measure);
  7508. void setZeroTime(QTime zero);
  7509. signals:@;
  7510. void measurement(Measurement measure);@t\2@>@/
  7511. }@t\kern-3pt@>;
  7512. @ The interesting part of this class is the function which takes a measurement
  7513. with a system time and produces a new measurement with a time relative to some
  7514. start time.
  7515. When using this class, it is possible that a measurement will arrive with a time
  7516. slightly before a newly chosen epoch. In such a case we do not want to simply
  7517. subtract the epoch from the measurement time as other classes will interpret
  7518. this incorrectly as a measurement time slightly less than 1 hour. This means
  7519. that we need to check if the measurement time is before the epoch. If it is, we
  7520. consider it to have been generated at the epoch rather than before. Aren't race
  7521. conditions fun?
  7522. Additionally, since we're only getting time of day information, some special
  7523. handling must be done for time series that cross the boundary between days. We
  7524. should never get measurements out of order, so keeping a record of the previous
  7525. measurement and verifying that the new measurement comes after it is sufficient.
  7526. @<MeasurementTimeOffset Implementation@>=
  7527. void MeasurementTimeOffset::newMeasurement(Measurement measure)@t\2\2@>@/
  7528. {@t\1@>@/
  7529. if(measure.time() < epoch)@/
  7530. {
  7531. if(hasPrevious)@/
  7532. {
  7533. QTime jitBase(epoch.hour() - 1, epoch.minute(), epoch.second(),
  7534. epoch.msec());
  7535. QTime jitComp(epoch.hour(), measure.time().minute(),
  7536. measure.time().second(), measure.time().msec());
  7537. int relTime = jitBase.msecsTo(jitComp);
  7538. @<Produce and emit relative time@>@;
  7539. }
  7540. else@/
  7541. {
  7542. Measurement rel = measure;
  7543. rel.setTime(QTime(0, 0, 0, 0));
  7544. emit measurement(rel);
  7545. }
  7546. }
  7547. else@/
  7548. {
  7549. int relTime = epoch.msecsTo(measure.time());
  7550. @<Produce and emit relative time@>@;
  7551. }
  7552. hasPrevious = true;
  7553. previous = measure.time();@t\2@>@/
  7554. }
  7555. @ The measurement emitted has a time with the number of minutes, seconds, and
  7556. milliseconds since the start of the batch. We never generate a time greater
  7557. than 1 hour.
  7558. @<Produce and emit relative time@>=
  7559. QTime newTime(0, 0, 0, 0);
  7560. newTime = newTime.addMSecs(relTime);
  7561. if(newTime.hour() > 0)
  7562. {
  7563. newTime.setHMS(0, newTime.minute(), newTime.second(), newTime.msec());
  7564. }
  7565. Measurement rel = measure;
  7566. rel.setTime(newTime);
  7567. emit measurement(rel);
  7568. @ The rest of the code handles updating and reporting the reference time.
  7569. @<MeasurementTimeOffset Implementation@>=
  7570. MeasurementTimeOffset::MeasurementTimeOffset(QTime zero) : epoch(zero),
  7571. previous(0, 0, 0, 0), hasPrevious(false)
  7572. {
  7573. /* Nothing has to be done here. */
  7574. }
  7575. QTime MeasurementTimeOffset::zeroTime()
  7576. {
  7577. return epoch;
  7578. }
  7579. void MeasurementTimeOffset::setZeroTime(QTime zero)
  7580. {
  7581. epoch = zero;
  7582. hasPrevious = false;
  7583. }
  7584. @ Two functions are required to make this class available to the scripting
  7585. engine.
  7586. @<Function prototypes for scripting@>=
  7587. QScriptValue constructMeasurementTimeOffset(QScriptContext *context,@|
  7588. QScriptEngine *engine);
  7589. void setMeasurementTimeOffsetProperties(QScriptValue value,
  7590. QScriptEngine *engine);
  7591. @ The scripting engine must be informed of the constructor.
  7592. @<Set up the scripting engine@>=
  7593. constructor = engine->newFunction(constructMeasurementTimeOffset);
  7594. value = engine->newQMetaObject(&MeasurementTimeOffset::staticMetaObject,
  7595. constructor);
  7596. engine->globalObject().setProperty("MeasurementTimeOffset", value);
  7597. @ Previously, another property was added to the newly created object. That
  7598. method is believed to be obsolete and has been removed. Careful testing will
  7599. need to be done to verify that this decision was sane. I was very hungry when
  7600. that change was made.
  7601. @<Functions for scripting@>=
  7602. QScriptValue constructMeasurementTimeOffset(QScriptContext *,
  7603. QScriptEngine *engine)
  7604. {
  7605. QScriptValue object =@|
  7606. engine->newQObject(new MeasurementTimeOffset(QTime::currentTime()));
  7607. setMeasurementTimeOffsetProperties(object, engine);
  7608. return object;
  7609. }
  7610. void setMeasurementTimeOffsetProperties(QScriptValue value,
  7611. QScriptEngine *engine)
  7612. {
  7613. setQObjectProperties(value, engine);
  7614. }
  7615. @* Measured value threshold detection.
  7616. \noindent There are times when one might want to detect when a measured value
  7617. from a data series has passed a given value, with the limitation that we are
  7618. only interested in the ascending or descending edge. This can be used, for
  7619. example, to translate roast profile data in a graph along the time axis such
  7620. that the bean temperature series are aligned at a given
  7621. temperature.\nfnote{More details on the reasoning behind why one might want
  7622. to do this are provided at:\par\indent\pdfURL{http://youtu.be/hS0SfzypyFQ}
  7623. {http://youtu.be/hS0SfzypyFQ}} For this we can use a |ThresholdDetector|.
  7624. If we would like to catch changes on both the ascending and descending edge, we
  7625. can use two objects, however it may be a good idea to use more than two to
  7626. allow for sane behavior in the face of hysteresis.
  7627. @<Class declarations@>=
  7628. class ThresholdDetector : public QObject@/
  7629. {
  7630. @[Q_OBJECT@]@;
  7631. @[Q_ENUMS(EdgeDirection)@]@;
  7632. public:@/
  7633. enum EdgeDirection {
  7634. Ascending, Descending
  7635. };
  7636. ThresholdDetector(double value);
  7637. @[public slots@]:@/
  7638. void newMeasurement(Measurement measure);
  7639. void setThreshold(double value);
  7640. void setEdgeDirection(EdgeDirection direction);
  7641. signals:@/
  7642. void timeForValue(double);
  7643. private:@/
  7644. bool previousValueValid;
  7645. double previousValue;
  7646. double threshold;
  7647. EdgeDirection currentDirection;
  7648. };
  7649. @ This class emits the time in seconds when a given measurement crosses the
  7650. threshold value in the appropriate direction.
  7651. This was previously written with |previousValue| initialized negative and a
  7652. check that |previousValue| was non-negative. When the |ThresholdDetector| is
  7653. connected to a data source representing temperature measurements this is a
  7654. reasonable choice, however it breaks when connected to a rate of change series.
  7655. To make this more generally correct, a boolean is checked to determine if a
  7656. previous value has been set.
  7657. @<ThresholdDetector Implementation@>=
  7658. void ThresholdDetector::newMeasurement(Measurement measure)
  7659. {
  7660. if((currentDirection == Ascending && previousValue < threshold &&
  7661. previousValueValid) || (currentDirection == Descending &&
  7662. previousValue > threshold && previousValueValid))
  7663. {
  7664. if((currentDirection == Ascending && measure.temperature() >= threshold) ||
  7665. (currentDirection == Descending && measure.temperature() <= threshold))
  7666. {
  7667. double offset = measure.time().hour() * 60 * 60;
  7668. offset += measure.time().minute() * 60;
  7669. offset += measure.time().second();
  7670. offset += measure.time().msec()/1000;
  7671. emit timeForValue(offset);
  7672. }
  7673. }
  7674. previousValue = measure.temperature();
  7675. previousValueValid = true;
  7676. }
  7677. ThresholdDetector::ThresholdDetector(double value) : QObject(NULL),
  7678. previousValueValid(false),
  7679. previousValue(-1), threshold(value), currentDirection(Ascending)
  7680. {
  7681. /* Nothing needs to be done here. */
  7682. }
  7683. void ThresholdDetector::setThreshold(double value)
  7684. {
  7685. threshold = value;
  7686. }
  7687. void ThresholdDetector::setEdgeDirection(EdgeDirection direction)
  7688. {
  7689. currentDirection = direction;
  7690. }
  7691. @ This is exposed to the host environment.
  7692. @<Function prototypes for scripting@>=
  7693. QScriptValue constructThresholdDetector(QScriptContext *context, QScriptEngine *engine);
  7694. void setThresholdDetectorProperties(QScriptValue value, QScriptEngine *engine);
  7695. @ Inform the engine of the constructor.
  7696. @<Set up the scripting engine@>=
  7697. constructor = engine->newFunction(constructThresholdDetector);
  7698. value = engine->newQMetaObject(&ThresholdDetector::staticMetaObject, constructor);
  7699. engine->globalObject().setProperty("ThresholdDetector", value);
  7700. @ Implementation. At present I'@q'@>m not bothering to implement constructor
  7701. arguments here and am aligning on a fixed point. Another slot method was added
  7702. to restore adjustability.
  7703. @<Functions for scripting@>=
  7704. QScriptValue constructThresholdDetector(QScriptContext *, QScriptEngine *engine)
  7705. {
  7706. QScriptValue object = engine->newQObject(new ThresholdDetector(300));
  7707. return object;
  7708. }
  7709. void setThresholdDetectorProperties(QScriptValue value, QScriptEngine *engine)
  7710. {
  7711. setQObjectProperties(value, engine);
  7712. }
  7713. @* The ZeroEmitter class.
  7714. \noindent Now that it is possible to record the time a measurement was taken
  7715. relative to an arbitrary start time, there is a minor problem left for logging.
  7716. It is extremely unlikely that a measurement will be passed through at the epoch.
  7717. For this, what we want to do is save the previous measurement and make it
  7718. available at time 0 whenever the start time is reset. This is the role of the
  7719. |ZeroEmitter| class.
  7720. Another problem is that most classes that are interested in a relative time are
  7721. also interested in logging multiple sets of temperature data. To facilitate this
  7722. an integer is emitted. Different temperature measurement sources should be set
  7723. to emit different numbers. A table view would place measurements in the
  7724. indicated column. A graph view would use different colors to plot different sets
  7725. of data.
  7726. @<Class declarations@>=
  7727. class ZeroEmitter : public QObject@/
  7728. {@t\1@>@/
  7729. @[Q_OBJECT@]@;
  7730. @[Q_PROPERTY(int column READ column WRITE setColumn)@]@;
  7731. Measurement cache;
  7732. int col;
  7733. public:@/
  7734. ZeroEmitter(int tempcolumn = 1);
  7735. int column();
  7736. double lastTemperature();@/
  7737. @t\4@>public slots@t\kern-3pt@>:@;
  7738. void newMeasurement(Measurement measure);
  7739. void setColumn(int column);
  7740. void emitZero();
  7741. signals:@;
  7742. void measurement(Measurement measure, int tempcolumn);@t\2@>@/
  7743. }@t\kern-3pt@>;
  7744. @ The implementation of the class is trivial.
  7745. @<ZeroEmitter Implementation@>=
  7746. ZeroEmitter::ZeroEmitter(int tempcolumn) : QObject(NULL), col(tempcolumn)@;
  7747. {
  7748. /* Nothing has to be done here. */
  7749. }
  7750. int ZeroEmitter::column()
  7751. {
  7752. return col;
  7753. }
  7754. double ZeroEmitter::lastTemperature()
  7755. {
  7756. return cache.temperature();
  7757. }
  7758. void ZeroEmitter::newMeasurement(Measurement measure)
  7759. {
  7760. cache = measure;
  7761. }
  7762. void ZeroEmitter::setColumn(int column)
  7763. {
  7764. col = column;
  7765. }
  7766. void ZeroEmitter::emitZero()
  7767. {
  7768. cache.setTime(QTime(0, 0, 0, 0));
  7769. emit measurement(cache, col);
  7770. }
  7771. @ Making this class available to scripts requires only two functions.
  7772. @<Function prototypes for scripting@>=
  7773. QScriptValue constructZeroEmitter(QScriptContext *context,
  7774. QScriptEngine *engine);
  7775. void setZeroEmitterProperties(QScriptValue value, QScriptEngine *engine);
  7776. @ To use it, we must inform the engine of the constructor.
  7777. @<Set up the scripting engine@>=
  7778. constructor = engine->newFunction(constructZeroEmitter);
  7779. value = engine->newQMetaObject(&ZeroEmitter::staticMetaObject, constructor);
  7780. engine->globalObject().setProperty("ZeroEmitter", value);
  7781. @ The implementation of the constructor is trivial.
  7782. @<Functions for scripting@>=
  7783. QScriptValue constructZeroEmitter(QScriptContext *context,
  7784. QScriptEngine *engine)
  7785. {
  7786. QScriptValue object =
  7787. engine->newQObject(new ZeroEmitter(argument<int>(0, context)));
  7788. setZeroEmitterProperties(object, engine);
  7789. return object;
  7790. }
  7791. void setZeroEmitterProperties(QScriptValue value, QScriptEngine *engine)
  7792. {
  7793. setQObjectProperties(value, engine);
  7794. }
  7795. @* The MeasurementAdapter class.
  7796. \noindent The last of the measurement filter classes is the |MeasurementAdapter|
  7797. class. This takes measurements, typically from a |MeasurementTimeOffset|, and
  7798. sends the measurement out with a number to indicate which data series the
  7799. measurement belongs to.
  7800. The measurement pipeline could probably be made simpler by introducing a series
  7801. identifier into the measurement class.
  7802. @<Class declarations@>=
  7803. class MeasurementAdapter : public QObject@/
  7804. {@t\1@>@/
  7805. Q_OBJECT@;
  7806. int col;
  7807. public:@/
  7808. MeasurementAdapter(int tempcolumn);
  7809. int column();@/
  7810. @t\4@>public slots@t\kern-3pt@>:@/
  7811. void newMeasurement(Measurement measure);
  7812. void setColumn(int column);
  7813. signals:@/
  7814. void measurement(Measurement measure, int tempcolumn);@t\2@>@/
  7815. }@t\kern-3pt@>;
  7816. @ The implementation of this filter class is trivial.
  7817. @<MeasurementAdapter Implementation@>=
  7818. MeasurementAdapter::MeasurementAdapter(int tempcolumn) : col(tempcolumn)@;
  7819. {
  7820. /* Nothing has to be done here. */
  7821. }
  7822. int MeasurementAdapter::column()
  7823. {
  7824. return col;
  7825. }
  7826. void MeasurementAdapter::newMeasurement(Measurement measure)
  7827. {
  7828. emit measurement(measure, col);
  7829. }
  7830. void MeasurementAdapter::setColumn(int column)
  7831. {
  7832. col = column;
  7833. }
  7834. @ This filter class is also available from the host environment.
  7835. @<Function prototypes for scripting@>=
  7836. QScriptValue constructMeasurementAdapter(QScriptContext *context,
  7837. QScriptEngine *engine);
  7838. void setMeasurementAdapterProperties(QScriptValue value, QScriptEngine *engine);
  7839. @ As usual, the engine must be informed of the constructor.
  7840. @<Set up the scripting engine@>=
  7841. constructor = engine->newFunction(constructMeasurementAdapter);
  7842. value = engine->newQMetaObject(&MeasurementAdapter::staticMetaObject,
  7843. constructor);
  7844. engine->globalObject().setProperty("MeasurementAdapter", value);
  7845. @ The implementation is trivial.
  7846. @<Functions for scripting@>=
  7847. QScriptValue constructMeasurementAdapter(QScriptContext *context,
  7848. QScriptEngine *engine)
  7849. {
  7850. QScriptValue object =
  7851. engine->newQObject(new MeasurementAdapter(argument<int>(0, context)));
  7852. setMeasurementAdapterProperties(object, engine);
  7853. return object;
  7854. }
  7855. void setMeasurementAdapterProperties(QScriptValue value, QScriptEngine *engine)
  7856. {
  7857. setQObjectProperties(value, engine);
  7858. }
  7859. @* A graph of temperature over time.
  7860. \noindent A useful tool when roasting is a visual depiction of the current batch
  7861. as it happens, possibly laid over a previously recorded target profile. The
  7862. |GraphView| class can take multiple sets of temperature data and produce such a
  7863. visualization.
  7864. \medskip
  7865. \centerline{\includegraphics{roast}}
  7866. \smallskip
  7867. \centerline{Figure \secno: A Typical Roast}
  7868. \medskip
  7869. This class assumes that temperature data will be passed in the correct order.
  7870. @<Class declarations@>=
  7871. class GraphView : public QGraphicsView@/
  7872. {@t\1@>@/
  7873. Q_OBJECT@;
  7874. QGraphicsScene *theScene;@/
  7875. QMap<int, QList<QGraphicsLineItem * > * > *graphLines;@/
  7876. QMap<int, QPointF> *prevPoints;
  7877. QMap<int, double> *translations;
  7878. QList<QGraphicsItem *> *gridLinesF;
  7879. QList<QGraphicsItem *> *gridLinesC;
  7880. QList<QGraphicsItem *> *relativeGridLines;
  7881. bool relativeEnabled;
  7882. bool timeIndicatorEnabled;
  7883. QGraphicsLineItem *timeLine;
  7884. LinearSplineInterpolator *relativeAdjuster;@/
  7885. public:@/
  7886. GraphView(QWidget *parent = NULL);
  7887. void removeSeries(int column);@/
  7888. protected:@/
  7889. void resizeEvent(QResizeEvent *event);@/
  7890. @t\4@>public slots@t\kern-3pt@>:@/
  7891. void newMeasurement(Measurement measure, int tempcolumn);
  7892. void setSeriesTranslation(int column, double offset);
  7893. void setTimeIndicatorEnabled(bool enabled);
  7894. void clear();
  7895. void showF();
  7896. void showC();@t\2@>@/
  7897. }@t\kern-3pt@>;
  7898. @ I decided that it would probably be best to keep the graph area the same even
  7899. with different roast lengths over different temperature ranges so that similar
  7900. portions between these graphs would continue to look similar rather than
  7901. constantly attempting to select the best way to display the data currently in
  7902. the view.
  7903. I have chosen to represent roasting times of up to 20 minutes and temperatures
  7904. between 0 and 500$^\circ$ Fahrenheit. This should certainly be configurable at
  7905. run time, but until that is implemented, roasters who routinely roast batches
  7906. for longer periods of time will want to change the constructor. This class
  7907. should probably be modified to allow the user to specify several characteristics
  7908. of the display.
  7909. This class must also deal with the fact that coordinates in a |QGraphicsScene|
  7910. are not quite the same as coordinates in a cartesian plane. The easiest way to
  7911. deal with this is to negate the y coordinate and translate the viewport to the
  7912. area we draw in.
  7913. A small margin area left around the edges of the graph. This should probably be
  7914. configurable for those with particularly small displays.
  7915. @<GraphView Implementation@>=
  7916. GraphView::GraphView(QWidget *parent) : QGraphicsView(parent),
  7917. theScene(new QGraphicsScene),@/
  7918. graphLines(new QMap<int, QList<QGraphicsLineItem *> *>),@/
  7919. prevPoints(new QMap<int, QPointF>),
  7920. translations(new QMap<int, double>),
  7921. gridLinesF(new QList<QGraphicsItem *>),
  7922. gridLinesC(new QList<QGraphicsItem *>),
  7923. relativeGridLines(new QList<QGraphicsItem *>),
  7924. relativeEnabled(false),
  7925. timeIndicatorEnabled(false),
  7926. timeLine(new QGraphicsLineItem),
  7927. relativeAdjuster(new LinearSplineInterpolator)@/
  7928. {
  7929. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  7930. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  7931. setScene(theScene);
  7932. setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  7933. QPen timePen;
  7934. timePen.setColor(QColor(160, 160, 164, 127)); //gray, half opacity
  7935. timeLine->setPen(timePen);
  7936. timeLine->setLine(0, 0, 0, -500);
  7937. timeLine->hide();
  7938. theScene->addItem(timeLine);
  7939. @<Draw temperature axis and grid lines@>;
  7940. @<Draw secondary axes@>@;
  7941. @<Draw time axis and ticks@>;
  7942. fitInView(theScene->sceneRect().adjusted(-50,-50,50,50));
  7943. }
  7944. @ Grid lines are drawn every 100 degrees. These lines are labeled with
  7945. |setHtml()| for convenient access to the $^\circ$ symbol. If \pn{} is modified
  7946. to allow different units, this code should also be modified.
  7947. As of version 1.3.3 it is possible to switch the graph between degrees
  7948. Fahrenheit and degrees Celcius. Celcius grid lines are drawn but initially
  7949. hidden. Both the grid lines and the labels are added to a list depending on the
  7950. unit so that when changing from one view to another all that needs to happen is
  7951. hide one list of items and show another.
  7952. @<Draw temperature axis and grid lines@>=
  7953. QGraphicsLineItem *tempaxis = new QGraphicsLineItem;
  7954. tempaxis->setLine(-10, -500, -10, 0);
  7955. theScene->addItem(tempaxis);
  7956. QGraphicsLineItem *gridLine;
  7957. QGraphicsTextItem *label;
  7958. for(int y = -100; y > -600; y -= 100)@/
  7959. {@/
  7960. gridLine = new QGraphicsLineItem;
  7961. gridLine->setLine(0, y, 1200, y);
  7962. theScene->addItem(gridLine);
  7963. label = new QGraphicsTextItem;
  7964. label->setHtml(QString("%1&deg;F").arg(-y));
  7965. label->setPos(-55, y - (label->boundingRect().height() / 2));
  7966. theScene->addItem(label);
  7967. gridLinesF->append(gridLine);
  7968. gridLinesF->append(label);
  7969. }
  7970. for(int degC = 50; degC <= 250; degC += 50)
  7971. {
  7972. gridLine = new QGraphicsLineItem;
  7973. int y = -(degC * (9.0/5.0) + 32);
  7974. gridLine->setLine(0, y, 1200, y);
  7975. gridLine->hide();
  7976. theScene->addItem(gridLine);
  7977. gridLinesC->append(gridLine);
  7978. label = new QGraphicsTextItem;
  7979. label->setHtml(QString("%1&deg;C").arg(degC));
  7980. label->setPos(-55, y - (label->boundingRect().height() / 2));
  7981. label->hide();
  7982. theScene->addItem(label);
  7983. gridLinesC->append(label);
  7984. }
  7985. @ If we are going to plot relative temperature measurements, we must obtain
  7986. information on how we wish to do that from settings. We take advantage of the
  7987. fact that iterating over the keys in a |QMap| produces results in sorted order.
  7988. While drawing the grid lines we also set up the |relativeAdjuster| that will be
  7989. used to transform incoming measurements to our coordinate system.
  7990. @<Draw secondary axes@>=
  7991. QSettings settings;
  7992. if(settings.contains("settings/graph/relative/enable"))
  7993. {
  7994. if(settings.value("settings/graph/relative/enable").toBool())
  7995. {
  7996. relativeEnabled = @[true@];
  7997. QColor relativeColor = QColor(settings.value("settings/graph/relative/color").toString());
  7998. QString unit = QString(settings.value(@|"settings/graph/relative/unit").toInt() == 0 ? "F" : "C");
  7999. QMap<double, QString> relativeAxisPairs;
  8000. QStringList relativeAxisLabels = settings.value(@|"settings/graph/relative/grid").toString().split(QRegExp("[\\s,]+"), QString::SkipEmptyParts);
  8001. foreach(QString item, relativeAxisLabels)
  8002. {
  8003. relativeAxisPairs.insert(item.toDouble(), item);
  8004. }
  8005. if(relativeAxisPairs.size() > 1)
  8006. {
  8007. double skip = 500.0 / (relativeAxisPairs.size() - 1);
  8008. double y = 0;
  8009. foreach(double key, relativeAxisPairs.keys())
  8010. {
  8011. gridLine = new QGraphicsLineItem;
  8012. gridLine->setLine(0, y, 1205, y);
  8013. gridLine->setPen(QPen(relativeColor));
  8014. theScene->addItem(gridLine);
  8015. relativeGridLines->append(gridLine);
  8016. label = new QGraphicsTextItem;
  8017. label->setHtml(QString("%1&deg;%2").arg(relativeAxisPairs.value(key)).arg(unit));
  8018. label->setPos(1210, y - (label->boundingRect().height() / 2));
  8019. theScene->addItem(label);
  8020. relativeGridLines->append(label);
  8021. if(unit == "F")
  8022. {
  8023. relativeAdjuster->add_pair(key, -y);
  8024. }
  8025. else
  8026. {
  8027. relativeAdjuster->add_pair(key * (9.0/5.0), -y);
  8028. }
  8029. y -= skip;
  8030. }
  8031. }
  8032. }
  8033. }
  8034. @ Two slots are used to switch between the different sets of grid lines.
  8035. @<GraphView Implementation@>=
  8036. void GraphView::showF()
  8037. {
  8038. for(int i = 0; i < gridLinesF->size(); i++)
  8039. {
  8040. gridLinesF->at(i)->show();
  8041. }
  8042. for(int i = 0; i < gridLinesC->size(); i++)
  8043. {
  8044. gridLinesC->at(i)->hide();
  8045. }
  8046. }
  8047. void GraphView::showC()
  8048. {
  8049. for(int i = 0; i < gridLinesF->size(); i++)
  8050. {
  8051. gridLinesF->at(i)->hide();
  8052. }
  8053. for(int i = 0; i < gridLinesC->size(); i++)
  8054. {
  8055. gridLinesC->at(i)->show();
  8056. }
  8057. }
  8058. @ The time axis has a tick every two minutes. The use of the |?| tertiary
  8059. operator shifts the labels with two digits a little more than labels with only
  8060. one digit. If it worked, a more resilient approach would be to take the width of
  8061. the label and center it under the tick.
  8062. @<Draw time axis and ticks@>=
  8063. QGraphicsLineItem *timeaxis = new QGraphicsLineItem;
  8064. timeaxis->setLine(0, 10, 1200, 10);
  8065. theScene->addItem(timeaxis);
  8066. for(int x = 0; x < 1201; x += 120)@/
  8067. {@/
  8068. QGraphicsLineItem *tick = new QGraphicsLineItem;
  8069. tick->setLine(x, 0, x, 20);
  8070. theScene->addItem(tick);
  8071. QGraphicsTextItem *label = new QGraphicsTextItem;
  8072. label->setPlainText(QString("%1").arg(x/60));
  8073. label->setPos(x - (label->boundingRect().width() / 2), 20);
  8074. theScene->addItem(label);
  8075. }
  8076. @ Typically, the user will be able to resize the graph. When the widget is
  8077. resized, we should fit the graph to the new size of the widget. This is safe to
  8078. do as we have already turned off the scroll bars.
  8079. @<GraphView Implementation@>=
  8080. void GraphView::resizeEvent(QResizeEvent *)
  8081. {
  8082. fitInView(theScene->sceneRect().adjusted(-50,-50,50,50));
  8083. }
  8084. @ When adding a new measurement, there are three cases that should be
  8085. considered. In the case of the first measurement, no drawing occurs. A |QList|
  8086. of line items is initialized when the second measurement is taken. Subsequent
  8087. measurements are able to simply append new line segments to the list.
  8088. Relative measurements are first converted to the coordinate system of the
  8089. appropriate secondary axis.
  8090. @<GraphView Implementation@>=
  8091. #define FULLTIMETOINT(t) (t.msec() + (t.second() * 1000) + (t.minute() * 60 * 1000))
  8092. void GraphView::newMeasurement(Measurement measure, int tempcolumn)@/
  8093. {@/
  8094. double offset = 0;
  8095. if(measure.contains("relative"))
  8096. {
  8097. if(measure.value("relative").toBool())
  8098. {
  8099. if(relativeEnabled)
  8100. {
  8101. measure.setTemperature(relativeAdjuster->newMeasurement(measure).temperature());
  8102. }
  8103. else
  8104. {
  8105. return;
  8106. }
  8107. }
  8108. }
  8109. if(translations->contains(tempcolumn))
  8110. {
  8111. offset = translations->value(tempcolumn);
  8112. }
  8113. if(prevPoints->contains(tempcolumn))@/
  8114. @t\1@>{@/
  8115. @<At least one measurement exists@>@;
  8116. if(graphLines->contains(tempcolumn))@/
  8117. {@t\1@>
  8118. /* More than one measurement existed. */
  8119. graphLines->value(tempcolumn)->append(segment);@t\2@>@/
  8120. }@/
  8121. else@/
  8122. {@/
  8123. /* This is the second measurement. */
  8124. QList<QGraphicsLineItem *> *newLine =
  8125. new QList<QGraphicsLineItem *>;@/
  8126. newLine->append(segment);
  8127. graphLines->insert(tempcolumn, newLine);
  8128. }@t\2@>@/
  8129. }@/
  8130. else@/
  8131. {@/
  8132. @<Handle the first measurement@>@;
  8133. }
  8134. }
  8135. @ There are some parts of the code that are correct, but seem somewhat goofy.
  8136. This is especially true surrounding the graphics view architecture as this was
  8137. not working correctly when I wrote the code that uses it. The code as it is
  8138. written works for me, but when these workarounds are no longer needed it would
  8139. be better to remove them. Handling values on the time axis is one example of
  8140. this.
  8141. Some might question the use of an integer data type, particularly when storing
  8142. the result of a division operation. While this could be a concern if high
  8143. resolution wall sized displays become common, is is expected that in most cases
  8144. with sufficiently high sample rates, many data points will map to the same pixel
  8145. even with the minor data loss below.
  8146. In the case of the first measurement,
  8147. @<Handle the first measurement@>=
  8148. int x = FULLTIMETOINT(measure.time())/1000;
  8149. prevPoints->insert(tempcolumn, QPointF(x, measure.temperature()));
  8150. if(timeIndicatorEnabled)
  8151. {
  8152. timeLine->setLine(x, 0, x, -500);
  8153. }
  8154. @ When at least one measurement already exists, we need to handle drawing the
  8155. line between the new measurement and the previous measurement.
  8156. \danger At present, the color chosen for these lines is based on the temperature
  8157. column passed to the graph. It would be better if colors could be passed to the
  8158. view for a specified series rather than have this hard coded. \endanger
  8159. @<At least one measurement exists@>=
  8160. QGraphicsLineItem *segment = new QGraphicsLineItem;
  8161. QPointF nextPoint(FULLTIMETOINT(measure.time())/1000, measure.temperature());
  8162. segment->setLine(prevPoints->value(tempcolumn).x() + offset,
  8163. -(prevPoints->value(tempcolumn).y()),
  8164. nextPoint.x() + offset, -(nextPoint.y()));
  8165. static QColor p[12] = {Qt::yellow, Qt::blue, Qt::cyan, Qt::red, Qt::magenta,
  8166. Qt::green, Qt::darkGreen, Qt::darkMagenta,
  8167. Qt::darkRed, Qt::darkCyan, Qt::darkBlue,
  8168. Qt::darkYellow};
  8169. segment->setPen(p[tempcolumn % 12]);
  8170. theScene->addItem(segment);
  8171. prevPoints->insert(tempcolumn, nextPoint);
  8172. if(timeIndicatorEnabled)
  8173. {
  8174. timeLine->setLine(nextPoint.x() + offset, 0, nextPoint.x() + offset, -500);
  8175. }
  8176. @ In addition to adding data to the view, we also sometimes want to clear the
  8177. view of data.
  8178. @<GraphView Implementation@>=
  8179. void GraphView::clear()
  8180. {
  8181. int i;
  8182. foreach(i, prevPoints->keys())
  8183. {
  8184. removeSeries(i);
  8185. }
  8186. translations->clear();
  8187. }
  8188. @ Removing a set of data from the view involves removing the lines from the
  8189. scene and removing the column from a couple data structures.
  8190. @<GraphView Implementation@>=
  8191. void GraphView::removeSeries(int column)
  8192. {
  8193. if(graphLines->contains(column))
  8194. {
  8195. QList<QGraphicsLineItem *> *series = graphLines->value(column);
  8196. QGraphicsLineItem *segment;
  8197. foreach(segment, *series)
  8198. {
  8199. theScene->removeItem(segment);
  8200. }
  8201. qDeleteAll(*series);
  8202. }
  8203. graphLines->remove(column);
  8204. prevPoints->remove(column);
  8205. }
  8206. @ Second prototype for data series translation.
  8207. @<GraphView Implementation@>=
  8208. void GraphView::setSeriesTranslation(int column, double offset)
  8209. {
  8210. if(graphLines->contains(column))
  8211. {
  8212. QList<QGraphicsLineItem *> *series = graphLines->value(column);
  8213. QGraphicsLineItem *segment;
  8214. foreach(segment, *series)
  8215. {
  8216. segment->setPos(segment->pos().x()+offset, segment->pos().y());
  8217. }
  8218. }
  8219. if(translations->contains(column))
  8220. {
  8221. translations->insert(column, offset + translations->value(column));
  8222. }
  8223. else
  8224. {
  8225. translations->insert(column, offset);
  8226. }
  8227. }
  8228. @ Starting in \pn{} 1.6 it is possible to add a vertical line indicating the
  8229. time position of the most recent measurement. This should be hidden for loading
  8230. target roast profiles and shown depending on preference. A new method controls
  8231. this.
  8232. @<GraphView Implementation@>=
  8233. void GraphView::setTimeIndicatorEnabled(bool enabled)
  8234. {
  8235. timeIndicatorEnabled = enabled;
  8236. if(enabled)
  8237. {
  8238. timeLine->show();
  8239. }
  8240. else
  8241. {
  8242. timeLine->hide();
  8243. }
  8244. }
  8245. @ These functions are required to create a |GraphView| object from a script.
  8246. @<Function prototypes for scripting@>=
  8247. void setGraphViewProperties(QScriptValue value, QScriptEngine *engine);
  8248. QScriptValue constructGraphView(QScriptContext *context, QScriptEngine *engine);
  8249. @ The scripting engine must be informed of the constructor.
  8250. @<Set up the scripting engine@>=
  8251. constructor = engine->newFunction(constructGraphView);
  8252. value = engine->newQMetaObject(&GraphView::staticMetaObject, constructor);
  8253. engine->globalObject().setProperty("GraphView", value);
  8254. @ The function implementation is trivial.
  8255. @<Functions for scripting@>=
  8256. QScriptValue constructGraphView(QScriptContext *, QScriptEngine *engine)
  8257. {
  8258. QScriptValue object = engine->newQObject(new GraphView);
  8259. setGraphViewProperties(object, engine);
  8260. return object;
  8261. }
  8262. void setGraphViewProperties(QScriptValue value, QScriptEngine *engine)
  8263. {
  8264. setQGraphicsViewProperties(value, engine);
  8265. }
  8266. @* A table of roasting data.
  8267. \noindent A typical roast log is a table listing temperature measurements taken
  8268. at regular intervals. The introduction of a computer brings several advantages.
  8269. A human does not need to record the measurements. Every measurement taken can be
  8270. logged, but the measurements do not all need to be displayed. The |ZoomLog|
  8271. class presents a table with time, temperature, and annotation for one or more
  8272. sets of roasting data and allows the user to select from a few different levels
  8273. of detail.
  8274. Experience has shown that one measurement every 30 or 15 seconds is most useful,
  8275. but it is also possible to view one measurement every 1, 5, 10, or 60 seconds
  8276. and there is an option to view every measurement collected. This last is what is
  8277. saved to a file.
  8278. The zooming log is implemented by keeping a measurement model with every level
  8279. of detail of interest and making sure that new measurements get to the models
  8280. they belong in. Switching the level of detail of the view then becomes a matter
  8281. of changing which model the view is using. This is very inefficient in terms of
  8282. space, but it is very fast and simple to code.
  8283. Starting in version 1.4, column sizes are persisted automatically using the
  8284. same method as described in the section on |SqlQueryView|.
  8285. Starting in version 1.8, |rowCount()| is |Q_INVOKABLE|. This allows the manual
  8286. log entry interface to easily determine if any roasting data exists to save.
  8287. @<Class declarations@>=
  8288. class MeasurementModel;@/
  8289. class ZoomLog : public QTableView@/
  8290. {@/
  8291. @[Q_OBJECT@]@;
  8292. @<ZoomLog private member data@>@;
  8293. void switchLOD(MeasurementModel *m);@/
  8294. @[private slots@]:@/
  8295. void centerOn(int row);
  8296. void persistColumnResize(int column, int oldsize, int newsize);
  8297. void restoreColumnWidths();
  8298. public:@/
  8299. ZoomLog();
  8300. QVariant data(int row, int column) const;
  8301. @[Q_INVOKABLE@,@, int rowCount();
  8302. bool saveXML(QIODevice *device);
  8303. bool saveCSV(QIODevice *device);
  8304. QString lastTime(int series);
  8305. @[Q_INVOKABLE@,@, Units::Unit displayUnits()@];@t\2\2@>@/
  8306. @[public slots@]:@/
  8307. void setVisible(bool visibility);
  8308. void setHeaderData(int section, QString text);
  8309. void LOD_ms();
  8310. void LOD_1s();
  8311. void LOD_5s();
  8312. void LOD_10s();
  8313. void LOD_15s();
  8314. void LOD_30s();
  8315. void LOD_1m();
  8316. void newMeasurement(Measurement measure, int tempcolumn);
  8317. void newAnnotation(QString annotation, int tempcolumn,
  8318. int annotationcolumn);
  8319. void clear();
  8320. void addOutputTemperatureColumn(int column);
  8321. void addOutputControlColumn(int column);
  8322. void addOutputAnnotationColumn(int column);
  8323. void clearOutputColumns();
  8324. void setDisplayUnits(Units::Unit scale);
  8325. void addToCurrentColumnSet(int column);
  8326. void clearCurrentColumnSet();@/
  8327. protected:@/
  8328. virtual void showEvent(QShowEvent *event);
  8329. };
  8330. @ This class uses a different model for each level of detail and provides logic
  8331. for placing measurements and annotations in the appropriate models. A list of
  8332. each model is provided for conveniently performing operations that apply to
  8333. every model.
  8334. @<ZoomLog private member data@>=
  8335. MeasurementModel *model_ms;
  8336. MeasurementModel *model_1s;
  8337. MeasurementModel *model_5s;
  8338. MeasurementModel *model_10s;
  8339. MeasurementModel *model_15s;
  8340. MeasurementModel *model_30s;
  8341. MeasurementModel *model_1m;
  8342. QList<MeasurementModel *> modelSet;
  8343. QHash<int, Measurement> lastMeasurement;
  8344. MeasurementModel *currentModel;
  8345. QList<int> saveTempCols;
  8346. QList<int> saveControlCols;
  8347. QList<int> saveNoteCols;
  8348. QList<int> currentColumnSet;
  8349. @ Most of the functionality this class provides is in getting measurements to
  8350. the right models. Every measurement goes to the full detail model. We also keep
  8351. track of the most recent measurement to detect the first measurement in a new
  8352. second and pass all of these on to the 1 second level of detail model. Some of
  8353. these are also passed to other models. Additionally, the models that store
  8354. coarser data strip the millisecond portion of the time.
  8355. A decision was made to present data promptly. With a high sample rate, some
  8356. might prefer an average of a few measurements near the reported time, but such
  8357. a feature does not exist in \pn{} currently.
  8358. The first measurement is always added to each model.
  8359. @<ZoomLog Implementation@>=
  8360. void ZoomLog::newMeasurement(Measurement measure, int tempcolumn)
  8361. {
  8362. if(measure.time() != QTime(0, 0, 0, 0))
  8363. {
  8364. @<Synthesize measurements for slow hardware@>@;
  8365. }
  8366. model_ms->newMeasurement(measure, tempcolumn);
  8367. if(lastMeasurement.contains(tempcolumn))
  8368. {
  8369. if(measure.time().second() !=
  8370. lastMeasurement.value(tempcolumn).time().second())
  8371. {
  8372. Measurement adjusted = measure;
  8373. QTime adjtime(0, measure.time().minute(), measure.time().second(), 0);
  8374. adjusted.setTime(adjtime);
  8375. model_1s->newMeasurement(adjusted, tempcolumn);
  8376. if(adjusted.time().second() % 5 == 0)
  8377. {
  8378. model_5s->newMeasurement(adjusted, tempcolumn);
  8379. if(adjusted.time().second() % 10 == 0)
  8380. {
  8381. model_10s->newMeasurement(adjusted, tempcolumn);
  8382. }
  8383. if(adjusted.time().second() % 15 == 0)
  8384. {
  8385. model_15s->newMeasurement(adjusted, tempcolumn);
  8386. if(adjusted.time().second() % 30 == 0)
  8387. {
  8388. model_30s->newMeasurement(adjusted, tempcolumn);
  8389. if(adjusted.time().second() == 0)
  8390. {
  8391. model_1m->newMeasurement(adjusted, tempcolumn);
  8392. }
  8393. }
  8394. }
  8395. }
  8396. }
  8397. @<Synthesize measurements for columns in set@>@;
  8398. }
  8399. else
  8400. {
  8401. @<Add the first measurement to every model@>@;
  8402. }
  8403. lastMeasurement.insert(tempcolumn, measure);
  8404. }
  8405. @ The first measurement in a series should be the epoch measurement. This
  8406. should exist in every level of detail.
  8407. @<Add the first measurement to every model@>=
  8408. MeasurementModel *m;
  8409. foreach(m, modelSet)
  8410. {
  8411. m->newMeasurement(measure, tempcolumn);
  8412. }
  8413. @ Some measurement hardware in use cannot guarantee delivery of at least one
  8414. measurement per second. This causes problems for the current |ZoomLog|
  8415. implementation as, for example, if there is no measurement at a time where
  8416. the seconds are divisible by 5, there will be no entry in that view. This can
  8417. result in situations where the |ZoomLog| at its default view of one measurement
  8418. every 30 seconds might not display any data at all aside from the first
  8419. measurement, the last measurement, and any measurement that happens to have an
  8420. annotation associated with it. The solution in this case is to synthesize
  8421. measurements so that the |ZoomLog| thinks it gets at least one measurement
  8422. every second.
  8423. Prior to version 1.8 this simply replicated the last measurement every second
  8424. until the time for the most recent measurement was reached, however this yields
  8425. problematic results when loading saved data or attempting to use this view for
  8426. manual data entry. The current behavior performs a linear interpolation which
  8427. will match the graph.
  8428. @<Synthesize measurements for slow hardware@>=
  8429. if(lastMeasurement.contains(tempcolumn))
  8430. {
  8431. if(lastMeasurement[tempcolumn].time() < measure.time())
  8432. {
  8433. QList<QTime> timelist;
  8434. QList<double> templist;
  8435. QTime z = QTime(0, 0, 0, 0);
  8436. double ptime = (double)(z.secsTo(lastMeasurement[tempcolumn].time()));
  8437. double ptemp = lastMeasurement[tempcolumn].temperature();
  8438. double ctime = (double)(z.secsTo(measure.time()));
  8439. double ctemp = measure.temperature();
  8440. for(QTime i = lastMeasurement.value(tempcolumn).time().addSecs(1); i < measure.time(); i = i.addSecs(1))
  8441. {
  8442. timelist.append(i);
  8443. double v = ((ptemp * (ctime - z.secsTo(i))) + (ctemp * (z.secsTo(i) - ptime))) / (ctime - ptime);
  8444. templist.append(v);
  8445. }
  8446. for(int i = 0; i < timelist.size(); i++)
  8447. {
  8448. Measurement synthesized = measure;
  8449. synthesized.setTime(timelist[i]);
  8450. synthesized.setTemperature(templist[i]);
  8451. newMeasurement(synthesized, tempcolumn);
  8452. }
  8453. }
  8454. }
  8455. @ New to \pn{} 1.4 is the concept of a current column set. This was added to
  8456. improve support for devices where measurements on different data series may not
  8457. arrive at exactly the same time and for multi-device configurations where
  8458. measurements from different devices are unlikely to arrive at the same time.
  8459. This can cause issues with log annotations and serialization. The solution is
  8460. to group all columns that are logically part of the same data acquisition
  8461. process and as measurements come in, the most recent measurement from other
  8462. columns can be duplicated at the new time. Two methods are responsible for
  8463. managing this measurement set. One adds a column to the set and the other
  8464. removes all columns from the set.
  8465. @<ZoomLog Implementation@>=
  8466. void ZoomLog::addToCurrentColumnSet(int column)
  8467. {
  8468. currentColumnSet.append(column);
  8469. }
  8470. void ZoomLog::clearCurrentColumnSet()
  8471. {
  8472. currentColumnSet.clear();
  8473. }
  8474. @ Replicating the measurements occurs as measurements are delivered. Note
  8475. that this code will not be called for the first measurement in each column.
  8476. @<Synthesize measurements for columns in set@>=
  8477. if(currentColumnSet.contains(tempcolumn))
  8478. {
  8479. int replicationcolumn;
  8480. foreach(replicationcolumn, currentColumnSet)
  8481. {
  8482. if(replicationcolumn != tempcolumn)
  8483. {
  8484. if(lastMeasurement.contains(replicationcolumn))
  8485. {
  8486. if(measure.time() > lastMeasurement.value(replicationcolumn).time())
  8487. {
  8488. Measurement synthetic = lastMeasurement.value(replicationcolumn);
  8489. synthetic.setTime(measure.time());
  8490. model_ms->newMeasurement(synthetic, replicationcolumn);
  8491. if(synthetic.time().second() != lastMeasurement.value(replicationcolumn).time().second())@/
  8492. {
  8493. Measurement adjusted = synthetic;
  8494. adjusted.setTime(QTime(0, synthetic.time().minute(), synthetic.time().second(), 0));
  8495. model_1s->newMeasurement(adjusted, replicationcolumn);
  8496. if(adjusted.time().second() % 5 == 0)
  8497. {
  8498. model_5s->newMeasurement(adjusted, replicationcolumn);
  8499. if(adjusted.time().second() % 10 == 0)
  8500. {
  8501. model_10s->newMeasurement(adjusted, replicationcolumn);
  8502. }
  8503. if(adjusted.time().second() % 15 == 0)
  8504. {
  8505. model_15s->newMeasurement(adjusted, replicationcolumn);
  8506. if(adjusted.time().second() % 30 == 0)
  8507. {
  8508. model_30s->newMeasurement(adjusted, replicationcolumn);
  8509. if(adjusted.time().second() == 0)
  8510. {
  8511. model_1m->newMeasurement(adjusted, replicationcolumn);
  8512. }
  8513. }
  8514. }
  8515. }
  8516. }
  8517. lastMeasurement[replicationcolumn] = synthetic;
  8518. }
  8519. }
  8520. }
  8521. }
  8522. }
  8523. @ Just as the first measurement should exist at every level of detail, so should
  8524. any annotations. The measurement models will, when presented with an annotation,
  8525. apply it to the most recently entered measurement in the specified data series.
  8526. This presents a problem for the coarser views as the data point the annotation
  8527. belongs to most likely does not exist in that view. Furthermore, the model as it
  8528. is currently written will overwrite annotations that already exist on a
  8529. measurement if it is still the most recently entered. When collecting samples
  8530. during profile development, it is common to produce several annotations in a
  8531. short amount of time. The most useful thing to do in such a case is to add the
  8532. most recent measurement to each model and then apply the annotation. This, of
  8533. course, should only be done if there is a most recent measurement. An annotation
  8534. regarding the starting condition of the roaster should apply to the yet to be
  8535. recorded time zero measurement.
  8536. Note that only the value from the temperature column specified is displayed in
  8537. the row with the annotation. It would be better to check the full detail model
  8538. to determine if there are other measurements at the annotation time and present
  8539. these as well. Another possibility in the case of data not existing in other
  8540. temperature columns would be to interpolate a value from the existing data in
  8541. these columns, however this is potentially challenging as I would want to keep
  8542. true measurements distinct from estimations.
  8543. @<ZoomLog Implementation@>=
  8544. void ZoomLog::newAnnotation(QString annotation, int tempcolumn,
  8545. int annotationcolumn)
  8546. {
  8547. model_ms->newAnnotation(annotation, tempcolumn, annotationcolumn);
  8548. MeasurementModel *m;
  8549. if(lastMeasurement.contains(tempcolumn))
  8550. {
  8551. foreach(m, modelSet)
  8552. {
  8553. m->newMeasurement(lastMeasurement.value(tempcolumn), tempcolumn);
  8554. }
  8555. }
  8556. foreach(m, modelSet)
  8557. {
  8558. m->newAnnotation(annotation, tempcolumn, annotationcolumn);
  8559. }
  8560. }
  8561. @ As measurements are added to the model, the model will emit rowChanged
  8562. signals. These signals are connected to a function here that will attempt to
  8563. scroll the view to keep the most recently entered data in the center of the
  8564. view.
  8565. @<ZoomLog Implementation@>=
  8566. void ZoomLog::centerOn(int row)
  8567. {
  8568. scrollTo(currentModel->index(row, 0), QAbstractItemView::PositionAtCenter);
  8569. }
  8570. @ Once we are done with the data in the table, we want to clear it to prepare
  8571. for new data. This also clears the lists holding the output columns to use when
  8572. saving data.
  8573. @<ZoomLog Implementation@>=
  8574. void ZoomLog::clear()
  8575. {
  8576. MeasurementModel *m;
  8577. foreach(m, modelSet)
  8578. {
  8579. m->clear();
  8580. }
  8581. lastMeasurement.clear();
  8582. saveTempCols.clear();
  8583. saveControlCols.clear();
  8584. saveNoteCols.clear();
  8585. }
  8586. @ These are depreciated methods originally written to assist in serializing
  8587. model data prior to the introduction of the |XMLOutput| class. These methods are
  8588. likely to be removed in a future version of the program.
  8589. @<ZoomLog Implementation@>=
  8590. QVariant ZoomLog::data(int row, int column) const
  8591. {
  8592. return model_ms->data(model_ms->index(row, column, QModelIndex()),
  8593. Qt::DisplayRole);
  8594. }
  8595. int ZoomLog::rowCount()
  8596. {
  8597. return model_ms->rowCount();
  8598. }
  8599. @ This method initializes an |XMLOutput| instance, passes the columns that we
  8600. would like to save to that object, and uses it to write an XML file with the
  8601. desired data to the specified device.
  8602. Since the output format does not currently specify a unit, there is an
  8603. assumption that the XML output will always have measurements in Fahrenheit. If
  8604. the model is not currently displaying measurements in Fahrenheit, it is asked to
  8605. do so before writing the XML data. User preference is restored after the XML
  8606. data has been written. Since this change is only performed on |model_ms|, most
  8607. users will never notice this.
  8608. @<ZoomLog Implementation@>=
  8609. bool ZoomLog::saveXML(QIODevice *device)
  8610. {
  8611. Units::Unit prevUnits = model_ms->displayUnits();
  8612. if(prevUnits != Units::Fahrenheit)
  8613. {
  8614. model_ms->setDisplayUnits(Units::Fahrenheit);
  8615. }
  8616. XMLOutput writer(model_ms, device, 0);
  8617. int c;
  8618. foreach(c, saveTempCols)
  8619. {
  8620. writer.addTemperatureColumn(model_ms->headerData(c, Qt::Horizontal).
  8621. toString(), c);
  8622. }
  8623. foreach(c, saveControlCols)
  8624. {
  8625. writer.addControlColumn(model_ms->headerData(c, Qt::Horizontal).
  8626. toString(), c);
  8627. }
  8628. foreach(c, saveNoteCols)
  8629. {
  8630. writer.addAnnotationColumn(model_ms->headerData(c, Qt::Horizontal).
  8631. toString(), c);
  8632. }
  8633. bool retval = writer.output();
  8634. if(prevUnits != Units::Fahrenheit)
  8635. {
  8636. model_ms->setDisplayUnits(prevUnits);
  8637. }
  8638. return retval;
  8639. }
  8640. @ This method is similar to |saveXML()|. The main difference is that CSV data is
  8641. exported instead of XML.
  8642. @<ZoomLog Implementation@>=
  8643. bool ZoomLog::saveCSV(QIODevice *device)
  8644. {
  8645. CSVOutput writer(currentModel, device, 0);
  8646. int c;
  8647. foreach(c, saveTempCols)
  8648. {
  8649. writer.addTemperatureColumn(model_ms->headerData(c, Qt::Horizontal).
  8650. toString(), c);
  8651. }
  8652. foreach(c, saveControlCols)
  8653. {
  8654. writer.addControlColumn(model_ms->headerData(c, Qt::Horizontal).
  8655. toString(), c);
  8656. }
  8657. foreach(c, saveNoteCols)
  8658. {
  8659. writer.addAnnotationColumn(model_ms->headerData(c, Qt::Horizontal).
  8660. toString(), c);
  8661. }
  8662. return writer.output();
  8663. }
  8664. @ Several little functions, all alike\nfnote{If you get the reference, you may
  8665. enjoy reading another \cweb{} program:\par\indent\pdfURL{%
  8666. http://www-cs-staff.stanford.edu/$\sim$uno/programs/advent.w.gz}
  8667. {http://www-cs-staff.stanford.edu/~uno/programs/advent.w.gz}}, are used to
  8668. switch the view from one level of detail to another.
  8669. @<ZoomLog Implementation@>=
  8670. void ZoomLog::switchLOD(MeasurementModel *m)
  8671. {
  8672. disconnect(currentModel, SIGNAL(rowChanged(int)), this, 0);
  8673. setModel(m);
  8674. currentModel = m;
  8675. connect(currentModel, SIGNAL(rowChanged(int)), this, SLOT(centerOn(int)));
  8676. }
  8677. void ZoomLog::LOD_ms()
  8678. {
  8679. switchLOD(model_ms);
  8680. }
  8681. void ZoomLog::LOD_1s()
  8682. {
  8683. switchLOD(model_1s);
  8684. }
  8685. void ZoomLog::LOD_5s()
  8686. {
  8687. switchLOD(model_5s);
  8688. }
  8689. void ZoomLog::LOD_10s()
  8690. {
  8691. switchLOD(model_10s);
  8692. }
  8693. void ZoomLog::LOD_15s()
  8694. {
  8695. switchLOD(model_15s);
  8696. }
  8697. void ZoomLog::LOD_30s()
  8698. {
  8699. switchLOD(model_30s);
  8700. }
  8701. void ZoomLog::LOD_1m()
  8702. {
  8703. switchLOD(model_1m);
  8704. }
  8705. @ It can be useful to display temperature measurements in various units. To do
  8706. so, we simply tell all of the models which unit to provide data in. It is also
  8707. possible to obtain the currently selected unit.
  8708. @<ZoomLog Implementation@>=
  8709. void ZoomLog::setDisplayUnits(Units::Unit scale)
  8710. {
  8711. model_ms->setDisplayUnits(scale);
  8712. model_1s->setDisplayUnits(scale);
  8713. model_5s->setDisplayUnits(scale);
  8714. model_10s->setDisplayUnits(scale);
  8715. model_15s->setDisplayUnits(scale);
  8716. model_30s->setDisplayUnits(scale);
  8717. model_1m->setDisplayUnits(scale);
  8718. }
  8719. Units::Unit ZoomLog::displayUnits()
  8720. {
  8721. return model_ms->displayUnits();
  8722. }
  8723. @ For convenience, a method is provided for returning a string containing the
  8724. time of the last inserted measurement in a given data series.
  8725. @<ZoomLog Implementation@>=
  8726. QString ZoomLog::lastTime(int series)
  8727. {
  8728. Measurement measure = lastMeasurement.value(series);
  8729. QTime time = measure.time();
  8730. return time.toString("h:mm:ss.zzz");
  8731. }
  8732. @ This just leaves the initial table setup.
  8733. @<ZoomLog Implementation@>=
  8734. ZoomLog::ZoomLog() : QTableView(NULL), model_ms(new MeasurementModel(this)),
  8735. model_1s(new MeasurementModel(this)),@/ model_5s(new MeasurementModel(this)),
  8736. model_10s(new MeasurementModel(this)),@/ model_15s(new MeasurementModel(this)),
  8737. model_30s(new MeasurementModel(this)),@/ model_1m(new MeasurementModel(this))@/
  8738. {@/
  8739. setEditTriggers(QAbstractItemView::NoEditTriggers);
  8740. setSelectionMode(QAbstractItemView::NoSelection);
  8741. modelSet << model_ms << model_1s << model_5s << model_10s << model_15s <<
  8742. model_30s << model_1m;
  8743. currentModel = model_30s;
  8744. setModel(currentModel);
  8745. connect(currentModel, SIGNAL(rowChanged(int)), this, SLOT(centerOn(int)));
  8746. connect(horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
  8747. this, SLOT(persistColumnResize(int, int, int)));
  8748. connect(horizontalHeader(), SIGNAL(sectionCountChanged(int, int)),
  8749. this, SLOT(restoreColumnWidths()));
  8750. }
  8751. @ A new method was added to this class for version 1.0.7. This allows header
  8752. data to be set on the log and have it propagate to the model set. The longer
  8753. term plan involves removing the hard coding of some of the header data.
  8754. @<ZoomLog Implementation@>=
  8755. void ZoomLog::setHeaderData(int section, QString text)
  8756. {
  8757. MeasurementModel *m;
  8758. foreach(m, modelSet)
  8759. {
  8760. m->setHeaderData(section, Qt::Horizontal, QVariant(text));
  8761. }
  8762. }
  8763. @ As of version 1.2.3, these methods replace similar methods added for version
  8764. 1.0.8. The main difference is that it is now possible to save multiple data
  8765. series to the same output document.
  8766. Starting in version 1.6 it is possible to save control columns. These should
  8767. contain unitless data which should remain unaffected by the current displayed
  8768. unit.
  8769. @<ZoomLog Implementation@>=
  8770. void ZoomLog::addOutputTemperatureColumn(int column)
  8771. {
  8772. saveTempCols.append(column);
  8773. }
  8774. void ZoomLog::addOutputControlColumn(int column)
  8775. {
  8776. saveControlCols.append(column);
  8777. }
  8778. void ZoomLog::addOutputAnnotationColumn(int column)
  8779. {
  8780. saveNoteCols.append(column);
  8781. }
  8782. void ZoomLog::clearOutputColumns()
  8783. {
  8784. saveTempCols.clear();
  8785. saveControlCols.clear();
  8786. saveNoteCols.clear();
  8787. }
  8788. @ Starting in version 1.4 two methods have been introduced which are used to
  8789. save and restore column widths.
  8790. @<ZoomLog Implementation@>=
  8791. void ZoomLog::persistColumnResize(int column, int, int newsize)
  8792. {
  8793. @<Save updated column size@>@;
  8794. }
  8795. void ZoomLog::restoreColumnWidths()
  8796. {
  8797. @<Restore table column widths@>@;
  8798. }
  8799. void ZoomLog::setVisible(bool visibility)
  8800. {
  8801. QTableView::setVisible(visibility);
  8802. }
  8803. void ZoomLog::showEvent(QShowEvent *)
  8804. {
  8805. @<Restore table column widths@>@;
  8806. }
  8807. @ The |ZoomLog| class is one of the more complicated classes to expose to the
  8808. scripting engine. In addition to a script constructor, we also need functions
  8809. for saving and restoring the state of the display and functions for saving data
  8810. from the log in the supported formats.
  8811. @<Function prototypes for scripting@>=
  8812. void setZoomLogProperties(QScriptValue value, QScriptEngine *engine);
  8813. QScriptValue constructZoomLog(QScriptContext *context, QScriptEngine *engine);
  8814. QScriptValue ZoomLog_saveXML(QScriptContext *context, QScriptEngine *engine);
  8815. QScriptValue ZoomLog_saveCSV(QScriptContext *context, QScriptEngine *engine);
  8816. QScriptValue ZoomLog_saveState(QScriptContext *context, QScriptEngine *engine);
  8817. QScriptValue ZoomLog_restoreState(QScriptContext *context,
  8818. QScriptEngine *engine);
  8819. QScriptValue ZoomLog_lastTime(QScriptContext *context, QScriptEngine *engine);
  8820. QScriptValue ZoomLog_saveTemporary(QScriptContext *context,
  8821. QScriptEngine *engnie);
  8822. QScriptValue ZoomLog_setDisplayUnits(QScriptContext *context,
  8823. QScriptEngine *engine);
  8824. @ Of these, the global object only needs to know about the constructor.
  8825. @<Set up the scripting engine@>=
  8826. constructor = engine->newFunction(constructZoomLog);
  8827. value = engine->newQMetaObject(&ZoomLog::staticMetaObject, constructor);
  8828. engine->globalObject().setProperty("ZoomLog", value);
  8829. @ The script constructor sets properties on the newly created object to allow
  8830. the other functions to be called.
  8831. @<Functions for scripting@>=
  8832. QScriptValue constructZoomLog(QScriptContext *, QScriptEngine *engine)@/
  8833. {@/
  8834. QScriptValue object = engine->newQObject(new ZoomLog);
  8835. setZoomLogProperties(object, engine);
  8836. return object;@/
  8837. }
  8838. void setZoomLogProperties(QScriptValue value, QScriptEngine *engine)
  8839. {
  8840. setQTableViewProperties(value, engine);
  8841. value.setProperty("saveXML", engine->newFunction(ZoomLog_saveXML));
  8842. value.setProperty("saveCSV", engine->newFunction(ZoomLog_saveCSV));
  8843. value.setProperty("saveState", engine->newFunction(ZoomLog_saveState));
  8844. value.setProperty("restoreState",
  8845. engine->newFunction(ZoomLog_restoreState));
  8846. value.setProperty("lastTime", engine->newFunction(ZoomLog_lastTime));
  8847. value.setProperty("saveTemporary",
  8848. engine->newFunction(ZoomLog_saveTemporary));
  8849. value.setProperty("setDisplayUnits", engine->newFunction(ZoomLog_setDisplayUnits));
  8850. }
  8851. @ The functions for saving data are simple wrappers around the corresponding
  8852. calls in |ZoomLog|, except for a function added for saving data to a temporary
  8853. file. The last provides the name of the file saved for use in copying that data
  8854. to a database entry.
  8855. @<Functions for scripting@>=
  8856. QScriptValue ZoomLog_saveXML(QScriptContext *context, QScriptEngine *engine)
  8857. {
  8858. ZoomLog *self = getself<ZoomLog *>(context);
  8859. bool retval = self->saveXML(argument<QIODevice *>(0, context));
  8860. return QScriptValue(engine, retval);
  8861. }
  8862. QScriptValue ZoomLog_saveCSV(QScriptContext *context, QScriptEngine *engine)
  8863. {
  8864. ZoomLog *self = getself<ZoomLog *>(context);
  8865. bool retval = self->saveCSV(argument<QIODevice *>(0, context));
  8866. return QScriptValue(engine, retval);
  8867. }
  8868. QScriptValue ZoomLog_saveTemporary(QScriptContext *context,
  8869. QScriptEngine *engine)
  8870. {
  8871. ZoomLog *self = getself<ZoomLog *>(context);
  8872. QString filename = QDir::tempPath();
  8873. filename.append("/");
  8874. filename.append(QUuid::createUuid().toString());
  8875. filename.append(".xml");
  8876. QFile *file = new QFile(filename);
  8877. self->saveXML(file);
  8878. file->close();
  8879. delete file;
  8880. return QScriptValue(engine, filename);
  8881. }
  8882. @ The remaining functions are convenience functions for use with the scripting
  8883. engine. One will save the column widths to a |QSettings| object. Another will
  8884. restore the column widths from settings. Finally, there is a function for
  8885. obtaining a string representation of the most recent measurement from a data
  8886. series.
  8887. \danger There are a couple of problems with these functions. First, the body of
  8888. these functions would probably be better off as methods in the |ZoomLog| class
  8889. proper, either as slots or |Q_INVOKABLE| so the special scripting functions
  8890. could be eliminated. Second, rather than polluting the settings with separate
  8891. entries for each column, it would probably be better to store all of these
  8892. values in an array.\endanger
  8893. |ZoomLog_saveState()| was changed in version 1.2.3 to not save a new value for
  8894. the column width if that width is |0|. This was done mainly to ease debugging.
  8895. Similarly, |ZoomLog_restoreState()| picks a new default value when |0| is
  8896. encountered.
  8897. @<Functions for scripting@>=
  8898. QScriptValue ZoomLog_saveState(QScriptContext *context, QScriptEngine *)
  8899. {
  8900. ZoomLog *self = getself<@[ZoomLog *@]>(context);
  8901. QString key = argument<QString>(0, context);
  8902. int columns = argument<int>(1, context);
  8903. QSettings settings;
  8904. for(int i = 0; i < columns; i++)
  8905. {
  8906. if(self->columnWidth(i))
  8907. {
  8908. settings.beginGroup(key);
  8909. settings.setValue(QString("%1").arg(i), self->columnWidth(i));
  8910. settings.endGroup();
  8911. }
  8912. }
  8913. return QScriptValue();
  8914. }
  8915. QScriptValue ZoomLog_restoreState(QScriptContext *context, QScriptEngine *)
  8916. {
  8917. ZoomLog *self = getself<@[ZoomLog *@]>(context);
  8918. QString key = argument<QString>(0, context);
  8919. int columns = argument<int>(1, context);
  8920. QSettings settings;
  8921. for(int i = 0; i < columns; i++)
  8922. {
  8923. settings.beginGroup(key);
  8924. self->setColumnWidth(i,
  8925. settings.value(QString("%1").arg(i), 80).toInt());
  8926. if(settings.value(QString("%1").arg(i), 80).toInt() == 0)
  8927. {
  8928. self->setColumnWidth(i, 80);
  8929. }
  8930. settings.endGroup();
  8931. }
  8932. return QScriptValue();
  8933. }
  8934. QScriptValue ZoomLog_lastTime(QScriptContext *context, QScriptEngine *engine)
  8935. {
  8936. ZoomLog *self = getself<@[ZoomLog *@]>(context);
  8937. return QScriptValue(engine, self->lastTime(argument<int>(0, context)));
  8938. }
  8939. @ There seems to be a bad interaction when enumerated value types as used as
  8940. the argument to slot methods called through QtScript. Script code that attempts
  8941. to make use of the enumeration appears to get the value without any type
  8942. information. When attempting to use that value as an argument the meta-object
  8943. system cannot find an appropriate match and the script just hangs silently.
  8944. The solution is to wrap such methods in the script bindings and explicitly cast
  8945. the argument value to the enumerated type. This looks stupid but it works.
  8946. @<Functions for scripting@>=
  8947. QScriptValue ZoomLog_setDisplayUnits(QScriptContext *context, QScriptEngine *)
  8948. {
  8949. ZoomLog *self = getself<@[ZoomLog *@]>(context);
  8950. self->setDisplayUnits((Units::Unit)argument<int>(0, context));
  8951. return QScriptValue();
  8952. }
  8953. @* A model for roasting data.
  8954. \noindent Qt provides a tool called the model view architecture. This provides a
  8955. uniform interface allowing different types of model classes to work with
  8956. different types of view classes without either needing to know implementation
  8957. details of the other. \pn{} provides the |MeasurementModel| as a specialization
  8958. of |QAbstractItemModel| for use in this architecture.
  8959. @<Class declarations@>=
  8960. class MeasurementList;@/
  8961. class MeasurementModel : public QAbstractItemModel@/
  8962. {@t\1@>@/
  8963. Q_OBJECT@;
  8964. Units::Unit unit;
  8965. QList<MeasurementList *> *entries;
  8966. QStringList *hData;
  8967. int colcount;
  8968. QHash<int, int> *lastTemperature;
  8969. QList<MeasurementList *>::iterator@, lastInsertion;
  8970. QHash<int, bool> *controlColumns;
  8971. public:@/
  8972. MeasurementModel(QObject *parent = NULL);
  8973. ~MeasurementModel();
  8974. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  8975. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  8976. bool setHeaderData(int section, Qt::Orientation orientation,
  8977. const QVariant &value,@|int role = Qt::DisplayRole);
  8978. QVariant data(const QModelIndex &index, int role) const;
  8979. bool setData(const QModelIndex &index, const QVariant &value,
  8980. int role = Qt::EditRole);
  8981. Qt::ItemFlags flags(const QModelIndex &index) const;
  8982. QVariant headerData(int section, Qt::Orientation orientation,
  8983. int role = Qt::DisplayRole) const;
  8984. QModelIndex index(int row, int column,
  8985. const QModelIndex &parent = QModelIndex()) const;
  8986. QModelIndex parent(const QModelIndex &index) const;
  8987. Units::Unit displayUnits();@/
  8988. @t\4@>public slots@t\kern-3pt@>:@/
  8989. void newMeasurement(Measurement measure, int tempcolumn);
  8990. void newAnnotation(QString annotation, int tempcolumn,
  8991. int annotationColumn);
  8992. void clear();
  8993. void setDisplayUnits(Units::Unit scale);
  8994. signals:@/
  8995. void rowChanged(int);@t\2@>@/
  8996. }@t\kern-3pt@>;
  8997. @ The measurement model stores its data in a list of measurement lists. This
  8998. allows the model to store as many sets of data as needed. In order to keep
  8999. measurements in the model sorted by time, the measurement list adds two
  9000. comparison functions.
  9001. @<Class declarations@>=
  9002. class MeasurementList : public QVariantList@/
  9003. {@t\1@>@/
  9004. @t\4@>public:@/
  9005. bool operator<(const MeasurementList &other) const;
  9006. bool operator==(const MeasurementList &other) const;@t\2@>@/
  9007. }@t\kern-3pt@>;
  9008. @ The overload of |<| checks if the time in one list (always stored in the first
  9009. column) is less than the time stored in the second. The overload of |==| is used
  9010. in an optimization that allows us to skip the search procedure on model
  9011. insertion.
  9012. @<MeasurementModel Implementation@>=
  9013. bool MeasurementList::operator<(const MeasurementList &other) const
  9014. {
  9015. return this->first().toTime() < other.first().toTime();
  9016. }
  9017. bool MeasurementList::operator==(const MeasurementList &other) const
  9018. {
  9019. return this->first().toTime() == other.first().toTime();
  9020. }
  9021. @ The |MeasurementModel| class extends the |QAbstractItemModel| class to work
  9022. better with measurements and annotations that are passed around in \pn{}. Many
  9023. of the class methods are required because of that choice. For example, the
  9024. parent function which is never used directly:
  9025. @<MeasurementModel Implementation@>=
  9026. QModelIndex MeasurementModel::parent(const QModelIndex&) const
  9027. {
  9028. return QModelIndex();
  9029. }
  9030. @ Perhaps the most complicated part of this class is the code for dealing with a
  9031. new measurement. This is complicated by the requirement to insert measurements
  9032. while keeping the model sorted by time.
  9033. @<MeasurementModel Implementation@>=
  9034. void MeasurementModel::newMeasurement(Measurement measure, int tempcolumn)
  9035. {
  9036. if(measure.scale() == Units::Unitless)
  9037. {
  9038. controlColumns->insert(tempcolumn, true);
  9039. }
  9040. else
  9041. {
  9042. controlColumns->insert(tempcolumn, false);
  9043. }
  9044. MeasurementList *temp;
  9045. temp = new MeasurementList;
  9046. temp->append(QVariant(measure.time()));
  9047. @<Find the insertion point@>@;
  9048. MeasurementList *newEntry;
  9049. int insertion;
  9050. if(i != entries->end())
  9051. {
  9052. insertion = entries->indexOf(*i);
  9053. if((*i)->first().toTime() == measure.time())
  9054. {
  9055. @<Insert a new measurement at an existing time@>@;
  9056. }
  9057. else
  9058. {
  9059. @<Insert a new measurement somewhere else@>@;
  9060. }
  9061. }
  9062. else
  9063. {
  9064. @<Append a measurement@>@;
  9065. }
  9066. if(tempcolumn >= colcount)
  9067. {
  9068. colcount = tempcolumn + 1;
  9069. }
  9070. emit rowChanged(insertion);
  9071. delete temp;
  9072. }
  9073. @ To find the insertion point for new measurements we use a binary search of the
  9074. existing data. The code below is a direct adaptation of Program B\nfnote{%
  9075. \underbar{The Art of Computer Programming} Volume 3 Sorting and Searching 2nd
  9076. ed. (Knuth, 1997) Section 6.2.1: Searching an Ordered Table} modified to use
  9077. list iterators and control structures more familiar to \CPLUSPLUS/programmers
  9078. rather than {\mc MIX} machine codes. When the loop exits |i| is the insertion
  9079. point.
  9080. \medskip
  9081. \centerline{\includegraphics{search}}
  9082. \smallskip
  9083. \centerline{Figure \secno: Binary Search}
  9084. \medskip
  9085. @<Find the insertion point@>=
  9086. @<Scan from most recent insertion@>@;
  9087. if(quickscan == false)
  9088. {
  9089. i = entries->begin();
  9090. QList<MeasurementList *>::iterator@, u = entries->end();
  9091. QList<MeasurementList *>::iterator@, midpoint;
  9092. int n = u - i;
  9093. int rA;
  9094. while(n > 0)@/
  9095. {
  9096. rA = n>>1; /* |rA = |~$\bigl\lfloor{n\over2}\bigr\rfloor$ */
  9097. midpoint = i + rA;
  9098. if(**midpoint < *temp)@/
  9099. {
  9100. i = midpoint + 1;
  9101. n -= rA + 1;
  9102. }
  9103. else@/
  9104. {
  9105. n = rA;
  9106. }
  9107. }
  9108. }
  9109. @ The binary search, while correct, is not a particularly optimal choice for
  9110. this application. While the average running time for this is on the order of
  9111. $\ln N$ when each insertion point is equally likely, the reality of this
  9112. application is that insertions will likely be at the beginning of the list, at
  9113. the point of the most recent insertion, or a short distance from the most recent
  9114. insertion. By first considering the possibility that the measurement should be
  9115. inserted at or near the most recent measurement, shorter, more constant running
  9116. times as $N$ increases can be obtained.
  9117. To do this, when the number of measurements in the list is above a small number
  9118. which must be greater than 1, we check first if the insertion point is at the
  9119. last insertion (the |<| comparison fails and we do an |==| comparison before
  9120. giving up), then we check a small number of rows for either the end of the list,
  9121. in which case the insertion point is at the end, or for a point at which the |<|
  9122. comparison fails. If neither condition holds for a small number of comparisons
  9123. we resort to the binary search.
  9124. Performance measurements with this modification compared with previous versions
  9125. shows that this provides a huge performance boost.
  9126. @<Scan from most recent insertion@>=
  9127. @[QList<MeasurementList *>::iterator@, i@] = lastInsertion;@/
  9128. bool quickscan = false;@/
  9129. if(entries->size() > 5)@/
  9130. {@t\1@>@/
  9131. if(**i < *temp)@/
  9132. {@t\1@>@/
  9133. i += 1;@/
  9134. for(int j = 10; j > 0; j--)@/
  9135. {@t\1@>@/
  9136. if(i != entries->end())@/
  9137. {@t\1@>@/
  9138. if(**i < *temp)@/
  9139. {
  9140. i += 1;
  9141. }@/
  9142. else@/
  9143. {@t\1@>@/
  9144. quickscan = true;
  9145. break;@t\2@>@/
  9146. }@t\2@>@/
  9147. }@/
  9148. else@/
  9149. {@t\1@>@/
  9150. quickscan = true;
  9151. break;@t\2@>@/
  9152. }@t\2@>@/
  9153. }@t\2@>@/
  9154. }@/
  9155. else@/
  9156. {@t\1@>@/
  9157. if(**i == *temp)@/
  9158. {@t\1@>@/
  9159. quickscan = true;@t\2@>@/
  9160. }@t\2@>@/
  9161. }@t\2@>@/
  9162. }
  9163. @ If the chosen insertion point is at an existing time, we don'@q'@>t need to
  9164. worry about inserting rows. There may be a need to increase the size of the
  9165. measurement list to accept an entry in a new data series.
  9166. @<Insert a new measurement at an existing time@>=
  9167. if((*i)->size() < tempcolumn + 1)
  9168. {
  9169. for(int j = (*i)->size() - 1; j < tempcolumn + 1; j++)
  9170. {
  9171. (*i)->append(QVariant());
  9172. }
  9173. }
  9174. (*i)->replace(tempcolumn, measure);
  9175. lastInsertion = i;
  9176. emit dataChanged(createIndex(insertion, tempcolumn),
  9177. createIndex(insertion, tempcolumn));
  9178. lastTemperature->insert(tempcolumn, insertion);
  9179. @ If the measurement is not past the end of the existing data and the insertion
  9180. point has a different time, we need to use |beginInsertRows()| and
  9181. |endInsertRows()| to notify any attached view that a new row will be added.
  9182. @<Insert a new measurement somewhere else@>=
  9183. beginInsertRows(QModelIndex(), insertion, insertion);
  9184. newEntry = new MeasurementList;
  9185. newEntry->append(QVariant(measure.time()));
  9186. for(int j = 0; j < tempcolumn + 1; j++)
  9187. {
  9188. newEntry->append(QVariant());
  9189. }
  9190. newEntry->replace(tempcolumn, measure);
  9191. lastInsertion = entries->insert(i, newEntry);
  9192. endInsertRows();
  9193. lastTemperature->insert(tempcolumn, insertion);
  9194. @ If the insertion point is past the end of the existing data, a new row should
  9195. be appended to the data. This only needs to be a separate case to prevent the
  9196. comparison with a nonexistent entry. This is very similar to the case of
  9197. inserting at a new time anywhere else.
  9198. @<Append a measurement@>=
  9199. insertion = entries->size();@/
  9200. @<Insert a new measurement somewhere else@>
  9201. @ The other bit of code that'@q'@>s a little bit more complicated than other
  9202. parts of the class handles adding annotations to the data. Two signals are
  9203. emitted in this method. The |dataChanged| signal is expected by view classes
  9204. that can use this model. The |rowChanged| signal is used by |ZoomLog| to scroll
  9205. the view to the row the annotation has been added to. This is mainly useful
  9206. when loading a target profile and entering the first annotation prior to
  9207. starting the batch.
  9208. @<MeasurementModel Implementation@>=
  9209. void MeasurementModel::newAnnotation(QString annotation, int tempcolumn,@|
  9210. int annotationColumn)
  9211. {
  9212. int r;
  9213. if(lastTemperature->contains(tempcolumn))
  9214. {
  9215. r = lastTemperature->value(tempcolumn);
  9216. }
  9217. else
  9218. {
  9219. r = 0;
  9220. }
  9221. if(r == 0 && entries->size() == 0)
  9222. {
  9223. @<Create the first row@>@;
  9224. }
  9225. MeasurementList *row = entries->at(r);
  9226. if(row->size() <= annotationColumn)
  9227. {
  9228. for(int i = row->size() - 1; i < annotationColumn + 1; i++)
  9229. {
  9230. row->append(QVariant());
  9231. }
  9232. }
  9233. row->replace(annotationColumn, annotation);
  9234. emit dataChanged(createIndex(r, annotationColumn),
  9235. createIndex(r, annotationColumn));
  9236. emit rowChanged(r);
  9237. if(annotationColumn > colcount - 1)
  9238. {
  9239. colcount = annotationColumn + 1;
  9240. }
  9241. }
  9242. @ There is no need to further complicate the function by adding the annotation
  9243. when the first row is created.
  9244. @<Create the first row@>=
  9245. beginInsertRows(QModelIndex(), 0, 0);
  9246. MeasurementList *newEntry = new MeasurementList;
  9247. newEntry->append(QVariant(QTime(0, 0, 0, 0)));
  9248. entries->append(newEntry);
  9249. endInsertRows();
  9250. @ Clearing the model data is a simple matter of deleting every row, remembering
  9251. to let any attached views know that we are doing this, and resetting the number
  9252. of columns.
  9253. @<MeasurementModel Implementation@>=
  9254. void MeasurementModel::clear()
  9255. {
  9256. beginRemoveRows(QModelIndex(), 0, entries->size());
  9257. while(entries->size() != 0)
  9258. {
  9259. MeasurementList *row = entries->takeFirst();
  9260. delete row;
  9261. }
  9262. endRemoveRows();
  9263. colcount = hData->size();
  9264. lastTemperature->clear();
  9265. reset();
  9266. }
  9267. @ While these methods for adding measurements and annotations are fine when
  9268. recording a stream of measurements, either from the |DAQ| or when loading saved
  9269. data, there are also cases where we'@q'@>d like to edit the data in the model
  9270. directly from the table view. For this, we need to reimplement |setData()|.
  9271. Note that editing from the |ZoomLog| has never been supported. While stream
  9272. inserted data currently preserves all properties of inserted measurements,
  9273. using |setData| it is possible to insert a numeric value as if it were a
  9274. measurement. Such an entry will not have any additional information associated
  9275. and cannot be expected to exhibit behavior implemented through the use of that
  9276. extra information.
  9277. Very little input checking is done here. Editable views may want to place
  9278. delegates\nfnote{Qt 4.4: Delegate Classes\par\indent\hbox{%
  9279. \pdfURL{http://doc.trolltech.com/4.4/model-view-delegate.html}{%
  9280. http://doc.trolltech.com/4.4/model-view-delegate.html}}} on the columns to make
  9281. editing the data easier and less error prone.
  9282. @<MeasurementModel Implementation@>=
  9283. bool MeasurementModel::setData(const QModelIndex &index,
  9284. const QVariant &value, int role)@t\2\2@>@/
  9285. {@t\1@>@/
  9286. if(role != Qt::EditRole && role != Qt::DisplayRole)@/
  9287. {@t\1@>@/
  9288. return false;@t\2@>@/
  9289. }@/
  9290. @<Check that the index is valid@>@;
  9291. if(!valid)@/
  9292. {@t\1@>@/
  9293. return false;@t\2@>@/
  9294. }@/
  9295. MeasurementList *row = entries->at(index.row());
  9296. if(index.column() >= row->size())
  9297. {
  9298. @<Expand the row to prepare for new data@>@;
  9299. }
  9300. if(index.column() == 0)
  9301. {
  9302. @<Edit data in the time column@>@;
  9303. }
  9304. else
  9305. {
  9306. @<Edit data in other columns@>@;
  9307. }
  9308. return true;@t\2@>@/
  9309. }
  9310. @ There is no sense in attempting to edit the data if there isn'@q'@>t any data
  9311. available to edit. This check is also used when retrieving data from the model.
  9312. @<Check that the index is valid@>=
  9313. bool valid = false;
  9314. if(index.isValid())@/
  9315. {@t\1@>@/
  9316. if(index.row() < entries->size())@/
  9317. {@t\1@>@/
  9318. if(index.column() < colcount)@/
  9319. {@t\1@>@/
  9320. valid = true;@t\2@>@/
  9321. }@t\2@>@/
  9322. }@t\2@>@/
  9323. }
  9324. @ When editing data, there might not be anything where we want to add the data.
  9325. For example, adding an annotation to an otherwise unannotated measurement. This
  9326. is fine, but we need to expand the row instead of inserting data out of bounds.
  9327. @<Expand the row to prepare for new data@>=
  9328. for(int i = row->size() - 1; i < index.column(); i++)
  9329. {
  9330. row->append(QVariant());
  9331. }
  9332. @ Changing time data must be considered separately from other data. As the model
  9333. keeps itself sorted based on the time field, allowing the user to get the model
  9334. data out of order would result in poorly defined behavior later. Our approach is
  9335. to remove the row from the model temporarily then reuse the code from
  9336. |newMeasurement()| to find the new insertion point. No attempt is made to merge
  9337. the contents from two rows with identical times, but an attempt is made to not
  9338. be too rigid in what we expect the user to enter. If an invalid time is entered,
  9339. we give up and leave the data as we found it.
  9340. @<Edit data in the time column@>=
  9341. QTime time;@/
  9342. if(!(time = QTime::fromString(value.toString(), "m:s.z")).isValid())@/
  9343. {@t\1@>@/
  9344. if(!(time = QTime::fromString(value.toString(), "m:s")).isValid())@/
  9345. {@t\1@>@/
  9346. return false;@t\2@>@/
  9347. }@t\2@>@/
  9348. }@/
  9349. row = entries->takeAt(index.row());
  9350. row->replace(index.column(), QVariant(time));
  9351. MeasurementList *temp = row;
  9352. @<Find the insertion point@>@;
  9353. entries->insert(i, row);
  9354. int newRow = entries->indexOf(*i);
  9355. if(newRow < index.row())@/
  9356. {
  9357. emit dataChanged(createIndex(newRow, index.column()), index);
  9358. }
  9359. else@/
  9360. {
  9361. emit dataChanged(index, createIndex(newRow, index.column()));
  9362. }
  9363. @ Data in other columns is a little easier to handle.
  9364. @<Edit data in other columns@>=
  9365. row->replace(index.column(), value);
  9366. emit dataChanged(index, index);
  9367. @ As it has already been established that the first column is always considered
  9368. the time of the measurement, this assumption can be built into the model
  9369. constructor.
  9370. @<MeasurementModel Implementation@>=
  9371. MeasurementModel::MeasurementModel(QObject *parent) : QAbstractItemModel(parent),
  9372. unit(Units::Fahrenheit), hData(new QStringList),
  9373. lastTemperature(new QHash<int, int>),
  9374. controlColumns(new QHash<int, bool>)@/
  9375. {
  9376. colcount = 1;
  9377. entries = new QList<MeasurementList *>;
  9378. lastInsertion = entries->begin();
  9379. hData->append(tr("Time"));
  9380. }
  9381. @ In the destructor we need to remember to clean up after ourselves.
  9382. @<MeasurementModel Implementation@>=
  9383. MeasurementModel::~MeasurementModel()
  9384. {
  9385. clear();
  9386. delete entries;
  9387. delete hData;
  9388. }
  9389. @ A pair of functions are used to determine the number of rows and columns the
  9390. model provides. No entries in the model have children, so the parent should
  9391. always be the invisible root object. If it isn'@q'@>t, we should return 0.
  9392. @<MeasurementModel Implementation@>=
  9393. int MeasurementModel::rowCount(const QModelIndex &parent) const
  9394. {
  9395. if(parent == QModelIndex())
  9396. {
  9397. return entries->size();
  9398. }
  9399. return 0;
  9400. }
  9401. int MeasurementModel::columnCount(const QModelIndex &parent) const
  9402. {
  9403. if(parent == QModelIndex())
  9404. {
  9405. return colcount;
  9406. }
  9407. return 0;
  9408. }
  9409. @ The model maintains a set of header data. At present, it only supports header
  9410. data at the top of the model due to the author'@q'@>s preference to not have row
  9411. numbers littering the left of the table (the time column is sufficient to
  9412. identify the row for the user).
  9413. The model view architecture supports the concept of different data roles in the
  9414. header data. At present, this model ignores the role when setting header data.
  9415. @<MeasurementModel Implementation@>=
  9416. bool MeasurementModel::setHeaderData(int section, Qt::Orientation orientation,@|
  9417. const QVariant &value, int)@t\2@>@/
  9418. @t\4@>{@/
  9419. if(orientation == Qt::Horizontal)@/
  9420. {@t\1@>@/
  9421. if(hData->size() < section + 1)@/
  9422. {@/
  9423. for(int i = hData->size(); i < section + 1; i++)@/
  9424. {@/
  9425. if(colcount < i)@/
  9426. {@/
  9427. beginInsertColumns(QModelIndex(), i, i);
  9428. }
  9429. hData->append(QString());
  9430. if(colcount < i)@/
  9431. {@/
  9432. endInsertColumns();
  9433. }
  9434. }
  9435. }
  9436. hData->replace(section, value.toString());
  9437. emit headerDataChanged(orientation, section, section);
  9438. if(colcount < section + 1)@/
  9439. {@/
  9440. colcount = section + 1;
  9441. }@/
  9442. return true;@t\2@>@/
  9443. }@/
  9444. return false;@/
  9445. @t\4@>}
  9446. @ While the current implementation always receives measurements in degrees
  9447. Fahrenheit, international users often want to see data presented in Celsius. To
  9448. do this, a slot is provided to allow selecting among different units. When this
  9449. method is called, the model indicates that all attached views must update all
  9450. displayed data and requests for temperature data will have any needed conversion
  9451. performed before sending that information to the view. Another method is
  9452. available to request a number identifyin the currently displayed units.
  9453. @<MeasurementModel Implementation@>=
  9454. void MeasurementModel::setDisplayUnits(Units::Unit scale)
  9455. {
  9456. beginResetModel();
  9457. unit = scale;
  9458. endResetModel();
  9459. }
  9460. Units::Unit MeasurementModel::displayUnits()
  9461. {
  9462. return unit;
  9463. }
  9464. @ A model is generally quite useless if the data the model contains cannot be
  9465. retrieved. To do this, we check that the index requested is a valid index that
  9466. is within the bounds of the model data and that a role we understand has been
  9467. requested. If none of these conditions are met, a default constructed |QVariant|
  9468. is returned.
  9469. At present, |Qt::DisplayRole| and |Qt::EditRole| are supported. These return the
  9470. same thing. Views will request the display role for presenting the information
  9471. to the user, but they will request the edit role if the user attempts to modify
  9472. the data through a view.
  9473. As of version 1.6, |Qt::UserRole| allows retrieval of raw measurement data.
  9474. @<MeasurementModel Implementation@>=
  9475. QVariant MeasurementModel::data(const QModelIndex &index, int role) const@/
  9476. {@/
  9477. @<Check that the index is valid@>@;
  9478. if(!valid)
  9479. {
  9480. return QVariant();
  9481. }
  9482. MeasurementList *row = entries->at(index.row());
  9483. if(role == Qt::UserRole)
  9484. {
  9485. return QVariant(row->at(index.column()));
  9486. }
  9487. if(role == Qt::DisplayRole || role == Qt::EditRole)
  9488. {
  9489. if(index.column() > row->size())
  9490. {
  9491. return QVariant();
  9492. }
  9493. else
  9494. {
  9495. if(index.column() == 0)
  9496. {
  9497. return QVariant(row->at(0).toTime().toString("mm:ss.zzz"));
  9498. }
  9499. else if(lastTemperature->contains(index.column()))
  9500. {
  9501. QVariantMap v = row->at(index.column()).toMap();
  9502. if(!v.contains("measurement"))
  9503. {
  9504. return QVariant();
  9505. }
  9506. if((Units::Unit)(v.value("unit").toInt()) == Units::Unitless)
  9507. {
  9508. return v.value("measurement");
  9509. }
  9510. else
  9511. {
  9512. if(v.contains("relative"))
  9513. {
  9514. if(v.value("relative").toBool())
  9515. {
  9516. return QVariant(QString("%1").@|arg(Units::convertRelativeTemperature(v.value("measurement").
  9517. toDouble(),@| (Units::Unit)(v.value("unit").toInt()), unit)));
  9518. }
  9519. }
  9520. return QVariant(QString("%1").@|
  9521. arg(Units::convertTemperature(v.value("measurement").toDouble(),@|
  9522. (Units::Unit)(v.value("unit").toInt()), unit)));
  9523. }
  9524. }
  9525. return QVariant(row->at(index.column()).toString());
  9526. }
  9527. }
  9528. return QVariant();@/
  9529. }
  9530. @ Views also must be able to retrieve the header data.
  9531. @<MeasurementModel Implementation@>=
  9532. QVariant MeasurementModel::headerData(int section, Qt::Orientation orientation,
  9533. int role ) const
  9534. {
  9535. if(orientation == Qt::Horizontal)
  9536. {
  9537. if(role == Qt::DisplayRole)
  9538. {
  9539. if(section < hData->size())
  9540. {
  9541. return QVariant(hData->at(section));
  9542. }
  9543. }
  9544. }
  9545. return QVariant();
  9546. }
  9547. @ Views will sometimes request information about the interactions available for
  9548. an index. In the case of this model, each index is treated in the same way.
  9549. It may be a good idea to extend the model class to allow models that can be
  9550. edited through the view such as the table view presented in the |LogEditWindow|
  9551. and models that probably shouldn'@q'@>t be edited in the view, such as the models
  9552. managed by |ZoomLog|. This could be done by subclassing and only reimplementing
  9553. this method. Otherwise, a new method to specify that the user should not edit
  9554. the model could be provided and a flag would be checked here.
  9555. @<MeasurementModel Implementation@>=
  9556. Qt::ItemFlags MeasurementModel::flags(const QModelIndex &index) const@/
  9557. {@/
  9558. @<Check that the index is valid@>@;
  9559. if(valid)
  9560. {
  9561. return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
  9562. }
  9563. return 0;
  9564. }
  9565. @ Much of the way models are interacted with in Qt'@q'@>s model view architecture is
  9566. through model indices. The model is responsible for creating these indices from
  9567. row column pairs.
  9568. @<MeasurementModel Implementation@>=
  9569. QModelIndex MeasurementModel::index(int row, int column,
  9570. const QModelIndex &parent) const@t\2\2@>@/
  9571. {@t\1@>@/
  9572. if(parent == QModelIndex())@/
  9573. {@t\1@>@/
  9574. if(row < entries->size() && entries->isEmpty() == false)@/
  9575. {@/
  9576. if(column < entries->at(row)->size())@/
  9577. {@/
  9578. return createIndex(row, column);@/
  9579. }@/
  9580. }@t\2@>@/
  9581. }@/
  9582. return QModelIndex();@/
  9583. @t\4@>}
  9584. @** Annotating roast data.
  9585. \noindent In addition to recording time temperature pairs, \pn{} allows the user
  9586. to annotate the roasting log to indicate control changes, the end of the batch,
  9587. or samples collected from the roast. It is important that these annotations can
  9588. be applied to the roasting log quickly. This is the purpose of the
  9589. |AnnotationButton| class.
  9590. @<Class declarations@>=
  9591. class AnnotationButton : public QPushButton@/
  9592. {@t\1@>@/
  9593. Q_OBJECT@;
  9594. QString noteTemplate;
  9595. QString note;
  9596. int tc;
  9597. int ac;
  9598. int count;
  9599. QString batch;
  9600. public:@/
  9601. AnnotationButton(const QString &text, QWidget *parent = NULL);@/
  9602. @t\4@>public slots@t\kern-3pt@>:@/
  9603. void setAnnotation(const QString &annotation);
  9604. void setTemperatureColumn(int tempcolumn);
  9605. void setAnnotationColumn(int annotationcolumn);
  9606. void annotate();
  9607. void resetCount();
  9608. void resetBatch();
  9609. void incrementBatch();
  9610. signals:@/
  9611. void annotation(QString annotation, int tempcolumn,
  9612. int notecolumn);@t\2@>@/
  9613. }@t\kern-3pt@>;
  9614. @ Setting up a new annotation button begins with the constructor. This takes a
  9615. string specifying the text that will appear on the button and optionally a
  9616. parent widget. This is also a sensible place to set up the desired behavior the
  9617. button should exhibit when clicked.
  9618. @<AnnotationButton Implementation@>=
  9619. AnnotationButton::AnnotationButton(const QString &text, QWidget *parent) :
  9620. QPushButton(text, parent), noteTemplate(""), note(""), tc(0), ac(0),
  9621. count(0), batch("A")@/
  9622. {
  9623. connect(this, SIGNAL(clicked()), this, SLOT(annotate()));
  9624. }
  9625. @ The slot that is called when the button is clicked needs to be able to handle
  9626. two types of annotations. Simple annotations send the same annotation every time
  9627. the button is clicked. Counting annotations are annotation strings that have a
  9628. |"%1"| somewhere in the string. That substring will be replaced with an integer
  9629. that is incremented before the annotation is sent. This integer is initialized
  9630. to 0. It will be incremented to 1 the first time the button is clicked and that
  9631. will be the replacement value.
  9632. @<AnnotationButton Implementation@>=
  9633. void AnnotationButton::annotate()
  9634. {
  9635. if(note.contains("%1"))
  9636. {
  9637. count++;
  9638. emit annotation(note.arg(count), tc, ac);
  9639. }
  9640. else
  9641. {
  9642. emit annotation(note, tc, ac);
  9643. }
  9644. }
  9645. @ A few methods are available to indicate which temperature series the
  9646. annotation should be applied to, which column in a table view the annotation
  9647. should be entered in, and what text should be in the annotation.
  9648. @<AnnotationButton Implementation@>=
  9649. void AnnotationButton::setTemperatureColumn(int tempcolumn)
  9650. {
  9651. tc = tempcolumn;
  9652. }
  9653. void AnnotationButton::setAnnotationColumn(int annotationcolumn)
  9654. {
  9655. ac = annotationcolumn;
  9656. }
  9657. void AnnotationButton::setAnnotation(const QString &annotation)
  9658. {
  9659. noteTemplate = annotation;
  9660. @<Replace batch holder in template@>@;
  9661. }
  9662. @ Note templates that contain "%A" should have that replaced with a
  9663. string that can be incremented between batches.
  9664. @<Replace batch holder in template@>=
  9665. int batchReplace = noteTemplate.indexOf("%A");
  9666. if(batchReplace >= 0)
  9667. {
  9668. QString temp = noteTemplate;
  9669. note = temp.replace(batchReplace, 2, batch);
  9670. }
  9671. else
  9672. {
  9673. note = noteTemplate;
  9674. }
  9675. @ In the case of counting annotations, there should be a way to reset
  9676. the number and batch used in the annotation.
  9677. @<AnnotationButton Implementation@>=
  9678. void AnnotationButton::resetCount()
  9679. {
  9680. count = 0;
  9681. }
  9682. void AnnotationButton::resetBatch()
  9683. {
  9684. batch = "A";
  9685. @<Replace batch holder in template@>@;
  9686. }
  9687. @ The batch sequence starts at A through Z, then proceeds to AA through AZ
  9688. and so on.
  9689. @<AnnotationButton Implementation@>=
  9690. void AnnotationButton::incrementBatch()
  9691. {
  9692. int position = batch.size() - 1;
  9693. increment:
  9694. if(batch[position] != 'Z')
  9695. {
  9696. batch[position] = batch[position].unicode() + 1;
  9697. }
  9698. else
  9699. {
  9700. batch[position] = 'A';
  9701. if(position > 0)
  9702. {
  9703. position--;
  9704. goto increment;
  9705. }
  9706. else
  9707. {
  9708. batch.append("A");
  9709. }
  9710. }
  9711. @<Replace batch holder in template@>@;
  9712. }
  9713. @ A script constructor is needed to allow an |AnnotationButton| to be created
  9714. from a script.
  9715. @<Function prototypes for scripting@>=
  9716. QScriptValue constructAnnotationButton(QScriptContext *context,
  9717. QScriptEngine *engine);
  9718. void setAnnotationButtonProperties(QScriptValue value, QScriptEngine *engine);
  9719. @ In order to use this, the engine needs to be informed of the function.
  9720. @<Set up the scripting engine@>=
  9721. constructor = engine->newFunction(constructAnnotationButton);
  9722. value = engine->newQMetaObject(&AnnotationButton::staticMetaObject,
  9723. constructor);
  9724. engine->globalObject().setProperty("AnnotationButton", value);
  9725. @ The implementation is trivial.
  9726. @<Functions for scripting@>=
  9727. QScriptValue constructAnnotationButton(QScriptContext *context,
  9728. QScriptEngine *engine)
  9729. {
  9730. QScriptValue object =
  9731. engine->newQObject(new AnnotationButton(argument<QString>(0, context)));
  9732. setAnnotationButtonProperties(object, engine);
  9733. return object;
  9734. }
  9735. void setAnnotationButtonProperties(QScriptValue value, QScriptEngine *engine)
  9736. {
  9737. setQPushButtonProperties(value, engine);
  9738. }
  9739. @* A spin box for annotations.
  9740. \noindent While the annotation button is adequate for most log annotation tasks,
  9741. there are some times where the log should contain a small number of numerical
  9742. observations where it is inconvenient or cost prohibitive to enable automated
  9743. logging. For these tasks, a spin box that produces an appropriate annotation may
  9744. be useful.
  9745. @<Class declarations@>=
  9746. class AnnotationSpinBox : public QDoubleSpinBox@/
  9747. {@t\1@>@/
  9748. Q_OBJECT@;
  9749. QString pretext;
  9750. QString posttext;
  9751. int tc;
  9752. int ac;
  9753. bool change;
  9754. public:
  9755. AnnotationSpinBox(const QString &pret, const QString &postt,
  9756. QWidget *parent = NULL);@/
  9757. @t\4@>public slots@t\kern-3pt@>:@/
  9758. void setPretext(const QString &pret);
  9759. void setPosttext(const QString &postt);
  9760. void setTemperatureColumn(int tempcolumn);
  9761. void setAnnotationColumn(int annotationcolumn);
  9762. void annotate();
  9763. void resetChange();
  9764. signals:@/
  9765. void annotation(QString annotation, int tempcolumn,
  9766. int notecolumn);@t\2@>@/
  9767. }@t\kern-3pt@>;
  9768. @ Setting up a new annotation spin box begins with the constructor. This takes
  9769. two strings specifying optional text that may appear before or after the
  9770. numerical value of the spin box in the annotation. No spaces are placed between
  9771. the text and the numerical values, so if such spacing is required, it must be
  9772. included in the relevant string.
  9773. This function also sets up the behavior for firing annotation events. An
  9774. annotation should be fired when the user presses enter while the spin box has
  9775. focus. This implementation will also attempt to fire an annotation when the
  9776. spin box loses focus. No annotation is fired if the value of the spin box has
  9777. not been changed since the previous annotation event.
  9778. @<AnnotationSpinBox Implementation@>=
  9779. AnnotationSpinBox::AnnotationSpinBox(const QString &pret,
  9780. const QString &postt,@|
  9781. QWidget *parent)
  9782. : QDoubleSpinBox(parent), pretext(pret), posttext(postt)@/
  9783. {
  9784. resetChange();
  9785. connect(this, SIGNAL(editingFinished()), this, SLOT(annotate()));
  9786. connect(this, SIGNAL(valueChanged(double)), this, SLOT(resetChange()));
  9787. }
  9788. @ The |resetChange()| signal just sets a boolean which is checked prior to
  9789. sending an annotation. This is called automatically when the value of the spin
  9790. box is changed, but it should also be called when a batch is finished in case
  9791. the first required annotation is the same as the last required annotation from
  9792. the previous batch.
  9793. @<AnnotationSpinBox Implementation@>=
  9794. void AnnotationSpinBox::resetChange()@t\2\2@>@/
  9795. {@t\1@>@/
  9796. change = true;@t\2@>@/
  9797. }
  9798. @ The annotation slot is responsible for determining if an annotation should be
  9799. sent. The current implementation is to only attempt to send such a signal when
  9800. the |editingFinished()| signal is emitted, however this could also be connected
  9801. to other signals.
  9802. @<AnnotationSpinBox Implementation@>=
  9803. void AnnotationSpinBox::annotate()@t\2\2@>@/
  9804. {@t\1@>@/
  9805. if(change)@/
  9806. {@t\1@>@/
  9807. change = false;@/
  9808. emit annotation(QString("%1%2%3").arg(pretext).
  9809. arg(value()).arg(posttext), tc, ac);@t\2@>@/
  9810. }@t\2@>@/
  9811. }
  9812. @ These methods set various properties of the annotation.
  9813. @<AnnotationSpinBox Implementation@>=
  9814. void AnnotationSpinBox::setTemperatureColumn(int tempcolumn)
  9815. {
  9816. tc = tempcolumn;
  9817. }
  9818. void AnnotationSpinBox::setAnnotationColumn(int annotationcolumn)
  9819. {
  9820. ac = annotationcolumn;
  9821. }
  9822. void AnnotationSpinBox::setPretext(const QString &pret)
  9823. {
  9824. pretext = pret;
  9825. }
  9826. void AnnotationSpinBox::setPosttext(const QString &postt)
  9827. {
  9828. posttext = postt;
  9829. }
  9830. @ Two functions are needed to interface |AnnotationSpinBox| with the host
  9831. environment. Additional functions are required for setting up inheritance
  9832. properly.
  9833. @<Function prototypes for scripting@>=
  9834. QScriptValue constructAnnotationSpinBox(QScriptContext *context,
  9835. QScriptEngine *engine);
  9836. void setAnnotationSpinBoxProperties(QScriptValue value, QScriptEngine *engine);
  9837. void setQDoubleSpinBoxProperties(QScriptValue value, QScriptEngine *engine);
  9838. void setQAbstractSpinBoxProperties(QScriptValue value, QScriptEngine *engine);
  9839. @ The first of these is passed into the host environment.
  9840. @<Set up the scripting engine@>=
  9841. constructor = engine->newFunction(constructAnnotationSpinBox);
  9842. value = engine->newQMetaObject(&AnnotationSpinBox::staticMetaObject,
  9843. constructor);
  9844. engine->globalObject().setProperty("AnnotationSpinBox", value);
  9845. @ The script constructor creates a new object and passes it to a function that
  9846. is responsible for setting up properties in the inheritance chain.
  9847. @<Functions for scripting@>=
  9848. QScriptValue constructAnnotationSpinBox(QScriptContext *context,
  9849. QScriptEngine *engine)
  9850. {
  9851. QScriptValue object = engine->newQObject(new AnnotationSpinBox(
  9852. argument<QString>(0, context), argument<QString>(1, context)));
  9853. setAnnotationSpinBoxProperties(object, engine);
  9854. return object;
  9855. }
  9856. void setAnnotationSpinBoxProperties(QScriptValue value, QScriptEngine *engine)
  9857. {
  9858. setQDoubleSpinBoxProperties(value, engine);
  9859. }
  9860. void setQDoubleSpinBoxProperties(QScriptValue value, QScriptEngine *engine)
  9861. {
  9862. setQAbstractSpinBoxProperties(value, engine);
  9863. }
  9864. void setQAbstractSpinBoxProperties(QScriptValue value, QScriptEngine *engine)
  9865. {
  9866. setQWidgetProperties(value, engine);
  9867. }
  9868. @** A digital timer.
  9869. \noindent Before \pn{} was a data logger, it was a simple digital timer written
  9870. because there were no shops in Racine that could sell a simple dual digital
  9871. count up timer at a time when my first timer was malfunctioning. After
  9872. attempting to purchase a replacement device at several stores that have sold
  9873. such devices in the past, I decided to spend a couple hours writing my own
  9874. timer.
  9875. For historical reasons, the |TimerDisplay| class is considerably more functional
  9876. than \pn{} requires. Those needing only a digital timer can extract the code for
  9877. this class and use it in a timer application. This should work on any platform
  9878. supported by Qt.
  9879. @<Class declarations@>=
  9880. class TimerDisplay : public QLCDNumber@/
  9881. {@t\1@>@/
  9882. Q_OBJECT@/
  9883. @<TimerDisplay Properties@>@;
  9884. @t\4@>private slots@t\kern-3pt@>:@/
  9885. void updateTime();
  9886. void setCountUpMode();
  9887. void setCountDownMode();
  9888. void setClockMode();
  9889. public:@/
  9890. TimerDisplay(QWidget *parent = NULL);
  9891. ~TimerDisplay();
  9892. enum TimerMode
  9893. {
  9894. CountUp,
  9895. CountDown,
  9896. Clock
  9897. };
  9898. QString value();
  9899. QTime seconds();
  9900. TimerMode mode();
  9901. bool isRunning();
  9902. QTime resetValue();
  9903. QString displayFormat();
  9904. bool autoReset();@/
  9905. @t\4@>public slots@t\kern-3pt@>:@/
  9906. void setTimer(QTime value = QTime(0, 0, 0));
  9907. void setMode(TimerMode mode);
  9908. void startTimer();
  9909. void stopTimer();
  9910. void copyTimer();
  9911. void setResetValue(QTime value = QTime(0, 0, 0));
  9912. void reset();
  9913. void setDisplayFormat(QString format);
  9914. void setAutoReset(bool reset);
  9915. void updateDisplay();
  9916. signals:@/
  9917. void valueChanged(QTime);
  9918. void runStateChanged(bool);@/
  9919. private:@/
  9920. @<TimerDisplay Private Variables@>@;@t\2@>@/
  9921. }@t\kern-3pt@>;
  9922. @ Qt provides a property system based on its meta-object system. This allows for
  9923. a number of advanced features which \pn{} does not use. The properties available
  9924. for the TimerDisplay class exist for historical reasons, but there are some
  9925. plans for future development which may make use of them. The properties may also
  9926. be useful for someone using this class in another program.
  9927. @<TimerDisplay Properties@>=
  9928. Q_PROPERTY(QTime seconds READ seconds WRITE setTimer)@/
  9929. Q_PROPERTY(TimerMode mode READ mode WRITE setMode)@/
  9930. Q_PROPERTY(bool running READ isRunning)@/
  9931. Q_PROPERTY(QTime resetValue READ resetValue WRITE setResetValue)@/
  9932. Q_PROPERTY(QString displayFormat READ displayFormat WRITE setDisplayFormat)@/
  9933. Q_PROPERTY(bool autoReset READ autoReset WRITE setAutoReset)@/
  9934. Q_PROPERTY(QString value READ value)@/
  9935. @ A number of private variables are used to implement this class.
  9936. @<TimerDisplay Private Variables@>=
  9937. QTime s;
  9938. QTime r;
  9939. QTimer clock;
  9940. TimerDisplay::TimerMode m;
  9941. bool running;
  9942. bool ar;
  9943. QAction *startAction;
  9944. QAction *stopAction;
  9945. QAction *resetAction;
  9946. QString f;
  9947. QTime relative;
  9948. QTime base;
  9949. @ |TimerDisplay| is a specialization of |QLCDNumber| designed for time keeping
  9950. purposes. It sets up a timer that fires roughly every half second to see if it
  9951. needs to update itself. The constructor sets this up, but does not start the
  9952. timer. The class provides three actions which can be used to start, stop, or
  9953. reset the timer. These actions are also set up in the constructor.
  9954. By default, the timer will display its time in hours, minutes, and seconds. This
  9955. can be changed as is done with the batch timer (it is expected that nobody will
  9956. want to spend an hour or more to roast a batch of coffee). The display style is
  9957. also changed to a sensible default, but this can be changed with the usual
  9958. |QLCDNumber| methods.
  9959. @<TimerDisplay Implementation@>=
  9960. TimerDisplay::TimerDisplay(QWidget *parent) : QLCDNumber(8, parent),
  9961. s(QTime(0, 0, 0)), r(QTime(0, 0, 0)), clock(NULL),@/ m(TimerDisplay::CountUp),
  9962. running(false), ar(false), startAction(new QAction(tr("Start"), NULL)),@/
  9963. stopAction(new QAction(tr("Stop"), NULL)),
  9964. resetAction(new QAction(tr("Reset"), NULL)),@/ f(QString("hh:mm:ss")),
  9965. relative(QTime::currentTime()), base(QTime(0, 0, 0))@/
  9966. {
  9967. connect(startAction, SIGNAL(triggered(bool)), this, SLOT(startTimer()));
  9968. connect(stopAction, SIGNAL(triggered(bool)), this, SLOT(stopTimer()));
  9969. connect(resetAction, SIGNAL(triggered(bool)), this, SLOT(reset()));
  9970. clock.setInterval(500);
  9971. clock.setSingleShot(false);
  9972. connect(&clock, SIGNAL(timeout()), this, SLOT(updateTime()));
  9973. setSegmentStyle(Filled);
  9974. updateDisplay();
  9975. }
  9976. @ The complicated bits are all in the |updateTime()| method. The behavior of
  9977. this function depends on the current |TimerMode| of the display.
  9978. @<TimerDisplay Implementation@>=
  9979. void TimerDisplay::updateTime()
  9980. {
  9981. QTime time;
  9982. int cseconds = 0;
  9983. int oseconds = 0;
  9984. int r = 0;
  9985. QTime nt = QTime(0, 0, 0);
  9986. int n = 0;
  9987. int bseconds = 0;
  9988. switch(m)@/
  9989. {@t\1@>@/
  9990. case TimerDisplay::CountUp:@/
  9991. @<Check for Timer Increment@>;
  9992. break;
  9993. case TimerDisplay::CountDown:@/
  9994. @<Check for Timer Decrement@>;
  9995. break;
  9996. case TimerDisplay::Clock:@/
  9997. @<Check for Clock Change@>;
  9998. break;
  9999. default:@/
  10000. Q_ASSERT_X(false, "updateTime", "invalid timer mode");
  10001. break;@t\2@>@/
  10002. }
  10003. updateDisplay();
  10004. }
  10005. @ To have the timer count up, we calculate the value that the timer should
  10006. indicate and compare it to the time indicated. If there is a difference, we
  10007. update the time to the new value and send emit a signal.
  10008. @<Check for Timer Increment@>=
  10009. @<Load seconds since base time into r@>@;
  10010. nt = nt.addSecs(r);
  10011. if(nt != s)
  10012. {
  10013. s = nt;
  10014. emit valueChanged(s);
  10015. }
  10016. @ Here we want to calculate the number of seconds in the current time, the
  10017. number of seconds in a base time, and the difference between the two. The
  10018. value loaded into oseconds could probably be cached.
  10019. @<Load seconds since base time into r@>=
  10020. #define TIMETOINT(t) ((t.hour() * 60 * 60) + (t.minute() * 60) + (t.second()))
  10021. time = QTime::currentTime();
  10022. cseconds = TIMETOINT(time);
  10023. oseconds = TIMETOINT(relative);
  10024. r = cseconds - oseconds;
  10025. @ The logic for a count down timer is very similar to the logic for a count up
  10026. timer. A key difference is that we don'@q'@>t want to continue counting down if the
  10027. timer has already reached 0.
  10028. @<Check for Timer Decrement@>=
  10029. if(s > QTime(0, 0, 0))@/
  10030. {@/
  10031. @<Load seconds since base time into r@>@;
  10032. bseconds = TIMETOINT(base);
  10033. n = bseconds - r;
  10034. nt = nt.addSecs(n);
  10035. if(nt != s)
  10036. {
  10037. s = nt;
  10038. emit valueChanged(s);
  10039. }
  10040. } else {
  10041. stopTimer();
  10042. }
  10043. @ The clock mode is the simplest case as it just needs to find out if the time
  10044. has changed.
  10045. @<Check for Clock Change@>=
  10046. time = QTime::currentTime();
  10047. if(time != s)
  10048. {
  10049. s = time;
  10050. emit valueChanged(s);
  10051. }
  10052. @ When counting up or down, it is important to record the time at which the
  10053. timer starts. The clock that triggers time updates must also be started. The
  10054. timer also needs to reset its value if that behavior is desired.
  10055. @<TimerDisplay Implementation@>=
  10056. #define TIMESUBTRACT(t1, t2) (t1.addSecs(-(TIMETOINT(t2))).addSecs(-t2.msec()))
  10057. void TimerDisplay::startTimer()@t\2\2@>@/
  10058. {@t\1@>@/
  10059. if(!running)@/
  10060. {@t\1@>@/
  10061. relative = QTime::currentTime();
  10062. if(ar)@/
  10063. {
  10064. reset();
  10065. }
  10066. else
  10067. {
  10068. relative = TIMESUBTRACT(relative, s);
  10069. }
  10070. if(m == Clock)@/
  10071. {
  10072. updateTime();
  10073. }
  10074. base = s;
  10075. clock.start();@/
  10076. running = true;
  10077. emit runStateChanged(true);@t\2@>@/
  10078. }@t\2@>@/
  10079. }
  10080. @ Stopping the timer is a little simpler. Remember to stop the clock so we
  10081. aren'@q'@>t updating senselessly.
  10082. @<TimerDisplay Implementation@>=
  10083. void TimerDisplay::stopTimer()@t\2\2@>@/
  10084. {@t\1@>@/
  10085. if(running)@/
  10086. {@t\1@>@/
  10087. clock.stop();@/
  10088. running = false;
  10089. emit runStateChanged(false);@t\2@>@/
  10090. }@t\2@>@/
  10091. }
  10092. @ The clock is also stopped in the destructor.
  10093. @<TimerDisplay Implementation@>=
  10094. TimerDisplay::~TimerDisplay()
  10095. {
  10096. clock.stop();
  10097. }
  10098. @ The rest of the functions are trivial. There are functions for changing the
  10099. timer mode:
  10100. @<TimerDisplay Implementation@>=
  10101. void TimerDisplay::setCountUpMode()
  10102. {
  10103. m = TimerDisplay::CountUp;
  10104. }
  10105. void TimerDisplay::setCountDownMode()
  10106. {
  10107. m = TimerDisplay::CountDown;
  10108. }
  10109. void TimerDisplay::setClockMode()
  10110. {
  10111. m = TimerDisplay::Clock;
  10112. }
  10113. @ There are a few functions to obtain information about the state of the timer.
  10114. @<TimerDisplay Implementation@>=
  10115. QString TimerDisplay::value()
  10116. {
  10117. return s.toString(f);
  10118. }
  10119. QTime TimerDisplay::seconds()
  10120. {
  10121. return s;
  10122. }
  10123. TimerDisplay::TimerMode TimerDisplay::mode()
  10124. {
  10125. return m;
  10126. }
  10127. bool TimerDisplay::isRunning()
  10128. {
  10129. return running;
  10130. }
  10131. QTime TimerDisplay::resetValue()
  10132. {
  10133. return r;
  10134. }
  10135. QString TimerDisplay::displayFormat()
  10136. {
  10137. return f;
  10138. }
  10139. bool TimerDisplay::autoReset()
  10140. {
  10141. return ar;
  10142. }
  10143. @ There are also some functions for setting aspects of the timer state.
  10144. @<TimerDisplay Implementation@>=
  10145. void TimerDisplay::setTimer(QTime value)
  10146. {
  10147. if(value.isValid())
  10148. {
  10149. s = value;
  10150. updateDisplay();
  10151. emit valueChanged(value);
  10152. }
  10153. }
  10154. void TimerDisplay::setMode(TimerDisplay::TimerMode mode)
  10155. {
  10156. m = mode;
  10157. }
  10158. void TimerDisplay::setResetValue(QTime value)
  10159. {
  10160. r = value;
  10161. }
  10162. void TimerDisplay::setDisplayFormat(QString format)
  10163. {
  10164. f = format;
  10165. setNumDigits(format.length());
  10166. }
  10167. void TimerDisplay::setAutoReset(bool reset)
  10168. {
  10169. ar = reset;
  10170. }
  10171. @ |TimerDisplay| supports using the system clipboard to copy the current timer
  10172. value.
  10173. @<TimerDisplay Implementation@>=
  10174. void TimerDisplay::copyTimer()
  10175. {
  10176. QApplication::clipboard()->setText(value());
  10177. }
  10178. @ Resetting the timer is simple. We don'@q'@>t reset the timer if it is still running
  10179. mainly to prevent accidents.
  10180. @<TimerDisplay Implementation@>=
  10181. void TimerDisplay::reset()
  10182. {
  10183. if(!running)
  10184. {
  10185. s = r;
  10186. updateDisplay();
  10187. }
  10188. }
  10189. @ Finally, there is the function for changing the text of the display to the
  10190. current time value.
  10191. @<TimerDisplay Implementation@>=
  10192. void TimerDisplay::updateDisplay()
  10193. {
  10194. display(value());
  10195. }
  10196. @ Exposing |TimerDisplay| to the host environment is simple.
  10197. @<Function prototypes for scripting@>=
  10198. QScriptValue constructTimerDisplay(QScriptContext *context,
  10199. QScriptEngine *engine);
  10200. void setTimerDisplayProperties(QScriptValue value, QScriptEngine *engine);
  10201. QScriptValue TimerDisplay_setTimerMode(QScriptContext *context, QScriptEngine *engine);
  10202. @ The engine must be informed of the script constructor.
  10203. @<Set up the scripting engine@>=
  10204. constructor = engine->newFunction(constructTimerDisplay);
  10205. value = engine->newQMetaObject(&TimerDisplay::staticMetaObject, constructor);
  10206. engine->globalObject().setProperty("TimerDisplay", value);
  10207. @ The implementation of these functions is trivial.
  10208. @<Functions for scripting@>=
  10209. QScriptValue constructTimerDisplay(QScriptContext *, QScriptEngine *engine)
  10210. {
  10211. QScriptValue object = engine->newQObject(new TimerDisplay);
  10212. setTimerDisplayProperties(object, engine);
  10213. return object;
  10214. }
  10215. void setTimerDisplayProperties(QScriptValue value, QScriptEngine *engine)
  10216. {
  10217. setQLCDNumberProperties(value, engine);
  10218. value.setProperty("setTimerMode", engine->newFunction(TimerDisplay_setTimerMode));
  10219. }
  10220. @ A new feature in \pn{} 1.6.4 benefits from having the ability to set the
  10221. timer mode from a script. Rather than exposing the |enum| responsible for this
  10222. to the host environment, a new function is provided to allow integer based
  10223. setting.
  10224. @<Functions for scripting@>=
  10225. QScriptValue TimerDisplay_setTimerMode(QScriptContext *context, QScriptEngine *)
  10226. {
  10227. TimerDisplay *self = getself<TimerDisplay *>(context);
  10228. if(self)
  10229. {
  10230. switch(argument<int>(0, context))
  10231. {
  10232. case 0:
  10233. self->setMode(TimerDisplay::CountUp);
  10234. break;
  10235. case 1:
  10236. self->setMode(TimerDisplay::CountDown);
  10237. break;
  10238. case 2:
  10239. self->setMode(TimerDisplay::Clock);
  10240. break;
  10241. default:
  10242. break;
  10243. }
  10244. }
  10245. return QScriptValue();
  10246. }
  10247. @** The Human Computer Interface.
  10248. \noindent A few classes are required for putting the rest of the program
  10249. together in a way that it can be used by a human. There is a layout class for
  10250. arranging widgets in a way that is not simple with the layouts provided by Qt.
  10251. There are classes for labeling the various indicators. There are also window
  10252. classes that put all of this together in a useful and usable way. One of these
  10253. classes is currently depreciated.
  10254. @* The PackLayout Class.
  10255. \noindent The |PackLayout| class provides functionality similar to the
  10256. |QBoxLayout| class in Qt. It allows the construction of a row or column of
  10257. widgets. Each item will take up space along the orientation of the layout equal
  10258. to its size hint except for the last widget which will take up all remaining
  10259. space. Widgets will be resized in the direction perpendicular to the orientation
  10260. of the layout to use all available space.
  10261. This class was originally written with the |WidgetDecorator| class which we will
  10262. get to later in mind, but it has found use in other places where the left or top
  10263. most widgets should not be resized.
  10264. By default, a new |PackLayout| will arrange widgets horizontally. This can be
  10265. changed with a call to |setOrientation()|.
  10266. @<Class declarations@>=
  10267. class PackLayout : public QLayout@/
  10268. {@/
  10269. int doLayout(const QRect &rect, bool testOnly) const;@/
  10270. QList<QLayoutItem *> itemList;@/
  10271. Qt::Orientations@, orientation;@/
  10272. public:@/
  10273. PackLayout(QWidget *parent, int margin = 0, int spacing = -1);
  10274. PackLayout(int spacing = -1);
  10275. ~PackLayout();
  10276. void addItem(QLayoutItem *item);
  10277. Qt::Orientations@, expandingDirections() const;
  10278. bool hasHeightForWidth() const;
  10279. int heightForWidth(int width) const;
  10280. int count() const;
  10281. QLayoutItem *itemAt(int index) const;
  10282. QSize minimumSize() const;
  10283. void setGeometry(const QRect &rect);
  10284. void setOrientation(Qt::Orientations direction);
  10285. QSize sizeHint() const;
  10286. QLayoutItem *takeAt(int index);
  10287. };
  10288. @ The interesting portion of this class is in |doLayout()|. This function goes
  10289. over the items in the layout and sets the geometry appropriately.
  10290. The seemingly odd choice of returning |y| at the end of this function (indeed of
  10291. having a return value at all) is to allow this function to provide the return
  10292. value needed in |heightForWidth()|.
  10293. If |testOnly| is set to |true|, |y| will be calculated, but the widget geometry
  10294. will not be changed.
  10295. @<PackLayout Implementation@>=
  10296. int PackLayout::doLayout(const QRect &rect, bool testOnly) const
  10297. {
  10298. int x = rect.x();
  10299. int y = rect.y();
  10300. QLayoutItem *item;
  10301. if(orientation == Qt::Horizontal)
  10302. {
  10303. @<Lay the widgets out horizontally@>@;
  10304. }
  10305. else
  10306. {
  10307. @<Lay the widgets out vertically@>@;
  10308. }
  10309. return y;
  10310. }
  10311. @ To lay the widgets out horizontally, we go over each item in the list taking
  10312. the width of the size hint and spacing into account unless the item is the last
  10313. item in the list, in which case the right of the widget needs to be at the end
  10314. of the available space. We use the foreach construction that Qt provides to
  10315. iterate over each item in the list in much the same way as foreach constructions
  10316. are used in languages that support them directly.
  10317. @<Lay the widgets out horizontally@>=
  10318. foreach(item, itemList)
  10319. {
  10320. int nextX = x + item->sizeHint().width() + spacing();
  10321. int right = x + item->sizeHint().width();
  10322. if(item == itemList.last())
  10323. {
  10324. right = rect.right();
  10325. }
  10326. int bottom = rect.bottom();
  10327. if(!testOnly)
  10328. {
  10329. item->setGeometry(QRect(QPoint(x, y), QPoint(right, bottom)));
  10330. }
  10331. x = nextX;
  10332. }
  10333. @ Laying out the widgets vertically is very similar.
  10334. @<Lay the widgets out vertically@>=
  10335. foreach(item, itemList)
  10336. {
  10337. int nextY = y + item->sizeHint().height() + spacing();
  10338. int bottom = y + item->sizeHint().height();
  10339. if(item == itemList.last())
  10340. {
  10341. bottom = rect.bottom();
  10342. }
  10343. int right = rect.right();
  10344. if(!testOnly)
  10345. {
  10346. item->setGeometry(QRect(QPoint(x, y), QPoint(right, bottom)));
  10347. }
  10348. y = nextY;
  10349. }
  10350. @ As a layout class, there are a number of things the class should be able to do
  10351. in order to play nicely with other classes. One of these is determining the
  10352. minimum size of the layout. The minimum size of the layout is equal to the space
  10353. required for each item in the layout plus the margin space. The margin space
  10354. will be equal to twice the specified margin in each direction to account for a
  10355. top, bottom, left, and right margin.
  10356. @<PackLayout Implementation@>=
  10357. QSize PackLayout::minimumSize() const
  10358. {
  10359. QSize size;
  10360. QLayoutItem *item;
  10361. foreach(item, itemList)
  10362. {
  10363. if(orientation == Qt::Horizontal)
  10364. {
  10365. size += QSize(item->minimumSize().width(), 0);
  10366. if(size.height() < item->minimumSize().height())
  10367. {
  10368. size.setHeight(item->minimumSize().height());
  10369. }
  10370. }
  10371. else
  10372. {
  10373. size += QSize(0, item->minimumSize().height());
  10374. if(size.width() < item->minimumSize().width())
  10375. {
  10376. size.setWidth(item->minimumSize().width());
  10377. }
  10378. }
  10379. }
  10380. size += QSize(2*margin(), 2*margin());
  10381. return size;
  10382. }
  10383. @ |PackLayout| features two constructors. One allows for setting the margin,
  10384. spacing, and a parent widget at the time of construction. The other creates a
  10385. parentless layout which will have to be added to another widget or layout.
  10386. @<PackLayout Implementation@>=
  10387. PackLayout::PackLayout(QWidget *parent, int margin, int spacing) :
  10388. QLayout(parent)@/
  10389. {
  10390. setMargin(margin);
  10391. setSpacing(spacing);
  10392. setOrientation(Qt::Horizontal);
  10393. }
  10394. PackLayout::PackLayout(int spacing)
  10395. {
  10396. setSpacing(spacing);
  10397. setOrientation(Qt::Horizontal);
  10398. }
  10399. @ In Qt, items in a layout are owned by that layout. When the layout is
  10400. destroyed, all of the items in that layout must also be deleted.
  10401. @<PackLayout Implementation@>=
  10402. PackLayout::~PackLayout()
  10403. {
  10404. QLayoutItem *item;
  10405. while((item = takeAt(0)))
  10406. {
  10407. delete item;
  10408. }
  10409. }
  10410. @ Deleting the items uses the |takeAt()| method to remove each widget from the
  10411. layout prior to deleting it. The item requested should exist, but if it doesn'@q'@>t,
  10412. |NULL| is returned.
  10413. @<PackLayout Implementation@>=
  10414. QLayoutItem* PackLayout::takeAt(int index)
  10415. {
  10416. if(index >= 0 && index < itemList.size())
  10417. {
  10418. return itemList.takeAt(index);
  10419. }
  10420. else
  10421. {
  10422. return NULL;
  10423. }
  10424. }
  10425. @ If we are interested in which item is in a particular position in the layout
  10426. but do not want to remove it from the layout, |itemAt()| provides that.
  10427. @<PackLayout Implementation@>=
  10428. QLayoutItem* PackLayout::itemAt(int index) const
  10429. {
  10430. if(index >= 0 && index < itemList.size())
  10431. {
  10432. return itemList.at(index);
  10433. }
  10434. else
  10435. {
  10436. return NULL;
  10437. }
  10438. }
  10439. @ A layout class is not very useful unless there is a way to get items into the
  10440. layout. The |QLayoutItem| class is designed in such a way that it is possible to
  10441. pass pointers to objects that inherit |QLayout| or |QWidget|.
  10442. The base |QLayout| class provides an |addWidget()| method that will use our
  10443. version of |addItem()|. That should be used when adding a widget to the layout.
  10444. The Qt documentation recommends also providing an |addLayout()| method so that
  10445. other code does not need to call this method, but that has not been provided
  10446. yet.
  10447. @<PackLayout Implementation@>=
  10448. void PackLayout::addItem(QLayoutItem *item)
  10449. {
  10450. itemList.append(item);
  10451. }
  10452. @ It is sometimes useful to know how many items are in a layout.
  10453. @<PackLayout Implementation@>=
  10454. int PackLayout::count() const@;@/
  10455. {@/
  10456. return itemList.size();@/
  10457. }
  10458. @ A few more functions are needed to make the layout class work well with other
  10459. classes. For more details, please consult the Qt Reference
  10460. Documentation\nfnote{Qt Reference Documentation\par\indent\hbox{%
  10461. \pdfURL{http://doc.trolltech.com/4.3/index.html}%
  10462. {http://doc.trolltech.com/4.3/index.html}}}
  10463. @<PackLayout Implementation@>=
  10464. Qt::Orientations PackLayout::expandingDirections() const
  10465. {
  10466. return Qt::Vertical | Qt::Horizontal;
  10467. }
  10468. bool PackLayout::hasHeightForWidth() const@t\2\2@>@/
  10469. {@t\1@>@/
  10470. return false;@t\2@>@/
  10471. }@/
  10472. int PackLayout::heightForWidth(int width) const
  10473. {
  10474. return doLayout(QRect(0, 0, width, 0), true);
  10475. }
  10476. void PackLayout::setGeometry(const QRect &rect)
  10477. {
  10478. QLayout::setGeometry(rect);
  10479. doLayout(rect, false);
  10480. }
  10481. QSize PackLayout::sizeHint() const
  10482. {
  10483. return minimumSize();
  10484. }
  10485. @ It was mentioned previously that this layout is capable of lining widgets up
  10486. in a row or presenting them in a column. This is done with the
  10487. |setOrientation()| method.
  10488. @<PackLayout Implementation@>=
  10489. void PackLayout::setOrientation(Qt::Orientations direction)
  10490. {
  10491. orientation = direction;
  10492. doLayout(geometry(), false);
  10493. }
  10494. @* The SceneButton Class.
  10495. \noindent Ordinarily, mouse down events that are passed from a |QGraphicsView|
  10496. to an interactive |QGraphicsScene| will continue to pass that click down to an
  10497. item in the scene. This class is used when we are interested in a click anywhere
  10498. in the view and it doesn'@q'@>t really matter where in the scene that click occurred
  10499. or even if there is a graphics item at that point. Any click passed to the
  10500. |SceneButton| will cause the scene to emit a signal containing the screen
  10501. coordinates of the click.
  10502. This was originally designed for use in the |WidgetDecorator| class. While the
  10503. functionality provided is not currently used, the original plan was to use this
  10504. to provide access to configuration options.
  10505. It is possible that this class is no longer necessary even if features it was
  10506. made for are implemented.
  10507. @<Class declarations@>=
  10508. class SceneButton : public QGraphicsScene@/
  10509. {@/
  10510. Q_OBJECT@;
  10511. public:@/
  10512. SceneButton();
  10513. ~SceneButton();
  10514. protected:@/
  10515. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
  10516. signals:@/
  10517. void clicked(QPoint pos);
  10518. };
  10519. @ The implementation is trivial.
  10520. @<SceneButton Implementation@>=
  10521. SceneButton::SceneButton() : QGraphicsScene()@/
  10522. {
  10523. /* Nothing has to be done here. */
  10524. }
  10525. SceneButton::~SceneButton()
  10526. {
  10527. /* Nothing has to be done here. */
  10528. }
  10529. void SceneButton::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
  10530. {
  10531. emit clicked(mouseEvent->buttonDownScreenPos(mouseEvent->button()));
  10532. }
  10533. @* The WidgetDecorator Class.
  10534. \noindent The |WidgetDecorator| class provides a way to label various widgets
  10535. while also providing additional options for interacting with them. The
  10536. decoration can exist to the left or atop the widget being decorated. When the
  10537. label is to the left of the widget, the label text is rotated.
  10538. This class is likely to change considerably in the future as features are added
  10539. that allow actions to be added to the decoration to allow various configuration
  10540. options.
  10541. @<Class declarations@>=
  10542. class WidgetDecorator : public QWidget@/
  10543. {
  10544. Q_OBJECT@;
  10545. PackLayout *layout;
  10546. QGraphicsView *label;
  10547. QGraphicsTextItem *text;
  10548. SceneButton *scene;
  10549. public:@/
  10550. WidgetDecorator(QWidget *widget, const QString &labeltext,@|
  10551. Qt::Orientations@, orientation = Qt::Horizontal,@|
  10552. QWidget *parent = NULL, Qt::WindowFlags f = 0);
  10553. ~WidgetDecorator();
  10554. void setBackgroundBrush(QBrush background);
  10555. void setTextColor(QColor color);
  10556. };
  10557. @ Almost everything this class currently does is handled in the constructor.
  10558. @<WidgetDecorator Implementation@>=
  10559. WidgetDecorator::WidgetDecorator(QWidget *widget, const QString &labeltext,
  10560. Qt::Orientations orientation,
  10561. QWidget *parent, Qt::WindowFlags f)@/:
  10562. QWidget(parent, f), label(new QGraphicsView()),
  10563. scene(new SceneButton())@t\2@>@/
  10564. {
  10565. layout = new PackLayout(this);
  10566. layout->setOrientation(orientation);
  10567. @<Prepare the graphics view@>@;
  10568. @<Add the label to the scene@>@;
  10569. @<Adjust the decoration width@>@;
  10570. @<Pack widgets into the layout@>@;
  10571. }
  10572. @ The decoration is a |QGraphicsView|. To get this to look right, we need to
  10573. make sure there aren'@q'@>t any scroll bars and there shouldn'@q'@>t be a frame
  10574. surrounding it. While we'@q'@>re at it, we allow it to accept clicks, though this
  10575. functionality is not yet used.
  10576. @<Prepare the graphics view@>=
  10577. label->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  10578. label->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  10579. label->setFrameShape(QFrame::NoFrame);
  10580. label->setInteractive(true);
  10581. @ The |QGraphicsView| needs a scene to display anything. The scene consists of a
  10582. background which, by default is solid cyan. This can be changed later by setting
  10583. a different background brush. Text also needs to be added to the scene. If the
  10584. decoration is to the left of the widget, the text needs to be rotated.
  10585. @<Add the label to the scene@>=
  10586. scene->setBackgroundBrush(Qt::cyan);
  10587. text = scene->addText(labeltext);
  10588. if(orientation == Qt::Horizontal)
  10589. {
  10590. text->rotate(270.0);
  10591. }
  10592. label->setScene(scene);
  10593. @ The decoration should have the text centered in the view. The widget should
  10594. also be no wider (or taller for horizontal orientation) than necessary for the
  10595. text.
  10596. The case for horizontal orientation here may seem a little strange, however the
  10597. dimensions of the bounding rectangle are not affected by rotation. This means
  10598. that even though we want the width of the rotated text, this is the same as the
  10599. height of the text.
  10600. @<Adjust the decoration width@>=
  10601. if(orientation == Qt::Horizontal)
  10602. {
  10603. label->setMaximumWidth((int)(text->boundingRect().height() + 1));
  10604. }
  10605. else
  10606. {
  10607. label->setMaximumHeight((int)(text->boundingRect().height() + 1));
  10608. }
  10609. label->centerOn(text);
  10610. @ Once the decoration is ready, the decoration and the widget being decorated
  10611. can be added to the layout. A minimum size for the compound widget is also
  10612. calculated.
  10613. @<Pack widgets into the layout@>=
  10614. layout->addWidget(label);
  10615. layout->addWidget(widget);
  10616. if(orientation == Qt::Horizontal)
  10617. {
  10618. setMinimumSize(widget->sizeHint().width() + label->sizeHint().width(),
  10619. widget->sizeHint().height());
  10620. }
  10621. else
  10622. {
  10623. setMinimumSize(widget->sizeHint().width(),
  10624. widget->sizeHint().height() + label->sizeHint().height());
  10625. }
  10626. @ As mentioned previously, it is possible to change the background pattern for
  10627. the decoration. It is also possible to change the color of the text.
  10628. @<WidgetDecorator Implementation@>=
  10629. void WidgetDecorator::setBackgroundBrush(QBrush background)
  10630. {
  10631. scene->setBackgroundBrush(background);
  10632. }
  10633. void WidgetDecorator::setTextColor(QColor color)
  10634. {
  10635. text->setDefaultTextColor(color);
  10636. }
  10637. @ Finally, there is a destructor.
  10638. @<WidgetDecorator Implementation@>=
  10639. WidgetDecorator::~WidgetDecorator()
  10640. {
  10641. /* Nothing has to be done here. */
  10642. }
  10643. @ In order to create a decorated widget from a script, we need these functions.
  10644. @<Function prototypes for scripting@>=
  10645. void setWidgetDecoratorProperties(QScriptValue value, QScriptEngine *engine);
  10646. QScriptValue constructWidgetDecorator(QScriptContext *context,
  10647. QScriptEngine *engine);
  10648. @ The scripting engine must be informed of this function.
  10649. @<Set up the scripting engine@>=
  10650. constructor = engine->newFunction(constructWidgetDecorator);
  10651. value = engine->newQMetaObject(&WidgetDecorator::staticMetaObject, constructor);
  10652. engine->globalObject().setProperty("WidgetDecorator", value);
  10653. @ The constructor is slightly more complex than other script constructors, but
  10654. still simple.
  10655. @<Functions for scripting@>=
  10656. QScriptValue constructWidgetDecorator(QScriptContext *context,
  10657. QScriptEngine *engine)
  10658. {
  10659. QWidget *widget = argument<QWidget *>(0, context);
  10660. QString text = argument<QString>(1, context);
  10661. Qt::Orientations@, orientation;
  10662. switch(argument<int>(2, context))@/
  10663. {@t\1@>@/
  10664. case 2:@/
  10665. orientation = Qt::Vertical;
  10666. break;
  10667. default:@/
  10668. orientation = Qt::Horizontal;
  10669. break;@t\2@>@/
  10670. }
  10671. QScriptValue object =
  10672. engine->newQObject(new WidgetDecorator(widget, text, orientation));
  10673. setWidgetDecoratorProperties(object, engine);
  10674. return object;
  10675. }
  10676. void setWidgetDecoratorProperties(QScriptValue value, QScriptEngine *engine)
  10677. {
  10678. setQWidgetProperties(value, engine);
  10679. }
  10680. @* The LogEditWindow Class.
  10681. \noindent This class will be depreciated in a future release once I have
  10682. confirmed that the class can be replaced by the configuration system. It has not
  10683. been updated to support new functionality added in version 1.2.3 and use of this
  10684. class is highly discouraged.
  10685. While the logging window provided in the example configuration is fine for
  10686. recording an existing roast, there are some who would like to be able to use
  10687. \pn{} to work with data collected with a manual logger. Different controls are
  10688. useful in such a case. The |LogEditWindow| provides this.
  10689. @<Class declarations@>=
  10690. class LogEditWindow : public QMainWindow@/
  10691. {@t\1@>@/
  10692. Q_OBJECT@;
  10693. QWidget *centralWidget;
  10694. PackLayout *mainLayout;
  10695. QHBoxLayout *addRowsLayout;
  10696. QLabel *startTimeLabel;
  10697. QTimeEdit *startTime;
  10698. QLabel *endTimeLabel;
  10699. QTimeEdit *endTime;
  10700. QLabel *intervalLabel;
  10701. QSpinBox *interval;
  10702. QPushButton *addRows;
  10703. QAction *saveXml;
  10704. QAction *saveCsv;
  10705. QAction *openXml;
  10706. MeasurementModel *model;
  10707. QTableView *log;@/
  10708. @t\4@>private slots@t\kern-3pt@>:@/
  10709. void addTheRows();
  10710. void saveXML();
  10711. void saveCSV();
  10712. void openXML();@/
  10713. protected:@/
  10714. void closeEvent(QCloseEvent *event);@/
  10715. public:@/
  10716. LogEditWindow();@t\2@>@/
  10717. }@t\kern-3pt@>;
  10718. @ This window provides controls for adding rows to a measurement. Typically, the
  10719. data on a manual roast log will have measurements at regular intervals with the
  10720. possible exception of a few points where there are control changes or the end of
  10721. the batch. The routine for adding rows is capable of adding a single row, rows
  10722. in a range of times at regular intervals, or rows in a range of times at regular
  10723. intervals plus one time at the end.
  10724. @<LogEditWindow Implementation@>=
  10725. void LogEditWindow::addTheRows()
  10726. {
  10727. QTime s = startTime->time();
  10728. while(s < endTime->time())
  10729. {
  10730. model->newMeasurement(Measurement(0, s), 1);
  10731. s = s.addSecs(interval->value());
  10732. }
  10733. model->newMeasurement(Measurement(0, endTime->time()), 1);
  10734. }
  10735. @ The window is prepared in its constructor.
  10736. @<LogEditWindow Implementation@>=
  10737. LogEditWindow::LogEditWindow() : QMainWindow(NULL),
  10738. centralWidget(new QWidget(NULL)), mainLayout(new PackLayout(0)),@|
  10739. addRowsLayout(new QHBoxLayout(NULL)),
  10740. startTimeLabel(new QLabel("Start Time")),@|
  10741. startTime(new QTimeEdit(QTime(0, 0, 0, 0))),@|
  10742. endTimeLabel(new QLabel("End Time")),
  10743. endTime(new QTimeEdit(QTime(0, 20, 0, 0))),@|
  10744. intervalLabel(new QLabel("Interval (seconds)")),@|
  10745. interval(new QSpinBox()),
  10746. addRows(new QPushButton("Add Rows")),@|
  10747. saveXml(new QAction(tr("Save Profile As..."), NULL)),@|
  10748. saveCsv(new QAction(tr("Export CSV"), NULL)),@|
  10749. openXml(new QAction(tr("Load Target Profile..."), NULL)),@|
  10750. model(new MeasurementModel()),
  10751. log(new QTableView())@/
  10752. {
  10753. @<Restore editor window geometry from settings@>@;
  10754. @<Set up the editor control bar@>@;
  10755. @<Prepare the model@>@;
  10756. @<Prepare the log table@>@;
  10757. mainLayout->addItem(addRowsLayout);
  10758. mainLayout->addWidget(log);
  10759. centralWidget->setLayout(mainLayout);
  10760. setCentralWidget(centralWidget);
  10761. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  10762. fileMenu->addAction(openXml);
  10763. connect(openXml, SIGNAL(triggered()), this, SLOT(openXML()));
  10764. fileMenu->addAction(saveXml);
  10765. connect(saveXml, SIGNAL(triggered()), this, SLOT(saveXML()));
  10766. fileMenu->addAction(saveCsv);
  10767. connect(saveCsv, SIGNAL(triggered()), this, SLOT(saveCSV()));
  10768. }
  10769. @ The window keeps its previous size and location in settings. These need to be
  10770. restored when a new window is created.
  10771. @<Restore editor window geometry from settings@>=
  10772. QSettings settings;
  10773. resize(settings.value("logSize", QSize(620,400)).toSize());
  10774. move(settings.value("logPos", QPoint(200,60)).toPoint());
  10775. @ When a new window is opened, it starts with an empty profile. If this is used
  10776. to manually enter a profile rather than edit an existing profile, rows will need
  10777. to be added. For this, we provide a set of controls where a start time, an end
  10778. time, and an interval in seconds is specified along with a button that, when
  10779. pressed, will produce a row in the model for the starting time, the ending time,
  10780. and regularly spaced times between the two. If only a single row is needed, this
  10781. can be produced by setting the start and end times the same.
  10782. @<Set up the editor control bar@>=
  10783. mainLayout->setOrientation(Qt::Vertical);
  10784. addRowsLayout->addSpacing(10);
  10785. addRowsLayout->addWidget(startTimeLabel);
  10786. addRowsLayout->addWidget(startTime);
  10787. addRowsLayout->addSpacing(10);
  10788. startTime->setDisplayFormat("mm:ss");
  10789. addRowsLayout->addWidget(endTimeLabel);
  10790. addRowsLayout->addWidget(endTime);
  10791. addRowsLayout->addSpacing(10);
  10792. endTime->setDisplayFormat("mm:ss");
  10793. addRowsLayout->addWidget(intervalLabel);
  10794. addRowsLayout->addWidget(interval);
  10795. addRowsLayout->addSpacing(10);
  10796. interval->setRange(0, 60);
  10797. interval->setValue(30);
  10798. addRowsLayout->addWidget(addRows);
  10799. addRowsLayout->addSpacing(10);
  10800. connect(addRows, SIGNAL(clicked()), this, SLOT(addTheRows()));
  10801. @ The model will have three columns: Time, Temperature, and Annotation. This
  10802. probably should not be hard coded.
  10803. @<Prepare the model@>=
  10804. model->setHeaderData(0, Qt::Horizontal, "Time");
  10805. model->setHeaderData(1, Qt::Horizontal, "Temperature");
  10806. model->setHeaderData(2, Qt::Horizontal, "Annotation");
  10807. model->clear();
  10808. @ The profile is presented in a table view. The columns should be wide enough to
  10809. contain a label, the data contained in the column, and an editor delegate.
  10810. @<Prepare the log table@>=
  10811. log->setModel(model);
  10812. log->setColumnWidth(0, 100);
  10813. log->setColumnWidth(1, 100);
  10814. log->setColumnWidth(2, 100);
  10815. @ Most users will want to save a profile after they'@q'@>ve edited it. We also
  10816. provide CSV export here. Note that this class only supports logs with a single
  10817. temperature and a single annotation column. As the class is considered
  10818. depreciated, it will not be extended to support arbitrarily many columns.
  10819. @<LogEditWindow Implementation@>=
  10820. void LogEditWindow::saveXML()
  10821. {
  10822. QSettings settings;
  10823. QString lastDir = settings.value("lastDirectory").toString();
  10824. QString filename = QFileDialog::getSaveFileName(this, tr("Save Log As..."),
  10825. lastDir, "", 0);
  10826. QFile file(filename);
  10827. XMLOutput writer(model, &file, 0);
  10828. writer.addTemperatureColumn("Temperature", 1);
  10829. writer.addAnnotationColumn("Annotation", 2);
  10830. if(writer.output())
  10831. {
  10832. QFileInfo info(filename);
  10833. QDir directory = info.dir();
  10834. lastDir = directory.path();
  10835. settings.setValue("lastDirectory", lastDir);
  10836. }
  10837. }
  10838. void LogEditWindow::saveCSV()
  10839. {
  10840. QSettings settings;
  10841. QString lastDir = settings.value("lastDirectory").toString();
  10842. QString filename = QFileDialog::getSaveFileName(this, tr("Export As..."),
  10843. lastDir, "", 0);
  10844. QFile file(filename);
  10845. CSVOutput writer(model, &file, 0);
  10846. writer.addTemperatureColumn("Temperature", 1);
  10847. writer.addAnnotationColumn("Annotation", 2);
  10848. if(writer.output())
  10849. {
  10850. QFileInfo info(filename);
  10851. QDir directory = info.dir();
  10852. lastDir = directory.path();
  10853. settings.setValue("lastDirectory", lastDir);
  10854. }
  10855. }
  10856. @ Some may want to open a previously saved profile, for example, to adjust the
  10857. position of an annotation. Note that this class is not appropriate for editing
  10858. profiles with more than one temperature column.
  10859. @<LogEditWindow Implementation@>=
  10860. void LogEditWindow::openXML()
  10861. {
  10862. QSettings settings;
  10863. QString lastDir = settings.value("lastDirectory").toString();
  10864. QString filename = QFileDialog::getOpenFileName(this, tr("Open XML Log..."),
  10865. lastDir, "", 0);
  10866. if(filename.isNull())
  10867. {
  10868. return;
  10869. }
  10870. QFile file(filename);
  10871. XMLInput reader(&file, 1);
  10872. connect(&reader, SIGNAL(measure(Measurement, int)),
  10873. model, SLOT(newMeasurement(Measurement, int)));
  10874. connect(&reader, SIGNAL(annotation(QString, int, int)),
  10875. model, SLOT(newAnnotation(QString, int, int)));
  10876. if(reader.input())
  10877. {
  10878. QFileInfo info(filename);
  10879. setWindowTitle(QString(tr("%1 - %2")).@|
  10880. arg(QCoreApplication::applicationName()).arg(info.baseName()));
  10881. QDir directory = info.dir();
  10882. lastDir = directory.path();
  10883. settings.setValue("lastDirectory", lastDir);
  10884. }
  10885. }
  10886. @ The window should remember its last size and position, so we store this
  10887. information in settings when the window is closed.
  10888. @<LogEditWindow Implementation@>=
  10889. void LogEditWindow::closeEvent(QCloseEvent *event)
  10890. {
  10891. QSettings settings;
  10892. settings.setValue("logSize", size());
  10893. settings.setValue("logPos", pos());
  10894. event->accept();
  10895. }
  10896. @ One function is required to instantiate this class from a script.
  10897. @<Function prototypes for scripting@>=
  10898. QScriptValue constructLogEditWindow(QScriptContext *context,
  10899. QScriptEngine *engine);
  10900. @ The engine must be informed of this function.
  10901. @<Set up the scripting engine@>=
  10902. constructor = engine->newFunction(constructLogEditWindow);
  10903. value = engine->newQMetaObject(&LogEditWindow::staticMetaObject, constructor);
  10904. engine->globalObject().setProperty("LogEditWindow", value);
  10905. @ The constructor just creates the window and passes it back to the engine.
  10906. @<Functions for scripting@>=
  10907. QScriptValue constructLogEditWindow(QScriptContext *, QScriptEngine *engine)
  10908. {
  10909. QScriptValue object = engine->newQObject(new LogEditWindow);
  10910. return object;
  10911. }
  10912. @** File IO.
  10913. \noindent So far, the data is all stored in memory. It is often useful to save
  10914. data to a file or read back previously saved data. Presently, two formats are
  10915. supported: an XML format which can also be read back in and CSV which can easily
  10916. be used with many external tools.
  10917. File IO is handled by a few classes: one per input format and one per output
  10918. format. The classes in the following sections should be simple enough to follow
  10919. that it should be clear how to extend \pn{} to support other formats if needed.
  10920. Should additional output formats be required, it may be beneficial to
  10921. reimplement the serializers as subclasses of a new abstract serializer class in
  10922. order to share common code among them where reusing \cweb{} chunks is not an
  10923. appropriate technique.
  10924. @* XML Output.
  10925. \noindent An XML format has been chosen as the native format for \pn{} because
  10926. of Qt'@q'@>s excellent support for reading and writing such documents. Using this
  10927. capability is less error prone than developing a new, more compact format.
  10928. Another reason to choose XML is that it becomes quite easy to modify saved data
  10929. in a text editor and still end up with something \pn{} will understand.
  10930. The structure of the file we will produce is simple, however it has been
  10931. modified from a simpler structure that was used in versions of Typica prior to
  10932. 1.2.3. How to read these files can be determined by the document type found at
  10933. the start of the file. At the start of the file, there should be one or more
  10934. {\tt <tempseries>} elements and one or more {\tt <noteseries>} elements. These
  10935. are empty elements with a {\tt name} attribute which can be used to label the
  10936. column in a view. Once these column declarations have been written, a
  10937. {\tt <roast>} element is produced which contains a set of zero or more
  10938. {\tt <tuple>} elements. Each tuple contains one {\tt <time>} element containing
  10939. the time of the measurement relative to the start of the batch and optionally
  10940. one or more {\tt <temperature>} and {\tt <annotation>} elements containing
  10941. measurement and annotation data associated with that time. The
  10942. {\tt <temperature>} and {\tt <annotation>} elements have a {\tt series}
  10943. attribute where the value of the attribute matches the {\tt name} attribute of a
  10944. {\tt <tempseries>} or {\tt <noteseries>} element which allows each measurement
  10945. to be placed in the correct data series regardless of element ordering in the
  10946. document.
  10947. There are certain oddities about this format compared with other XML based
  10948. formats. The order of some elements in the current implementation affects the
  10949. behavior of the program and there is no longer a proper root element. This
  10950. format may be extended in future versions of \pn{} to support additional
  10951. functionality or to improve the robustness of the format. Should such
  10952. modifications occur, an effort should be made to ensure that \pn{} continues to
  10953. support the import of old data.
  10954. As of version 1.0.8, this class is derived from |QObject| for easier integration
  10955. with the scripting engine.
  10956. The |temperatureColumns| and |annotationColumns| member data structures are
  10957. currently a |QMap| rather than a |QHash| because the number of data series in a
  10958. single file is likely to be small enough that the difference in lookup time
  10959. should be negligeable and the ability to iterate over the keys in the |QMap| in
  10960. sorted order is useful.
  10961. @<Class declarations@>=
  10962. class XMLOutput : public QObject@/
  10963. {@/
  10964. Q_OBJECT@;@/
  10965. MeasurementModel *data;
  10966. QIODevice *out;
  10967. int time;
  10968. QMap<int, QString> temperatureColumns;
  10969. QMap<int, QString> controlColumns;
  10970. QMap<int, QString> annotationColumns;
  10971. public:@/
  10972. XMLOutput(MeasurementModel *model, QIODevice *device, int timec = 0);
  10973. void addTemperatureColumn(const QString &series, int column);
  10974. void addControlColumn(const QString &series, int column);
  10975. void addAnnotationColumn(const QString &series, int column);
  10976. void setModel(MeasurementModel *model);
  10977. void setTimeColumn(int column);
  10978. void setDevice(QIODevice *device);
  10979. bool output();
  10980. };
  10981. @ The interesting part of this class is the |output| routine. This goes over the
  10982. data in the model and constructs an appropriate XML document. If the operation
  10983. fails, the function returns |false|, otherwise it returns |true|.
  10984. @<XMLOutput Implementation@>=
  10985. bool XMLOutput::output()@t\2\2@>@/
  10986. {@t\1@>@/
  10987. if(!out->open(QIODevice::WriteOnly | QIODevice::Text))@/
  10988. {@t\1@>@/
  10989. return false;@t\2@>@/
  10990. }@/
  10991. QXmlStreamWriter xmlout(out);
  10992. xmlout.writeStartDocument("1.0");
  10993. xmlout.writeDTD("<!DOCTYPE roastlog3.0>");
  10994. xmlout.writeStartElement("roastlog");
  10995. @<Output the column declarations@>@;
  10996. xmlout.writeStartElement("roast");
  10997. bool oresult;
  10998. for(int i = 0; i < data->rowCount(); i++)@/
  10999. {
  11000. @<Check if row should be output@>@;
  11001. if(oresult)
  11002. {
  11003. @<Output tuple element@>@;
  11004. }
  11005. }
  11006. xmlout.writeEndElement();
  11007. xmlout.writeEndElement();
  11008. xmlout.writeEndDocument();
  11009. out->close();@/
  11010. return true;@t\2@>@/
  11011. }
  11012. @ Temperature column declarations are output before annotation column
  11013. declarations. Within each category, column declarations are output in order by
  11014. column number.
  11015. @<Output the column declarations@>=
  11016. foreach(int c, temperatureColumns.keys())
  11017. {
  11018. xmlout.writeStartElement("tempseries");
  11019. xmlout.writeAttribute("name", temperatureColumns.value(c));
  11020. xmlout.writeEndElement();
  11021. }
  11022. foreach(int c, controlColumns.keys())
  11023. {
  11024. xmlout.writeStartElement("controlseries");
  11025. xmlout.writeAttribute("name", controlColumns.value(c));
  11026. xmlout.writeEndElement();
  11027. }
  11028. foreach(int c, annotationColumns.keys())
  11029. {
  11030. xmlout.writeStartElement("noteseries");
  11031. xmlout.writeAttribute("name", annotationColumns.value(c));
  11032. xmlout.writeEndElement();
  11033. }
  11034. @ When checking a row in the model to determine if it contains values that need
  11035. to be written, we want to know if any of the temperature or annotation columns
  11036. contain a value. If at least one of these columns is not empty for this row, we
  11037. need to output a tuple for that row.
  11038. @<Check if row should be output@>=
  11039. oresult = false;@/
  11040. foreach(int c, temperatureColumns.keys())@/
  11041. {@t\1@>@/
  11042. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11043. !(data->data(data->index(i, c), Qt::DisplayRole).toString().isEmpty()))@/
  11044. {@t\1@>@/
  11045. oresult = true;
  11046. break;@t\2@>@/
  11047. }@t\2@>@/
  11048. }@/
  11049. foreach(int c, controlColumns.keys())
  11050. {
  11051. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11052. !(data->data(data->index(i, c), Qt::DisplayRole).toString().isEmpty()))
  11053. {
  11054. oresult = true;
  11055. break;
  11056. }
  11057. }
  11058. if(oresult == false)@/
  11059. {@t\1@>@/
  11060. foreach(int c, annotationColumns.keys())@/
  11061. {@t\1@>@/
  11062. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11063. !(data->data(data->index(i, c), Qt::DisplayRole).toString().
  11064. isEmpty()))@/
  11065. {@t\1@>@/
  11066. oresult = true;
  11067. break;@t\2@>@/
  11068. }@t\2@>@/
  11069. }@t\2@>@/
  11070. }
  11071. @ Now that we know that values from the current row should be output, we can
  11072. produce a {\tt <tuple>} element, a {\tt <time>} element for that tuple, and then
  11073. iterate over the set of columns we might want to output, producing an
  11074. appropriate element for each non-empty column for that row.
  11075. @<Output tuple element@>=
  11076. xmlout.writeStartElement("tuple");
  11077. xmlout.writeTextElement("time", data->data(data->index(i, time),
  11078. Qt::DisplayRole).toString());
  11079. foreach(int c, temperatureColumns.keys())@/
  11080. {
  11081. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11082. !(data->data(data->index(i, c), Qt::DisplayRole).toString().isEmpty()))@/
  11083. {
  11084. xmlout.writeStartElement("temperature");
  11085. xmlout.writeAttribute("series", temperatureColumns.value(c));
  11086. if(data->data(data->index(i, c), Qt::UserRole).toMap().contains("relative"))
  11087. {
  11088. if(data->data(data->index(i, c), Qt::UserRole).toMap().value("relative").toBool())
  11089. {
  11090. xmlout.writeAttribute("relative", "true");
  11091. }
  11092. }
  11093. xmlout.writeCharacters(data->data(data->index(i, c), Qt::DisplayRole).
  11094. toString());
  11095. xmlout.writeEndElement();
  11096. }
  11097. }
  11098. foreach(int c, controlColumns.keys())
  11099. {
  11100. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11101. !(data->data(data->index(i, c), Qt::DisplayRole).toString().isEmpty()))
  11102. {
  11103. xmlout.writeStartElement("control");
  11104. xmlout.writeAttribute("series", controlColumns.value(c));
  11105. xmlout.writeCharacters(data->data(data->index(i, c), Qt::DisplayRole).toString());
  11106. xmlout.writeEndElement();
  11107. }
  11108. }
  11109. foreach(int c, annotationColumns.keys())@/
  11110. {
  11111. if(data->data(data->index(i, c), Qt::DisplayRole).isValid() &&
  11112. !(data->data(data->index(i, c), Qt::DisplayRole).toString().isEmpty()))@/
  11113. {
  11114. xmlout.writeStartElement("annotation");
  11115. xmlout.writeAttribute("series", annotationColumns.value(c));
  11116. xmlout.writeCharacters(data->data(data->index(i, c), Qt::DisplayRole).
  11117. toString());
  11118. xmlout.writeEndElement();
  11119. }
  11120. }
  11121. xmlout.writeEndElement();
  11122. @ The rest of the class just initializes the private member data.
  11123. @<XMLOutput Implementation@>=
  11124. XMLOutput::XMLOutput(MeasurementModel *model, QIODevice *device, int timec)
  11125. : QObject(NULL), data(model), out(device), time(timec)@/
  11126. {
  11127. /* Nothing has to be done here. */
  11128. }@;
  11129. void XMLOutput::setModel(MeasurementModel *model)
  11130. {
  11131. data = model;
  11132. }
  11133. void XMLOutput::setTimeColumn(int column)
  11134. {
  11135. time = column;
  11136. }
  11137. void XMLOutput::setDevice(QIODevice *device)
  11138. {
  11139. out = device;
  11140. }
  11141. @ As of version 1.2.3, the old |setTemperatureColumn()| and
  11142. |setAnnotationColumn()| methods have been replaced with the
  11143. |addTemperatureColumn()| and |addAnnotationColumn()| methods respectively. The
  11144. main difference is that the new methods take a column name in addition to a
  11145. number and it is now possible to specify multiple columns of each category for
  11146. export.
  11147. @<XMLOutput Implementation@>=
  11148. void XMLOutput::addTemperatureColumn(const QString &series, int column)
  11149. {
  11150. temperatureColumns.insert(column, series);
  11151. }
  11152. void XMLOutput::addControlColumn(const QString &series, int column)
  11153. {
  11154. controlColumns.insert(column, series);
  11155. }
  11156. void XMLOutput::addAnnotationColumn(const QString &series, int column)
  11157. {
  11158. annotationColumns.insert(column, series);
  11159. }
  11160. @* XML Input.
  11161. \noindent Once model data can be saved to a file, it is useful to be able to
  11162. read that data back in. This is a little different from reading data out of a
  11163. model as more than one object is potentially interested in the data. Instead, we
  11164. emit signals for measurements and annotations. This class has been modified to
  11165. support both the current (as of version 1.2.3) output of the |XMLOutput| class
  11166. and the older version. If changes are made to |XMLOutput| this class may also
  11167. need to be modified.
  11168. The main differences in the current version of this class are that the first
  11169. column is specified rather than specifying temperature and annotation columns
  11170. separately and additional signals are emitted to allow views to prepare for an
  11171. arbitrary number of columns.
  11172. The |newTemperatureColumn| and |newAnnotationColumn| signals can be used to set
  11173. up column headers while the |lastColumn| signal can be used to shift live data
  11174. streams to unoccupied columns.
  11175. @<Class declarations@>=
  11176. class XMLInput : public QObject@/
  11177. {
  11178. Q_OBJECT@;
  11179. int firstc;
  11180. QIODevice *in;
  11181. public:@/
  11182. XMLInput(QIODevice *input, int c);
  11183. void setFirstColumn(int column);
  11184. void setDevice(QIODevice *device);
  11185. bool input();
  11186. signals:@/
  11187. void measure(Measurement, int);
  11188. void annotation(QString, int, int);
  11189. void newTemperatureColumn(int, QString);
  11190. void newAnnotationColumn(int, QString);
  11191. void lastColumn(int);
  11192. };
  11193. @ The main point of interest here is the |input()| method. If the file is read
  11194. successfully, |true| is returned. Otherwise, |false| is returned.
  11195. @<XMLInput Implementation@>=
  11196. bool XMLInput::input()@t\2\2@>@/
  11197. {@t\1@>@/
  11198. if(!in->open(QIODevice::ReadOnly | QIODevice::Text))@/
  11199. {@t\1@>@/
  11200. return false;@t\2@>@/
  11201. }@/
  11202. QXmlStreamReader xmlin(in);
  11203. QMap<QString, int> temperatureColumns;
  11204. QMap<QString, int> annotationColumns;
  11205. int nextColumn = firstc;
  11206. @<Read column declarations@>@;
  11207. QTime timeval = QTime();
  11208. double tempval = 0;
  11209. QString noteval = QString();
  11210. int column;
  11211. int counter = 0;@/
  11212. while(!xmlin.atEnd())@/
  11213. {@/
  11214. @<Read XML file@>@;
  11215. }@/
  11216. return true;@t\2@>@/
  11217. }
  11218. @ A data file may or may not contain elements that specify the name of a column.
  11219. In order to determine how to proceed, we should check the doctype of the input
  11220. file. This should be the first element of the input file encountered.
  11221. \danger There is not nearly enough error checking here.
  11222. \endanger
  11223. @<Read column declarations@>=
  11224. while(!xmlin.isDTD())
  11225. {
  11226. xmlin.readNext();
  11227. }
  11228. if(xmlin.isDTD())
  11229. {
  11230. if(xmlin.text() == "<!DOCTYPE roastlog>")
  11231. {
  11232. @<Emit old format column specification@>@;
  11233. }
  11234. else
  11235. {
  11236. xmlin.readNext();
  11237. @<Scan for column declarations and emit@>@;
  11238. }
  11239. }
  11240. @ Old format data will not have column declarations. This means that we must
  11241. produce a default set of signals rather than waiting to read elements describing
  11242. the columns.
  11243. @<Emit old format column specification@>=
  11244. emit newTemperatureColumn(firstc, "Bean");
  11245. emit newAnnotationColumn(firstc + 1, "Note");
  11246. emit lastColumn(firstc + 1);
  11247. @ The current format will have column declarations prior to the {\tt <roast>}
  11248. element. We can just read until we hit that element and emit the appropriate
  11249. signals as elements are encountered.
  11250. @<Scan for column declarations and emit@>=
  11251. while(xmlin.name() != "roast")
  11252. {
  11253. if(xmlin.isStartElement())
  11254. {
  11255. if((xmlin.name() == "tempseries") || (xmlin.name() == "controlseries"))
  11256. {
  11257. temperatureColumns.insert(xmlin.attributes().value("name").
  11258. toString(),
  11259. nextColumn);
  11260. emit newTemperatureColumn(nextColumn,
  11261. xmlin.attributes().value("name").
  11262. toString());
  11263. nextColumn++;
  11264. }
  11265. else if(xmlin.name() == "noteseries")
  11266. {
  11267. annotationColumns.insert(xmlin.attributes().value("name").
  11268. toString(), nextColumn);
  11269. emit newAnnotationColumn(nextColumn,
  11270. xmlin.attributes().value("name").
  11271. toString());
  11272. nextColumn++;
  11273. }
  11274. }
  11275. xmlin.readNext();
  11276. }
  11277. emit lastColumn(nextColumn - 1);
  11278. @ Now we are ready to read measurements from the file. When encountering a
  11279. {\tt <time>} element, we record the time and move on. For {\tt <temperature>}
  11280. and {\tt <annotation>} elements, we emit the appropriate signal. This is handled
  11281. slightly differently depending on which version of the file format is being
  11282. used. Note that there is not nearly enough error checking here and we are
  11283. basically ignoring {\tt <tuple>} elements.
  11284. Due to the typically large number of measurements taken over the course of a
  11285. roast and the amount of time often taken to process these measurements when they
  11286. are read from a file, there is a need to periodically pass control back to the
  11287. event loop to remain responsive to user input.
  11288. @<Read XML file@>=
  11289. xmlin.readNext();
  11290. if(xmlin.isStartElement())
  11291. {
  11292. @<Read measurement data@>@;
  11293. }
  11294. counter++;
  11295. if(counter % 100 == 0)
  11296. {
  11297. QCoreApplication::processEvents();
  11298. }
  11299. @ When reading start elements, it is safe to ignore {\tt <tuple>} and
  11300. {\tt <roast>}. Technically, this means that the program can read certain types
  11301. of invalid data. The Robustness Principle\nfnote{``Be liberal in what you
  11302. accept, and conservative in what you send,'' --- Robert Braden, {\it RFC 1122
  11303. \S 1.2.2}} is generally applicable to any type of data exchange. That said,
  11304. malformed data is not guaranteed readable in the future, even if it does work
  11305. now.
  11306. \danger One set of test input caused this code to emit an empty annotation for
  11307. every measurement. This is the reason for wrapping the annotation signal
  11308. emission to check for this. The detected annotation elements were not present in
  11309. the input stream and I have absolutely no idea where the program came up with
  11310. them. \endanger
  11311. @<Read measurement data@>=
  11312. if(xmlin.name() == "time")
  11313. {
  11314. timeval = QTime::fromString(xmlin.readElementText(), "mm:ss.zzz");
  11315. }
  11316. else if(xmlin.name() == "temperature")
  11317. {
  11318. column = xmlin.attributes().hasAttribute("series") ?
  11319. temperatureColumns.value(xmlin.attributes().value("series").toString()) : firstc;
  11320. bool relative = (xmlin.attributes().value("relative") == "true");
  11321. tempval = xmlin.readElementText().toDouble();
  11322. Measurement measurement(tempval, timeval);
  11323. if(relative)
  11324. {
  11325. measurement.insert("relative", true);
  11326. }
  11327. emit measure(measurement, column);
  11328. }
  11329. else if(xmlin.name() == "control")
  11330. {
  11331. column = xmlin.attributes().value("series").toString().isEmpty() ?
  11332. firstc : temperatureColumns.value(xmlin.attributes().
  11333. value("series").toString());
  11334. tempval = xmlin.readElementText().toDouble();
  11335. Measurement measurement(tempval, timeval, Units::Unitless);
  11336. emit measure(measurement, column);
  11337. }
  11338. else if(xmlin.name() == "annotation")
  11339. {
  11340. column = xmlin.attributes().value("series").toString().isEmpty() ?
  11341. firstc + 1 : annotationColumns.value(xmlin.attributes().
  11342. value("series").toString());
  11343. noteval = xmlin.readElementText();
  11344. if(!noteval.isEmpty())
  11345. {
  11346. emit annotation(noteval, firstc, column);
  11347. }
  11348. }
  11349. @ The other methods just set the private member data.
  11350. @<XMLInput Implementation@>=
  11351. XMLInput::XMLInput(QIODevice *input, int c) :
  11352. firstc(c), in(input)@/
  11353. {@/
  11354. /* Nothing has to be done here. */
  11355. }
  11356. void XMLInput::setFirstColumn(int column)
  11357. {
  11358. firstc = column;
  11359. }
  11360. void XMLInput::setDevice(QIODevice *device)
  11361. {
  11362. in = device;
  11363. }
  11364. @ In order to allow scripts to instantiate the |XMLInput| class, we need a
  11365. constructor and a wrapper around the |input()| method.
  11366. @<Function prototypes for scripting@>=
  11367. QScriptValue constructXMLInput(QScriptContext *context, QScriptEngine *engine);
  11368. QScriptValue XMLInput_input(QScriptContext *context, QScriptEngine *engine);
  11369. @ The script constructor is passed to the scripting engine.
  11370. @<Set up the scripting engine@>=
  11371. constructor = engine->newFunction(constructXMLInput);
  11372. value = engine->newQMetaObject(&XMLInput::staticMetaObject, constructor);
  11373. engine->globalObject().setProperty("XMLInput", value);
  11374. @ The implementation should seem familiar.
  11375. @<Functions for scripting@>=
  11376. QScriptValue constructXMLInput(QScriptContext *context, QScriptEngine *engine)
  11377. {
  11378. QIODevice *device = argument<QIODevice *>(0, context);
  11379. QScriptValue object = engine->newQObject(new XMLInput(&*device,
  11380. argument<int>(1, context)));
  11381. object.setProperty("input", engine->newFunction(XMLInput_input));
  11382. return object;
  11383. }
  11384. QScriptValue XMLInput_input(QScriptContext *context, QScriptEngine *)
  11385. {
  11386. XMLInput *self = getself<@[XMLInput *@]>(context);
  11387. self->input();
  11388. return QScriptValue();
  11389. }
  11390. @* CSV Output.
  11391. \noindent While XML is convenient for \pn{}, other programs may not handle this
  11392. format well. For this purpose, we use a text file with comma separated values.
  11393. Data in this format can easily be handled by shell scripts, simple programs, and
  11394. any spreadsheet (though some may handle the time column poorly).
  11395. We do not need to concern ourselves with reading data in this format back in,
  11396. but there is no reason a class could not be written to do this.
  11397. The structure of this class is very similar to the |XMLOutput| class.
  11398. @<Class declarations@>=
  11399. class CSVOutput@/
  11400. {@/
  11401. MeasurementModel *data;
  11402. QIODevice *out;
  11403. int time;
  11404. QMap<int, QString> temperatureColumns;
  11405. QMap<int, QString> controlColumns;
  11406. QMap<int, QString> annotationColumns;@/
  11407. public:@/
  11408. CSVOutput(MeasurementModel *model, QIODevice *device, int timec = 0);
  11409. void addTemperatureColumn(const QString &series, int column);
  11410. void addControlColumn(const QString &series, int column);
  11411. void addAnnotationColumn(const QString &series, int column);
  11412. void setModel(MeasurementModel *model);
  11413. void setTimeColumn(int column);
  11414. void setDevice(QIODevice *device);
  11415. bool output();@/
  11416. };
  11417. @ Very little needs to be done to output the data. We open the output stream
  11418. and, if the output stream was successfully opened, we look for measurements and
  11419. output the text, remembering to output a comma between items and a newline after
  11420. each record. If the data is successfully output, |true| is returned, otherwise
  11421. we return |false|.
  11422. The comparably simple structure of the CSV format allows us to just fling the
  11423. data onto a text stream.
  11424. @<CSVOutput Implementation@>=
  11425. bool CSVOutput::output()@t\2\2@>@/
  11426. {@t\1@>@/
  11427. if(!out->open(QIODevice::WriteOnly | QIODevice::Text))@/
  11428. {@t\1@>@/
  11429. return false;@t\2@>@/
  11430. }@/
  11431. QTextStream output(out);
  11432. @<Output CSV column headers@>@;
  11433. bool oresult;
  11434. for(int i = 0; i < data->rowCount(); i++)@/
  11435. {
  11436. @<Check if row should be output@>@;
  11437. if(oresult)
  11438. {
  11439. @<Output CSV row@>@;
  11440. }
  11441. }
  11442. out->close();@/
  11443. return true;@t\2@>@/
  11444. }
  11445. @ Before writing the data, we output a row containing the name of each column.
  11446. @<Output CSV column headers@>=
  11447. output << "Time";
  11448. foreach(int c, temperatureColumns.keys())
  11449. {
  11450. output << ',' << temperatureColumns.value(c);
  11451. }
  11452. foreach(int c, controlColumns.keys())
  11453. {
  11454. output << ',' << controlColumns.value(c);
  11455. }
  11456. foreach(int c, annotationColumns.keys())
  11457. {
  11458. output << ',' << annotationColumns.value(c);
  11459. }
  11460. output << '\n';
  11461. @ Once the header information has been written, we can proceed to output the
  11462. real data. The algorithm for doing this has been changed as of version 1.2.3
  11463. with the result that most uses will now produce more delimiters than the same
  11464. data in previous versions. This should have no impact on the ability of other
  11465. programs to interact with data produced by \pn{}. The code to handle output in
  11466. this way is much easier to read. A future version might once again suppress
  11467. superfluous commas, however the presence of these commas is not considered a
  11468. serious issue at this time.
  11469. @<Output CSV row@>=
  11470. output << data->data(data->index(i, time), Qt::DisplayRole).toString();
  11471. foreach(int c, temperatureColumns.keys())
  11472. {
  11473. output << ',' << data->data(data->index(i, c), Qt::DisplayRole).toString();
  11474. }
  11475. foreach(int c, controlColumns.keys())
  11476. {
  11477. output << ',' << data->data(data->index(i, c), Qt::DisplayRole).toString();
  11478. }
  11479. foreach(int c, annotationColumns.keys())
  11480. {
  11481. output << ',' << data->data(data->index(i, c), Qt::DisplayRole).toString();
  11482. }
  11483. output << '\n';
  11484. @ The rest of the class just initializes the private member data. See notes
  11485. on the implementation of |XMLOutput|.
  11486. @<CSVOutput Implementation@>=
  11487. CSVOutput::CSVOutput(MeasurementModel *model, QIODevice *device, int timec) :
  11488. data(model), out(device), time(timec)@/
  11489. {
  11490. /* Nothing has to be done here. */
  11491. }@;
  11492. void CSVOutput::setModel(MeasurementModel *model)
  11493. {
  11494. data = model;
  11495. }
  11496. void CSVOutput::setTimeColumn(int column)
  11497. {
  11498. time = column;
  11499. }
  11500. void CSVOutput::addTemperatureColumn(const QString &series, int column)
  11501. {
  11502. temperatureColumns.insert(column, series);
  11503. }
  11504. void CSVOutput::addControlColumn(const QString &series, int column)
  11505. {
  11506. controlColumns.insert(column, series);
  11507. }
  11508. void CSVOutput::addAnnotationColumn(const QString &series, int column)
  11509. {
  11510. annotationColumns.insert(column, series);
  11511. }
  11512. void CSVOutput::setDevice(QIODevice *device)
  11513. {
  11514. out = device;
  11515. }
  11516. @i webview.w
  11517. @i printerselector.w
  11518. @* The Application class.
  11519. The |Application| class represents the \pn{} program. It is responsible for
  11520. setting up the settings object and localization in addition to the normal
  11521. responsibilities of |QApplication|. In addition to declaring the class, we also
  11522. define a macro that returns the |Application| instance.
  11523. @<Class declarations@>=
  11524. #define AppInstance (qobject_cast<@[Application *@]>(qApp))
  11525. class NodeInserter;
  11526. class DeviceTreeModel;
  11527. class DatabaseNotification;
  11528. class Application : public QApplication@/
  11529. {@/
  11530. @[Q_OBJECT@]@;
  11531. public:@/
  11532. Application(int &argc, char **argv);
  11533. QDomDocument* configuration();
  11534. @<Device configuration members@>@;
  11535. QSqlDatabase database();
  11536. Q_INVOKABLE bool databaseConnected();
  11537. Q_INVOKABLE QString currentTypicaUser();
  11538. Q_INVOKABLE bool login(const QString &user, const QString &password);
  11539. Q_INVOKABLE bool autoLogin();
  11540. QScriptEngine *engine;
  11541. QNetworkAccessManager *network;
  11542. DatabaseNotification *subscribe(const QString &notification);@/
  11543. @[public slots@]:@/
  11544. void setDatabaseConnected(bool status);
  11545. void setCurrentTypicaUser(const QString &user);
  11546. void notify(const QString &notification);
  11547. @<Extended Application slots@>@;
  11548. @[signals@]:@/
  11549. void userChanged(const QString &user);
  11550. private:@/
  11551. @<Application private data members@>@;
  11552. QDomDocument conf;
  11553. bool connectionStatus;
  11554. QString currentUser;
  11555. QMap<QString, DatabaseNotification*> notifiers;
  11556. QSqlDriver *notificationDriver;
  11557. };
  11558. @ The constructor for this class handles a few things that had previously been
  11559. handled in |main()|.
  11560. @<Application Implementation@>=
  11561. Application::Application(int &argc, char **argv) : QApplication(argc, argv),
  11562. connectionStatus(false), currentUser(QString())@/
  11563. {
  11564. network = new QNetworkAccessManager(this);
  11565. @<Allow use of the default QSettings constructor@>@;
  11566. @<Load translation objects@>@;
  11567. @<Register meta-types@>@;
  11568. @<Register top level device configuration nodes@>@;
  11569. }
  11570. @ We use |QSettings| objects throughout \pn{} to remember details such as the
  11571. size and position of windows and the most recently used directory. To simplify
  11572. the creation of these objects, we specify some details up front. This allows us
  11573. to use the default constructor rather than specifying these things every time we
  11574. need an object.
  11575. @<Allow use of the default QSettings constructor@>=
  11576. setOrganizationName("Wilson's Coffee & Tea");
  11577. setOrganizationDomain("wilsonscoffee.com");
  11578. setApplicationName(PROGRAM_NAME);
  11579. @ Much of the user visible text in \pn{} is wrapped in a call to |tr()|. Such
  11580. text can be replaced with translated text based on the user'@q'@>s locale. For more
  11581. details, see the Qt Linguist manual.
  11582. @<Load translation objects@>=
  11583. QTranslator *base = new QTranslator;
  11584. if(base->load(QString("qt_%1").arg(QLocale::system().name()), QString("%1/Translations").arg(QCoreApplication::applicationDirPath())))
  11585. {
  11586. installTranslator(base);
  11587. }
  11588. QTranslator *app = new QTranslator;
  11589. if(app->load(QString("%1_%2").arg("Typica").arg(QLocale::system().name()), QString("%1/Translations").arg(QCoreApplication::applicationDirPath())))
  11590. {
  11591. installTranslator(app);
  11592. }
  11593. @ We also want to be able to access the application instance from within the
  11594. scripting engine. We don'@q'@>t need to be able to create new instances, just access the one that already exists.
  11595. @<Set up the scripting engine@>=
  11596. value = engine->newQObject(AppInstance);
  11597. value.setProperty("subscribe", engine->newFunction(Application_subscribe));
  11598. engine->globalObject().setProperty("Application", value);
  11599. @ The |configuration()| method provides access to an XML document containing the
  11600. current application configuration. The object is populated in |main()|.
  11601. @<Application Implementation@>=
  11602. QDomDocument* Application::configuration()
  11603. {
  11604. return &conf;
  11605. }
  11606. @ The |database()| method provides access to a database connection for use by
  11607. database aware widgets.
  11608. @<Application Implementation@>=
  11609. QSqlDatabase Application::database()
  11610. {
  11611. QString connectionName;
  11612. QSqlDatabase connection =
  11613. QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false);
  11614. do
  11615. {
  11616. connectionName = QUuid::createUuid().toString();
  11617. } while (QSqlDatabase::connectionNames().contains(connectionName));
  11618. return QSqlDatabase::cloneDatabase(connection, QString(connectionName));
  11619. }
  11620. @ Starting with version 1.8 there are methods for determining if a connection
  11621. to the database was successfully established when Typica was opened.
  11622. @<Application Implementation@>=
  11623. void Application::setDatabaseConnected(bool status)
  11624. {
  11625. connectionStatus = status;
  11626. }
  11627. bool Application::databaseConnected()
  11628. {
  11629. return connectionStatus;
  11630. }
  11631. @ Database notifications allow various parts of Typica to be aware when data in
  11632. the database may have been changed by another instance of Typica running on a
  11633. different computer or by another program that uses the same database. To pass
  11634. notifications to the various places that may be interested in these and not
  11635. bother notification aware controls that are uninterested in unrelated
  11636. notifications, a notification object is created for each notification.
  11637. Note that the notification channel is an identifier and unquoted identifiers
  11638. will be converted to lowercase while \pn{} will attempt to match subscribed to
  11639. notifications exactly. This means that it is generally a good idea to subscribe
  11640. with the channel specified in lowercase.
  11641. @<Application Implementation@>=
  11642. DatabaseNotification* Application::subscribe(const QString &notification)
  11643. {
  11644. DatabaseNotification *retval;
  11645. if(notifiers.contains(notification))
  11646. {
  11647. retval = notifiers.value(notification);
  11648. }
  11649. else
  11650. {
  11651. if(notifiers.size() == 0)
  11652. {
  11653. notificationDriver = QSqlDatabase::database().driver();
  11654. connect(notificationDriver, SIGNAL(notification(QString)), this, SLOT(notify(QString)));
  11655. }
  11656. retval = new DatabaseNotification;
  11657. connect(this, SIGNAL(aboutToQuit()), retval, SLOT(deleteLater()));
  11658. if(notifiers.size() !=
  11659. notificationDriver->subscribedToNotifications().size())
  11660. {
  11661. for(int i = 0; i < notifiers.size(); i++)
  11662. {
  11663. notificationDriver->subscribeToNotification(notifiers.keys().at(i));
  11664. }
  11665. }
  11666. notifiers.insert(notification, retval);
  11667. notificationDriver->subscribeToNotification(notification);
  11668. }
  11669. return retval;
  11670. }
  11671. @ As the various parts of \pn{} operate mostly indepent of each other and
  11672. notifications from the current instance may not be immediately returned, it is
  11673. generally a good idea for operations that would generate a notification to also
  11674. signal that notification within the application.
  11675. @<Application Implementation@>=
  11676. void Application::notify(const QString &notification)
  11677. {
  11678. if(notifiers.contains(notification))
  11679. {
  11680. DatabaseNotification *notifier = notifiers.value(notification);
  11681. notifier->forwardNotification(notification);
  11682. }
  11683. }
  11684. @ A function is provided to allow subscribing to notifications from scripts.
  11685. @<Function prototypes for scripting@>=
  11686. QScriptValue Application_subscribe(QScriptContext *context,
  11687. QScriptEngine *engine);
  11688. @ The implementation is trivial.
  11689. @<Functions for scripting@>=
  11690. QScriptValue Application_subscribe(QScriptContext *context,
  11691. QScriptEngine *engine)
  11692. {
  11693. return engine->newQObject(
  11694. AppInstance->subscribe(argument<QString>(0, context)));
  11695. }
  11696. @ The DatabaseNotification object just needs a signal that interested objects
  11697. can connect to.
  11698. @<Class declarations@>=
  11699. class DatabaseNotification : public QObject
  11700. {
  11701. Q_OBJECT
  11702. public:
  11703. DatabaseNotification();
  11704. public slots:
  11705. void forwardNotification(const QString &notification);
  11706. signals:
  11707. void notify(const QString &notification);
  11708. };
  11709. @ The implementation is trivial.
  11710. @<Class declarations@>=
  11711. DatabaseNotification::DatabaseNotification() : QObject(NULL)
  11712. {
  11713. /* Nothing needs to be done here. */
  11714. }
  11715. void DatabaseNotification::forwardNotification(const QString &notification)
  11716. {
  11717. emit notify(notification);
  11718. }
  11719. @** Table editor for ordered arrays with SQL relations.
  11720. \noindent A database in use at Wilson's Coffee \char'046~Tea stores information
  11721. for a roasting log and uses entered information to adjust inventory tracking
  11722. tables. This roasting log connects the use of unroasted coffee with the creation
  11723. of roasted coffee. In order to support roasting coffee from more than one lot at
  11724. the same time, the columns that specify the types of coffee used and the amount
  11725. of each coffee are entered as ordered arrays in which the first entry in the
  11726. array specifying an unroasted coffee is associated with the first entry in the
  11727. array specifying the amount of coffee used. While most batches will involve only
  11728. a single unroasted coffee, the database has no limitation on the number of
  11729. coffees that may be roasted in a single batch. An additional characteristic of
  11730. this table is that the database requires an identification number for unroasted
  11731. coffee items, but it would be better to provide a list of acceptable items with
  11732. human readable names.
  11733. A scrollable area containing a table view which can provide the necessary input
  11734. delegates (such as a combo box for SQL relations) and validators which ensures
  11735. that there is always at least one empty row available for input with convenience
  11736. functions for extracting the arrays needed for database insertion would be ideal
  11737. for this.
  11738. To get this, we need a simple table model based on |QStandardItemModel|
  11739. or |QAbstractItemModel|. The model should ensure that there is always at least
  11740. one empty row available for editing. It should also provide a function for
  11741. obtaining a string that presents all values from a specified column with a given
  11742. role as an array literal suitable for binding to an SQL query.
  11743. A class based on |QComboBox| providing options selected from an SQL query will
  11744. be needed. This can be used as a standalone widget elsewhere, but here it is
  11745. also needed as an editor class for a column delegate. Another delegate class
  11746. allows input in another column to be constrained by a |QValidator| (in this case
  11747. a |QDoubleValidator|).
  11748. A class based on |QTableView| brings all of these classes together and presents
  11749. them to the user.
  11750. @* A table model for producing SQL array literals.
  11751. \noindent This is a simple table model which provides two somewhat unusual
  11752. features. First, it always provides at least one empty row at the end of the
  11753. data. Second, it provides SQL array literals for columns in the model.
  11754. \danger At some point I would like to replace this model and |MeasurementModel|
  11755. with an improved table model suitable to replace both. Some preliminary design
  11756. work suggests that this improvement simplifies \pn{} considerably both
  11757. internally and in the data flow configuration. This has not yet been done due to
  11758. development time constraints.\endanger
  11759. @<Class declarations@>=
  11760. class SaltModel : public QAbstractItemModel@/
  11761. {
  11762. Q_OBJECT@t\2\2@>@/
  11763. QList<QList<QMap<int, QVariant> > > modelData;
  11764. QStringList hData;
  11765. int colcount;@t\1\1@>@/
  11766. public:@/
  11767. SaltModel(int columns);
  11768. ~SaltModel();
  11769. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  11770. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  11771. bool setHeaderData(int section, Qt::Orientation@, orientation,
  11772. const QVariant &value, int role = Qt::DisplayRole);
  11773. QVariant data(const QModelIndex &index, int role) const;
  11774. bool setData(const QModelIndex &index, const QVariant &value,
  11775. int role = Qt::EditRole);
  11776. Qt::ItemFlags@, flags(const QModelIndex &index) const;
  11777. QVariant headerData(int section, Qt::Orientation@, orientation,
  11778. int role = Qt::DisplayRole) const;
  11779. QModelIndex index(int row, int column,
  11780. const QModelIndex &parent = QModelIndex()) const;
  11781. QModelIndex parent(const QModelIndex &index) const;
  11782. QString arrayLiteral(int column, int role) const;
  11783. QString quotedArrayLiteral(int column, int role) const;
  11784. void clear();
  11785. bool removeRows(int row, int count,
  11786. const QModelIndex &parent = QModelIndex());
  11787. int findData(const QVariant &value, int column, int role = Qt::UserRole);
  11788. };
  11789. @ The only unique methods in this class are the |arrayLiteral| and
  11790. |quotedArrayLiteral| methods. These take a column number and a data role and
  11791. produce a SQL array literal for every entry in that column with the specified
  11792. role. The string will take the form of
  11793. {\tt{'\LB row 1, row 2, }}$\dots$ {\tt{row N\RB '}}.
  11794. This is done simply by starting with a string identifying the start of an array
  11795. literal, looping over the rows in the model while appending any data found along
  11796. with the commas to separate values. If any data is found, the extra comma and
  11797. space are removed from the constructed string. Finally, text marking the end of
  11798. the array literal is added.
  11799. The |arrayLiteral| method is appropriate where the expected values are numeric.
  11800. The |quotedArrayLiteral| method is appropriate where the expected values are
  11801. text. The values in the array will have quotation marks around them for the
  11802. |quotedArrayLiteral|. Note that when binding these values to placeholders in a
  11803. SQL query the leading and trailing single quote characters should be removed.
  11804. \danger The way this method is currently used is quite harmless. Data from one
  11805. column is integer data obtained as a result of a previous database query and
  11806. data from the other column is restricted by the view to numeric data. Please
  11807. note, however, that it would be extremely stupid to use code such as this when
  11808. user input cannot be controlled so tightly. Were this model used with a view
  11809. that allows general text input, it would be trivial to construct an SQL
  11810. injection attack.
  11811. \medskip
  11812. \centerline{\includegraphics[width=6in]{exploits_of_a_mom}}
  11813. \smallskip
  11814. \centerline{Figure \secno: An Example of an SQL injection attack.\nfnote{%
  11815. Comic copyright Randall Munroe. Original can be found at:~%
  11816. \pdfURL{%
  11817. http://xkcd.com/327/}%
  11818. {http://xkcd.com/327/}}}
  11819. \medskip
  11820. \endanger
  11821. @<SaltModel Implementation@>=
  11822. QString SaltModel::arrayLiteral(int column, int role) const
  11823. {
  11824. QString literal = "'{";
  11825. for(int i = 0; i < rowCount(); i++)
  11826. {
  11827. QString datum = data(index(i, column), role).toString();
  11828. if(!datum.isEmpty())
  11829. {
  11830. literal.append(datum);
  11831. literal.append(", ");
  11832. }
  11833. }
  11834. if(literal.size() > 2)
  11835. {
  11836. literal.chop(2);
  11837. }
  11838. literal.append("}'");
  11839. return literal;
  11840. }
  11841. QString SaltModel::quotedArrayLiteral(int column, int role) const
  11842. {
  11843. QString literal = "'{";
  11844. for(int i = 0; i < rowCount(); i++)
  11845. {
  11846. QString datum = data(index(i, column), role).toString();
  11847. if(!datum.isEmpty())
  11848. {
  11849. literal.append("\"");
  11850. literal.append(datum);
  11851. literal.append("\", ");
  11852. }
  11853. }
  11854. if(literal.size() > 2)
  11855. {
  11856. literal.chop(2);
  11857. }
  11858. literal.append("}'");
  11859. return literal;
  11860. }
  11861. @ No entries in this model have children.
  11862. @<SaltModel Implementation@>=
  11863. QModelIndex SaltModel::parent(const QModelIndex &) const
  11864. {
  11865. return QModelIndex();
  11866. }
  11867. @ The |setData()| method is called by delegates on views when the user enters
  11868. new data. This method must check to determine if the data is being entered in
  11869. the last row to increase the size of the table.
  11870. The end of this function may seem a little strange. Why not simply look up the
  11871. map and insert information directly into the model data? Well, as of this
  11872. writing, that doesn'@q'@>t work. There are two ways around that problem. One is to
  11873. have the lists store references and dereference the real data. The other option
  11874. is to obtain a copy of the row, then a copy of the cell, update the cell, then
  11875. replace the old value of the cell in the copy of the row, then replace the old
  11876. values of the row in the real data. The other option would probably be more
  11877. efficient, but this does work.
  11878. @<SaltModel Implementation@>=
  11879. bool SaltModel::setData(const QModelIndex &index, const QVariant &value,
  11880. int role)@t\2\2@>@/
  11881. {@t\1@>@/
  11882. @<Check that the SaltModel index is valid@>@;
  11883. if(!valid)@/
  11884. {@t\1@>@/
  11885. return false;@t\2@>@/
  11886. }
  11887. if(index.row() == modelData.size() - 1)@/
  11888. {
  11889. beginInsertRows(QModelIndex(), modelData.size(), modelData.size());
  11890. @<Expand the SaltModel@>@;
  11891. endInsertRows();
  11892. }
  11893. QList<QMap<int, QVariant> > row = modelData.at(index.row());
  11894. QMap<int, QVariant> cell = row.at(index.column());
  11895. cell.insert(role, value);
  11896. if(role == Qt::EditRole)@/
  11897. {
  11898. cell.insert(Qt::DisplayRole, value);
  11899. }
  11900. row.replace(index.column(), cell);
  11901. modelData.replace(index.row(), row);
  11902. emit dataChanged(index, index);@/
  11903. return true;@t\2@>@/
  11904. }
  11905. @ Some model operations require checking that a model index is valid. This
  11906. chunk is used in these cases.
  11907. @<Check that the SaltModel index is valid@>=
  11908. bool valid = false;@/
  11909. if(index.isValid())@/
  11910. {@t\1@>@/
  11911. if(index.row() < modelData.size())@/
  11912. {@t\1@>@/
  11913. if(index.column() < colcount)@/
  11914. {@t\1@>@/
  11915. valid = true;@t\2@>@/
  11916. }@t\2@>@/
  11917. }@t\2@>@/
  11918. }
  11919. @ When data is modified in the last row of the table, the model must be expanded
  11920. to allow for additional data.
  11921. @<Expand the SaltModel@>=
  11922. QList<QMap<int, QVariant> > newRow;
  11923. QMap<int, QVariant> defaults;
  11924. for(int i = 0; i < colcount; i++)
  11925. {
  11926. newRow.append(defaults);
  11927. }
  11928. modelData.append(newRow);
  11929. @ The number of columns in the table is specified in the model constructor.
  11930. @<SaltModel Implementation@>=
  11931. SaltModel::SaltModel(int columns) : QAbstractItemModel(), colcount(columns)
  11932. {
  11933. for(int i = 0; i < columns; i++)
  11934. {
  11935. hData << "";
  11936. }
  11937. @<Expand the SaltModel@>@;
  11938. }
  11939. @ The destructor doesn'@q'@>t need to do anything.
  11940. @<SaltModel Implementation@>=
  11941. SaltModel::~SaltModel()
  11942. {
  11943. /* Nothing needs to be done here. */
  11944. }
  11945. @ A pair of methods provide the number of rows and columns in the model. No
  11946. entries in the model have children, so the parent should always be the invisible
  11947. root object.
  11948. @<SaltModel Implementation@>=
  11949. int SaltModel::rowCount(const QModelIndex &parent) const
  11950. {
  11951. return (parent == QModelIndex() ? modelData.size() : 0);
  11952. }
  11953. int SaltModel::columnCount(const QModelIndex &parent) const
  11954. {
  11955. return (parent == QModelIndex() ? colcount : 0);
  11956. }
  11957. @ The model maintains header data for labeling the model columns.
  11958. @<SaltModel Implementation@>=
  11959. bool SaltModel::setHeaderData(int section, Qt::Orientation@, orientation,@|
  11960. const QVariant &value, int)@t\2\2@>@/
  11961. {@t\1@>@/
  11962. if(orientation == Qt::Horizontal && section < colcount)@/
  11963. {@t\1@>@/
  11964. hData.replace(section, value.toString());@/
  11965. emit headerDataChanged(orientation, section, section);@/
  11966. return true;@t\2@>@/
  11967. }@/
  11968. return false;@t\2@>@/
  11969. }
  11970. @ Views need to be able to retrieve model and header data.
  11971. @<SaltModel Implementation@>=
  11972. QVariant SaltModel::data(const QModelIndex &index, int role) const
  11973. {
  11974. @<Check that the SaltModel index is valid@>@;
  11975. if(!valid)
  11976. {
  11977. return QVariant();
  11978. }
  11979. QList<QMap<int,QVariant> > row = modelData.at(index.row());
  11980. QMap<int,QVariant> cell = row.at(index.column());
  11981. return cell.value(role, QVariant());
  11982. }
  11983. QVariant SaltModel::headerData(int section, Qt::Orientation@, orientation,
  11984. int role) const
  11985. {
  11986. if(orientation == Qt::Horizontal && role == Qt::DisplayRole &&
  11987. section < colcount)
  11988. {
  11989. return QVariant(hData.at(section));
  11990. }
  11991. return QVariant();
  11992. }
  11993. @ Views need to know certain details such as if an item in the view can be
  11994. altered by the view. For this model, all valid indices can be edited.
  11995. @<SaltModel Implementation@>=
  11996. Qt::ItemFlags SaltModel::flags(const QModelIndex &index) const
  11997. {
  11998. @<Check that the SaltModel index is valid@>@;
  11999. if(valid)
  12000. {
  12001. return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
  12002. }
  12003. return 0;
  12004. }
  12005. @ So far, many of the methods use model indices. The model is responsible for
  12006. creating these.
  12007. @<SaltModel Implementation@>=
  12008. QModelIndex SaltModel::index(int row, int column,
  12009. const QModelIndex &parent) const
  12010. {
  12011. if(parent == QModelIndex())
  12012. {
  12013. if(row < modelData.size() && column < colcount)
  12014. {
  12015. return createIndex(row, column);
  12016. }
  12017. }
  12018. return QModelIndex();
  12019. }
  12020. @ There are some times when it is useful to clear the model data. Note that
  12021. column header data is retained and the table will contain a single empty row
  12022. after this method is called.
  12023. @<SaltModel Implementation@>=
  12024. void SaltModel::clear()
  12025. {
  12026. beginResetModel();
  12027. modelData.clear();
  12028. @<Expand the SaltModel@>@;
  12029. endResetModel();
  12030. }
  12031. @ Another commonly useful operation is the ability to remove rows from the
  12032. model. The new batch window uses this feature to eliminate rows in which the
  12033. coffee is set to NULL. Note that if all rows of the model are removed, a new
  12034. empty row will be created.
  12035. @<SaltModel Implementation@>=
  12036. bool SaltModel::removeRows(int row, int count,
  12037. const QModelIndex &parent)
  12038. {
  12039. if(parent == QModelIndex())
  12040. {
  12041. if(row >= 0 && count > 0 && (row + count - 1) < modelData.size())
  12042. {
  12043. beginRemoveRows(parent, row, row + count - 1);
  12044. for(int i = 0; i < count; i++)
  12045. {
  12046. modelData.removeAt(row);
  12047. }
  12048. endRemoveRows();
  12049. if(modelData.size() == 0)
  12050. {
  12051. beginInsertRows(parent, 0, 0);
  12052. @<Expand the SaltModel@>@;
  12053. endInsertRows();
  12054. }
  12055. return @[true@];
  12056. }
  12057. }
  12058. return @[false@];
  12059. }
  12060. @ To find the row number for removal operations it is useful to search for
  12061. special values on a given role. The |findData()| method returns the first row
  12062. in which the given value matches for a particular column and a particular role
  12063. or |-1| if no such match exists.
  12064. @<SaltModel Implementation@>=
  12065. int SaltModel::findData(const QVariant &value, int column, int role)
  12066. {
  12067. for(int i = 0; i < modelData.size(); i++)
  12068. {
  12069. if(modelData.at(i).size() > column)
  12070. {
  12071. if(modelData.at(i).at(column).contains(role))
  12072. {
  12073. if(modelData.at(i).at(column).value(role) == value)
  12074. {
  12075. return i;
  12076. }
  12077. }
  12078. }
  12079. }
  12080. return -1;
  12081. }
  12082. @* A Delegate for SQL Relations.
  12083. \noindent The first column of the table view being described is responsible for
  12084. providing item numbers to the database. Requiring that these numbers be entered
  12085. directly is prone to not particularly user friendly and almost encourages input
  12086. errors. These item numbers, however, refer to the items table in the database
  12087. which includes, among other details, a human readable text string naming the
  12088. item. This delegate provides the user with a drop down menu from which such a
  12089. string may be selected with this information provided by the database itself.
  12090. When the user selects an item, it informs the model not only of the text string
  12091. in the display role, but also of the id number in a user data role which can
  12092. later be queried in order to properly craft the appropriate query.
  12093. This is implemented with two classes. The first is a |QComboBox| which queries
  12094. the database and maintains a mapping of id to text. This is made its own widget
  12095. as it is useful without being turned into a delegate. The second class provides
  12096. this widget as a delegate and handles communications between it and the model.
  12097. @<Class declarations@>=
  12098. class SqlComboBox : public QComboBox@/
  12099. {@t\1@>@/
  12100. Q_OBJECT@;
  12101. int dataColumn;
  12102. int displayColumn;
  12103. bool dataColumnShown;
  12104. QString specialNullText;
  12105. QVariant specialNullData;
  12106. public:@/
  12107. SqlComboBox();
  12108. ~SqlComboBox();
  12109. SqlComboBox* clone(QWidget *parent);@/
  12110. @t\4@>public slots@t\kern-3pt@>:@/
  12111. void addNullOption();
  12112. void addSqlOptions(QString query);
  12113. void setDataColumn(int column);
  12114. void setDisplayColumn(int column);
  12115. void showData(bool show);
  12116. void setNullText(QString nullText);
  12117. void setNullData(QVariant nullData);@t\2@>@/
  12118. }@t\kern-3pt@>;
  12119. @ In order to make this class work a little more nicely as an item delegate,
  12120. the |clone()| method has been provided to create a new object with the same
  12121. options as a prototype.
  12122. @<SqlComboBox Implementation@>=
  12123. SqlComboBox* SqlComboBox::clone(QWidget *parent)
  12124. {
  12125. SqlComboBox *widget = new SqlComboBox();
  12126. widget->setParent(parent);
  12127. for(int i = 0; i < count(); i++)
  12128. {
  12129. widget->addItem(itemText(i), itemData(i));
  12130. }
  12131. return widget;
  12132. }
  12133. @ When using this class, we must first decide if the data column is shown. If
  12134. this is desired, the entries displayed will contain both the value from the
  12135. display column followed by the value from the data column. This can be useful in
  12136. cases where the same text is used for two different items.
  12137. @<SqlComboBox Implementation@>=
  12138. void SqlComboBox::showData(bool show)
  12139. {
  12140. dataColumnShown = show;
  12141. }
  12142. @ Next, there is a need to know if the NULL value may legally be selected. Where
  12143. this is the case, we generally want this to be inserted first. As the
  12144. |QComboBox| supports storing both display and user data, much of the code is a
  12145. thin wrapper around calls to the base class. The text and data for the NULL
  12146. value can be set arbitrarily, which can be useful in certain cases. Note that
  12147. any customization of the NULL text or data must be set before a call to
  12148. |addNullOption()|.
  12149. @<SqlComboBox Implementation@>=
  12150. void SqlComboBox::addNullOption()
  12151. {
  12152. addItem(specialNullText, specialNullData);
  12153. }
  12154. void SqlComboBox::setNullText(QString nullText)
  12155. {
  12156. specialNullText = nullText;
  12157. }
  12158. void SqlComboBox::setNullData(QVariant nullData)
  12159. {
  12160. specialNullData = nullData;
  12161. }
  12162. @ Typically, the SQL query used to populate this widget will request two columns
  12163. of data. One column is used as the display data, the other as user data. This is
  12164. done to present a human readable string where a database query needs an
  12165. identification number. By default, column |0| is used in both roles. If this
  12166. is not desired, the methods to change that must be called before specifying the
  12167. query.
  12168. @<SqlComboBox Implementation@>=
  12169. void SqlComboBox::setDataColumn(int column)
  12170. {
  12171. dataColumn = column;
  12172. }
  12173. void SqlComboBox::setDisplayColumn(int column)
  12174. {
  12175. displayColumn = column;
  12176. }
  12177. @ Once the widget is properly configured, we can run the SQL query and populate
  12178. the combo box with the results.
  12179. @<SqlComboBox Implementation@>=
  12180. void SqlComboBox::addSqlOptions(QString query)
  12181. {
  12182. SqlQueryConnection h;
  12183. QSqlQuery *dbquery = h.operator->();
  12184. if(!dbquery->exec(query))
  12185. {
  12186. QSqlError error = dbquery->lastError();
  12187. qDebug() << error.databaseText();
  12188. qDebug() << error.driverText();
  12189. qDebug() << error.text();
  12190. qDebug() << dbquery->lastQuery();
  12191. /* Throw an error here, please. */
  12192. }
  12193. while(dbquery->next())
  12194. {
  12195. QString displayValue(dbquery->value(displayColumn).toString());
  12196. QString dataValue(dbquery->value(dataColumn).toString());
  12197. if(dataColumnShown)
  12198. {
  12199. displayValue.append(QString(" (%1)").arg(dataValue));
  12200. }
  12201. addItem(displayValue, dataValue);
  12202. }
  12203. }
  12204. @ The constructor initializes some private member data. A size policy is also
  12205. set on the pop up. This allows the pop up to appear wider than the combo box to
  12206. allow more data to appear. On Linux this appears to also change the text elide
  12207. mode to something that is conveniently more appropriate for the use cases in
  12208. Typica. Note that this was not enough of a change to force the pop up to be
  12209. wide enough to contain all of the text for especially long items, but if the
  12210. combo box is wide enough the pop up will match that width.
  12211. The destructor is trivial.
  12212. @<SqlComboBox Implementation@>=
  12213. SqlComboBox::SqlComboBox() :
  12214. dataColumn(0), displayColumn(0), dataColumnShown(false),
  12215. specialNullText(tr("Unknown")), specialNullData(QVariant::String)
  12216. {
  12217. view()->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  12218. setMinimumContentsLength(20);
  12219. }
  12220. SqlComboBox::~SqlComboBox()
  12221. {
  12222. /* Nothing needs to be done here. */
  12223. }
  12224. @ To use this class as an editor delegate in a model we wrap the class in a
  12225. |QItemDelegate|.
  12226. @<Class declarations@>=
  12227. class SqlComboBoxDelegate : public QItemDelegate@/
  12228. {
  12229. Q_OBJECT@;
  12230. SqlComboBox *delegate;
  12231. public:@/
  12232. SqlComboBoxDelegate(QObject *parent = NULL);
  12233. QWidget *createEditor(QWidget *parent,
  12234. const QStyleOptionViewItem &option,@|
  12235. const QModelIndex &index) const;
  12236. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  12237. void setModelData(QWidget *editor, QAbstractItemModel *model,@|
  12238. const QModelIndex &index) const;
  12239. void setWidget(SqlComboBox *widget);
  12240. virtual QSize sizeHint() const;
  12241. void updateEditorGeometry(QWidget *editor,
  12242. const QStyleOptionViewItem &option,@|
  12243. const QModelIndex &index) const;
  12244. };
  12245. @ Rather than set the values for the combo box through the delegate class, we
  12246. create the editor and pass it in to the delegate.
  12247. @<SqlComboBoxDelegate Implementation@>=
  12248. void SqlComboBoxDelegate::setWidget(SqlComboBox *widget)
  12249. {
  12250. delegate = widget;
  12251. }
  12252. @ When a view requests this delegate, we simply return the widget that was
  12253. previously passed in.
  12254. @<SqlComboBoxDelegate Implementation@>=
  12255. QWidget* SqlComboBoxDelegate::createEditor(QWidget *parent,@|
  12256. const QStyleOptionViewItem &,
  12257. const QModelIndex &) const
  12258. {
  12259. return delegate->clone(parent);
  12260. }
  12261. @ To set the appropriate editor data, we check the value in the model and
  12262. attempt to set the value to match that.
  12263. @<SqlComboBoxDelegate Implementation@>=
  12264. void SqlComboBoxDelegate::setEditorData(QWidget *editor,
  12265. const QModelIndex &index) const
  12266. {
  12267. SqlComboBox *self = qobject_cast<SqlComboBox *>(editor);
  12268. self->setCurrentIndex(self->findData(
  12269. index.model()->data(index,
  12270. Qt::UserRole).toString()));
  12271. }
  12272. @ When setting the model data, we need to specify both the display role and the
  12273. user data role.
  12274. @<SqlComboBoxDelegate Implementation@>=
  12275. void SqlComboBoxDelegate::setModelData(QWidget *editor,@|
  12276. QAbstractItemModel *model,
  12277. const QModelIndex &index) const
  12278. {
  12279. SqlComboBox *self = qobject_cast<SqlComboBox *>(editor);
  12280. model->setData(index, self->itemData(self->currentIndex(), Qt::UserRole),
  12281. Qt::UserRole);
  12282. model->setData(index, self->currentText(), Qt::DisplayRole);
  12283. }
  12284. @ This is needed to play nicely with the model view architecture.
  12285. @<SqlComboBoxDelegate Implementation@>=
  12286. void SqlComboBoxDelegate::updateEditorGeometry(QWidget *editor,
  12287. const QStyleOptionViewItem &option,
  12288. const QModelIndex &) const
  12289. {
  12290. editor->setGeometry(option.rect);
  12291. }
  12292. @ When this delegate is used in a table view, we want to be able to provide a
  12293. size hint that can be used to resize the column in order to fit the delegate.
  12294. @<SqlComboBoxDelegate Implementation@>=
  12295. QSize SqlComboBoxDelegate::sizeHint() const
  12296. {
  12297. return delegate->sizeHint();
  12298. }
  12299. @ Finally, we need a constructor.
  12300. @<SqlComboBoxDelegate Implementation@>=
  12301. SqlComboBoxDelegate::SqlComboBoxDelegate(QObject *parent)
  12302. : QItemDelegate(parent)@/
  12303. {
  12304. /* Nothing needs to be done here. */
  12305. }
  12306. @** The main program.
  12307. The |main()| function is where program execution starts. Most of the work
  12308. required here is taken care of for us by the |Application| object.
  12309. The odd handling of argc is required to prevent segmentation faults in the Linux
  12310. build.
  12311. @<The main program@>=
  12312. int main(int argc, char **argv)@/
  12313. {@/
  12314. int *c = &argc;
  12315. Application app(*c, argv);
  12316. QSettings settings;
  12317. @<Set up logging@>@;
  12318. @<Set up icons@>@;
  12319. @<Set up fonts@>@;
  12320. @<Register device configuration widgets@>@;
  12321. @<Prepare the database connection@>@;
  12322. @<Load the application configuration@>@;
  12323. @<Set up the scripting engine@>@;
  12324. app.engine = engine;
  12325. @<Find and evaluate starting script@>@;
  12326. int retval = app.exec();
  12327. delete engine;
  12328. return retval;@/
  12329. }
  12330. @ \pn{} 1.6.3 introduces optional logging of diagnostic messages to a file. By
  12331. default this feature is not enabled. A sensible future refinement to this would
  12332. allow specification of where this file should be created.
  12333. @<Set up logging@>=
  12334. if(settings.value("settings/advanced/logging", false).toBool())
  12335. {
  12336. qInstallMsgHandler(messageFileOutput);
  12337. }
  12338. @ This requires that we have our messageFileOutput function.
  12339. @<Logging function prototype@>=
  12340. void messageFileOutput(QtMsgType type, const char *msg);
  12341. @ The current implementation is straightforward.
  12342. @<Logging function implementation@>=
  12343. void messageFileOutput(QtMsgType, const char *msg)
  12344. {
  12345. QFile output("Typica-"+QDate::currentDate().toString("yyyy-MM-dd")+".log");
  12346. output.open(QIODevice::WriteOnly | QIODevice::Append);
  12347. QTextStream outstream(&output);
  12348. outstream << msg << "\r\n";
  12349. }
  12350. @ \pn{} 1.4 introduces the ability to use icons in certain interface elements.
  12351. Some commonly desired public domain graphics are provided by the Tango Desktop
  12352. Project. We also set an application level default window icon.
  12353. @<Set up icons@>=
  12354. QStringList themeSearchPath = QIcon::themeSearchPaths();
  12355. themeSearchPath.append(":/resources/icons/tango");
  12356. QIcon::setThemeSearchPaths(themeSearchPath);
  12357. QIcon::setThemeName(":/resources/icons/tango");
  12358. app.setWindowIcon(QIcon(":/resources/icons/appicons/logo.svg"));
  12359. @ Similarly some elements make use of a special font which is loaded from
  12360. resource data.
  12361. There has been a report of a bug which I have not been able to reproduce and
  12362. which the original reporter has not yet gotten back to me with the results of
  12363. a test, so I have opted for an alternate approach which does not preclude the
  12364. use of the earlier plan but which may solve the matter. This brings in the
  12365. TeX Gyre Pagella font and sets this as the default standard font for all web
  12366. views.
  12367. @s QFontDatabase int
  12368. @s QWebSettings int
  12369. @<Set up fonts@>=
  12370. QFile entypo(":/resources/fonts/entypo.ttf");
  12371. entypo.open(QIODevice::ReadOnly);
  12372. QFontDatabase::addApplicationFontFromData(entypo.readAll());
  12373. entypo.close();
  12374. QFontDatabase::addApplicationFont(":/resources/fonts/texgyrepagella-regular.otf");
  12375. QFontDatabase::addApplicationFont(":/resources/fonts/texgyrepagella-bold.otf");
  12376. QFontDatabase::addApplicationFont(":/resources/fonts/texgyrepagella-bolditalic.otf");
  12377. QFontDatabase::addApplicationFont(":/resources/fonts/texgyrepagella-italic.otf");
  12378. QWebSettings::globalSettings()->setFontFamily(QWebSettings::StandardFont, "Tex Gyre Pagella");
  12379. @ Some widgets provided by \pn{} require access to a database in order to work.
  12380. To simplify using these widgets, the application will request information
  12381. needed to connect to a database. The use of two distinct |if| blocks rather than
  12382. an |if|$\dots$|else| construction is used because the data from settings can be
  12383. changed if an attempt to connect to the database fails.
  12384. @<Prepare the database connection@>=
  12385. if(settings.value("database/exists", "false").toString() == "true")
  12386. {
  12387. @<Try connecting to the database@>@;
  12388. }
  12389. if(settings.value("database/exists", "false").toString() == "false")
  12390. {
  12391. @<Prompt for database connection information@>@;
  12392. }
  12393. @ In order to connect to the database, we need five pieces of information: the
  12394. name of a database driver (PostgreSQL is recommended for now), the host name of
  12395. the computer running the database, the name of the database, the name of the
  12396. user connecting to the database, and that user'@q'@>s password. This information will
  12397. be stored in the user settings for the application so that the database
  12398. connection can be established without prompting the user next time. A class is
  12399. provided to gather this information.
  12400. @<Class declarations@>=
  12401. class SqlConnectionSetup : public QDialog@/
  12402. {@t\1@>@/
  12403. Q_OBJECT@;
  12404. public:@/
  12405. SqlConnectionSetup();
  12406. ~SqlConnectionSetup();@/
  12407. @t\4@>public slots@t\kern-3pt@>:@/
  12408. void testConnection();
  12409. private:@/
  12410. QFormLayout *formLayout;
  12411. QComboBox *driver;
  12412. QLineEdit *hostname;
  12413. QLineEdit *portnumber;
  12414. QLineEdit *dbname;
  12415. QLineEdit *user;
  12416. QLineEdit *password;
  12417. QVBoxLayout *layout;
  12418. QHBoxLayout *buttons;
  12419. QPushButton *cancelButton;
  12420. QPushButton *connectButton;@t\2@>@/
  12421. }@t\kern-3pt@>;
  12422. @ The constructor sets up this widget. The destructor does nothing.
  12423. @<SqlConnectionSetup implementation@>=
  12424. SqlConnectionSetup::SqlConnectionSetup() :
  12425. formLayout(new QFormLayout), driver(new QComboBox), hostname(new QLineEdit),
  12426. portnumber(new QLineEdit),
  12427. dbname(new QLineEdit), user(new QLineEdit), password(new QLineEdit),
  12428. layout(new QVBoxLayout), buttons(new QHBoxLayout),
  12429. cancelButton(new QPushButton(tr("Cancel"))),
  12430. connectButton(new QPushButton(tr("Connect")))@/
  12431. {
  12432. QSettings settings;
  12433. driver->addItem("PostgreSQL", "QPSQL");
  12434. formLayout->addRow(tr("Database driver:"), driver);
  12435. formLayout->addRow(tr("Host name:"), hostname);
  12436. hostname->setText(settings.value("database/hostname").toString());
  12437. formLayout->addRow(tr("Port number:"), portnumber);
  12438. portnumber->setText(settings.value("database/portnumber", "5432").toString());
  12439. formLayout->addRow(tr("Database name:"), dbname);
  12440. dbname->setText(settings.value("database/dbname").toString());
  12441. formLayout->addRow(tr("User name:"), user);
  12442. user->setText(settings.value("database/user").toString());
  12443. password->setEchoMode(QLineEdit::Password);
  12444. formLayout->addRow(tr("Password:"), password);
  12445. password->setText(settings.value("database/password").toString());
  12446. layout->addLayout(formLayout);
  12447. buttons->addStretch(1);
  12448. buttons->addWidget(cancelButton);
  12449. connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(reject()));
  12450. buttons->addWidget(connectButton);
  12451. layout->addLayout(buttons);
  12452. connect(connectButton, SIGNAL(clicked(bool)), this, SLOT(testConnection()));
  12453. connectButton->setDefault(true);
  12454. setLayout(layout);
  12455. setModal(true);
  12456. }
  12457. SqlConnectionSetup::~SqlConnectionSetup()
  12458. {
  12459. /* Nothing needs to be done here. */
  12460. }
  12461. @ The |testConnection()| method checks if the information provided can be used
  12462. to open a new database connection.
  12463. @<SqlConnectionSetup implementation@>=
  12464. void SqlConnectionSetup::testConnection()
  12465. {
  12466. QSqlDatabase database =
  12467. QSqlDatabase::addDatabase(driver->itemData(driver->currentIndex()).
  12468. toString());
  12469. database.setConnectOptions("application_name=Typica");
  12470. database.setHostName(hostname->text());
  12471. database.setPort(portnumber->text().toInt());
  12472. database.setDatabaseName(dbname->text());
  12473. database.setUserName(user->text());
  12474. database.setPassword(password->text());
  12475. if(database.open())
  12476. {
  12477. QSettings settings;
  12478. settings.setValue("database/exists", "true");
  12479. settings.setValue("database/driver",
  12480. driver->itemData(driver->currentIndex()).toString());
  12481. settings.setValue("database/hostname", hostname->text());
  12482. settings.setValue("database/portnumber", portnumber->text());
  12483. settings.setValue("database/dbname", dbname->text());
  12484. settings.setValue("database/user", user->text());
  12485. settings.setValue("database/password", password->text());
  12486. AppInstance->setDatabaseConnected(true);
  12487. accept();
  12488. }
  12489. else
  12490. {
  12491. QMessageBox::information(this, tr("Database connection failed"),
  12492. tr("Failed to connect to database."));
  12493. }
  12494. }
  12495. @ In order to prompt for connection information, we simply create a
  12496. |SqlConnectionSetup| object and call |exec()|. When control returns, the
  12497. settings will either contain appropriate connection information or we have to
  12498. give up on getting that information from the user for now.
  12499. @<Prompt for database connection information@>=
  12500. SqlConnectionSetup dialog;
  12501. dialog.exec();
  12502. @ If we have connected to a database in the previous running of the application,
  12503. we try to connect to the same database automatically rather than prompt the
  12504. user. If the connection attempt fails, we can fall back on asking the user for
  12505. help.
  12506. @<Try connecting to the database@>=
  12507. QSqlDatabase database =
  12508. QSqlDatabase::addDatabase(settings.value("database/driver").toString());
  12509. database.setConnectOptions("application_name=Typica");
  12510. database.setHostName(settings.value("database/hostname").toString());
  12511. database.setPort(settings.value("database/portnumber", 5432).toInt());
  12512. database.setDatabaseName(settings.value("database/dbname").toString());
  12513. database.setUserName(settings.value("database/user").toString());
  12514. database.setPassword(settings.value("database/password").toString());
  12515. if(!database.open())
  12516. {
  12517. settings.setValue("database/exists", "false");
  12518. }
  12519. else
  12520. {
  12521. AppInstance->setDatabaseConnected(true);
  12522. }
  12523. @** Viewing a record of batches.
  12524. \noindent It is frequently useful to present a table view with the results of a
  12525. SQL query and have a way of interacting with that view to obtain more details
  12526. related to a given record in that table. For this purpose, \pn{} provides a
  12527. widget based on |QTableView| which presents information from a
  12528. |QSqlQueryModel|. The table emits signals when an entry in the table is double
  12529. clicked. One of these contains the data from the first column of that row and
  12530. is suitable for use when a primary key is presented in that column and this is
  12531. sufficient for the desired drill down. The other signal provides the row number
  12532. which can be used along with a reference to the table to obtain the data in any
  12533. column.
  12534. This class also automatically persists column widths when these are changed.
  12535. @<Class declarations@>=
  12536. class SqlQueryView : public QTableView@/
  12537. {@/
  12538. @[Q_OBJECT@]@;
  12539. public:@/
  12540. SqlQueryView(QWidget *parent = NULL);
  12541. void setQuery(const QString &query);
  12542. bool setHeaderData(int section, Qt::Orientation@, orientation,
  12543. const QVariant &value, int role);
  12544. @[Q_INVOKABLE@,@, QVariant@]@, data(int row, int column,
  12545. int role = Qt::DisplayRole);@t\2\2@>@/
  12546. signals:@/
  12547. void openEntry(QString key);
  12548. void openEntryRow(int row);
  12549. void selectEntry(QString key);
  12550. void selectEntryRow(int row);@/
  12551. protected:@/
  12552. virtual void showEvent(QShowEvent *event);@/
  12553. @[private slots@]:@/
  12554. void openRow(const QModelIndex &index);
  12555. void persistColumnResize(int column, int oldsize, int newsize);
  12556. void selectRow(const QModelIndex &index);@/
  12557. };
  12558. @ The constructor sets up the communication between the model and the view and
  12559. also provides the connection needed to notice when columns change size to
  12560. persist that preference.
  12561. @<SqlQueryView implementation@>=
  12562. SqlQueryView::SqlQueryView(QWidget *parent) : QTableView(parent)
  12563. {
  12564. setModel(new QSqlQueryModel);
  12565. connect(this, SIGNAL(doubleClicked(QModelIndex)),
  12566. this, SLOT(openRow(QModelIndex)));
  12567. connect(horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
  12568. this, SLOT(persistColumnResize(int, int, int)));
  12569. connect(this, SIGNAL(activated(QModelIndex)),
  12570. this, SLOT(selectRow(QModelIndex)));
  12571. }
  12572. @ Column width persistance requires two methods. First we have a slot
  12573. method which is called when a column width is changed. This is saved with
  12574. |QSettings| under a key utilizing the name of the window, the name of the
  12575. table, and the column number.
  12576. @<SqlQueryView implementation@>=
  12577. void SqlQueryView::persistColumnResize(int column, int, int newsize)
  12578. {
  12579. @<Save updated column size@>@;
  12580. }
  12581. @ The body of this function has been split out so that it can be shared with
  12582. other table views without the need to introduce a new common base class.
  12583. @<Save updated column size@>=
  12584. QSettings settings;
  12585. @<Obtain top level widget@>@;
  12586. settings.setValue(QString("columnWidths/%1/%2/%3").
  12587. arg(topLevelWidget->objectName()).
  12588. arg(objectName()).arg(column),
  12589. QVariant(newsize));
  12590. @ To determine which window a given table is in, we just follow
  12591. |parentWidget()| until there isn'@q'@>t one. It is possible that the table view
  12592. will also be the window, however this is not advised as it is easier for the
  12593. settings key to be non-unique in such a case.
  12594. @<Obtain top level widget@>=
  12595. QWidget *topLevelWidget = this;
  12596. while(topLevelWidget->parentWidget())
  12597. {
  12598. topLevelWidget = topLevelWidget->parentWidget();
  12599. }
  12600. @ We restore column widths in response to a show event. One of these should be
  12601. received just before the widget is shown so the widget should appear correctly.
  12602. @<SqlQueryView implementation@>=
  12603. void SqlQueryView::showEvent(QShowEvent *event)
  12604. {
  12605. @<Restore table column widths@>@;
  12606. event->accept();
  12607. }
  12608. @ Similarly, most of the body of this method has also been split into a chunk
  12609. so that it might be shared with other classes.
  12610. @<Restore table column widths@>=
  12611. QSettings settings;
  12612. @<Obtain top level widget@>
  12613. QString baseKey =
  12614. QString("columnWidths/%1/%2").arg(topLevelWidget->objectName()).
  12615. arg(objectName());
  12616. for(int i = 0; i < model()->columnCount(); i++)
  12617. {
  12618. QString key = QString("%1/%2").arg(baseKey).arg(i);
  12619. if(settings.contains(key))
  12620. {
  12621. setColumnWidth(i, settings.value(key).toInt());
  12622. }
  12623. }
  12624. @ A slot is required for obtaining the information to send out in our signals.
  12625. @<SqlQueryView implementation@>=
  12626. void SqlQueryView::openRow(const QModelIndex &index)
  12627. {
  12628. emit openEntry(((QSqlQueryModel *)model())->record(index.row()).value(0).toString());
  12629. emit openEntryRow(index.row());
  12630. }
  12631. void SqlQueryView::selectRow(const QModelIndex &index)
  12632. {
  12633. emit selectEntry(((QSqlQueryModel *)model())->record(index.row()).value(0).toString());
  12634. emit selectEntryRow(index.row());
  12635. }
  12636. @ The other functions are wrappers around model methods.
  12637. @<SqlQueryView implementation@>=
  12638. void SqlQueryView::setQuery(const QString &query)
  12639. {
  12640. QSqlDatabase database = AppInstance->database();
  12641. database.open();
  12642. QSqlQuery q(query, database);
  12643. ((QSqlQueryModel*)model())->setQuery(q);
  12644. database.close();
  12645. }
  12646. bool SqlQueryView::setHeaderData(int section, Qt::Orientation@, orientation,
  12647. const QVariant &value, int role)
  12648. {
  12649. return model()->setHeaderData(section, orientation, value, role);
  12650. }
  12651. @ A method is also provided to allow scripts to access the data.
  12652. @<SqlQueryView implementation@>=
  12653. QVariant SqlQueryView::data(int row, int column, int role)
  12654. {
  12655. return model()->data(model()->index(row, column), role);
  12656. }
  12657. @ To use this class, it is useful to expose it to the host environment.
  12658. @<Function prototypes for scripting@>=
  12659. void setSqlQueryViewProperties(QScriptValue value, QScriptEngine *engine);
  12660. QScriptValue constructSqlQueryView(QScriptContext *context,
  12661. QScriptEngine *engine);
  12662. QScriptValue SqlQueryView_setQuery(QScriptContext *context,
  12663. QScriptEngine *engine);
  12664. QScriptValue SqlQueryView_setHeaderData(QScriptContext *context,
  12665. QScriptEngine *engine);
  12666. QScriptValue SqlQueryView_rows(QScriptContext *context,
  12667. QScriptEngine *engine);
  12668. @ The script constructor is passed to the host environment.
  12669. @<Set up the scripting engine@>=
  12670. constructor = engine->newFunction(constructSqlQueryView);
  12671. value = engine->newQMetaObject(&SqlQueryView::staticMetaObject, constructor);
  12672. engine->globalObject().setProperty("SqlQueryView", value);
  12673. @ Next we construct the view, add properties to access its methods from the host
  12674. environment, and pass that back.
  12675. @<Functions for scripting@>=
  12676. QScriptValue constructSqlQueryView(QScriptContext *, QScriptEngine *engine)
  12677. {
  12678. QScriptValue object = engine->newQObject(new SqlQueryView);
  12679. setSqlQueryViewProperties(object, engine);
  12680. return object;
  12681. }
  12682. void setSqlQueryViewProperties(QScriptValue value, QScriptEngine *engine)
  12683. {
  12684. setQTableViewProperties(value, engine);
  12685. value.setProperty("setHeaderData",
  12686. engine->newFunction(SqlQueryView_setHeaderData));
  12687. value.setProperty("setQuery", engine->newFunction(SqlQueryView_setQuery));
  12688. value.setProperty("rows", engine->newFunction(SqlQueryView_rows));
  12689. }
  12690. @ The properties added are simplified wrappers around the class methods.
  12691. @<Functions for scripting@>=
  12692. QScriptValue SqlQueryView_setQuery(QScriptContext *context, QScriptEngine *)
  12693. {
  12694. SqlQueryView *self = getself<SqlQueryView *>(context);
  12695. QString query = argument<QString>(0, context);
  12696. self->setQuery(query);
  12697. self->reset();
  12698. return QScriptValue();
  12699. }
  12700. QScriptValue SqlQueryView_setHeaderData(QScriptContext *context,
  12701. QScriptEngine *)
  12702. {
  12703. SqlQueryView *self = getself<SqlQueryView *>(context);
  12704. int section = argument<int>(0, context);
  12705. QString data = argument<QString>(1, context);
  12706. self->setHeaderData(section, Qt::Horizontal, data, Qt::DisplayRole);
  12707. return QScriptValue();
  12708. }
  12709. QScriptValue SqlQueryView_rows(QScriptContext *context, QScriptEngine *)
  12710. {
  12711. SqlQueryView *self = getself<SqlQueryView *>(context);
  12712. return QScriptValue(self->model()->rowCount());
  12713. }
  12714. @** Reporting.
  12715. \noindent \pn{} version 1.4 added a new type of menu which is designed to
  12716. handle reports. This makes extensive use of the previously existing reporting
  12717. system at present which makes modifying existing reports to work with the new
  12718. system very simple. Further changes may be introduced in the future that
  12719. substantially depart from this in order to simplify report files.
  12720. Previously to add a new report to a configuration, you needed to create the
  12721. report, add an {\tt <include>} tag in the main configuration file to bring that
  12722. report into the application configuration, then in any window with a Reports
  12723. menu you would need to add the report to that menu in its configuration file
  12724. and write a small bit of JavaScript to obtain a reference to that new menu
  12725. item and create the report when that menu item is triggered. This is highly
  12726. repetetive, error prone, and with the new approach it is not needed at all.
  12727. To add a new report to a configuration using the new approach one need only
  12728. save the new report file in the appropriate directory and \pn{} will detect
  12729. this, add it to any Reports menus that may exist, and handle all of the details
  12730. of generating these reports on demand.
  12731. The Reports menu is created in a configuration as a {\tt <menu>} element with
  12732. three attributes. The {\tt name} attribute as usual is the name of the menu
  12733. item. The {\tt type} attribute will have a value of {\tt "reports"} and the
  12734. {\tt src} attribute will indicate the directory to search for reports to
  12735. populate that menu. This allows for multiple Reports menus with different
  12736. reports in each menu if desired.
  12737. Reports are added to the menu in the order of the file names in the reports
  12738. directory.
  12739. \danger While it should not be an issue with the limited number of reports
  12740. presently distributed with Typica, the approach taken to implementing this menu
  12741. type is highly inefficient. There are many optimizations available if this
  12742. becomes problematic.\endanger
  12743. When a report menu is generated, the directory used for this is added as a
  12744. search path for the |"reports"| prefix. This is used by the |createReport()|
  12745. script method and is intended to allow access to reports from outside of the
  12746. Report menu.
  12747. @<Populate reports menu@>=
  12748. QSettings settings;
  12749. QString reportDirectory = QString("%1/%2").arg(settings.value("config").
  12750. toString()).
  12751. arg(element.attribute("src"));
  12752. QDir::addSearchPath("reports", reportDirectory);
  12753. QDir directory(reportDirectory);
  12754. directory.setFilter(QDir::Files);
  12755. directory.setSorting(QDir::Name);
  12756. QStringList nameFilter;
  12757. nameFilter << "*.xml";
  12758. directory.setNameFilters(nameFilter);
  12759. QFileInfoList reportFiles = directory.entryInfoList();
  12760. for(int i = 0; i < reportFiles.size(); i++)
  12761. {
  12762. QFileInfo reportFile = reportFiles.at(i);
  12763. @<Add report to reports menu@>@;
  12764. }
  12765. @ The menu items themselves are a subclass of |QAction| which holds all of the
  12766. information needed to respond to its activation by generating the appropriate
  12767. report.
  12768. @<Class declarations@>=
  12769. class ReportAction : public QAction@/
  12770. {@/
  12771. @[Q_OBJECT@]@;
  12772. public:@/
  12773. ReportAction(const QString &fileName, const QString &reportName,
  12774. QObject *parent = NULL);@/
  12775. @[private slots@]:@/
  12776. void createReport();@/
  12777. private:@/
  12778. QString reportFile;@/
  12779. };
  12780. @ The constructor receives the name of the report file which is used to
  12781. generate the report when needed and the name of the report which is used as the
  12782. name presented in the menu.
  12783. @<ReportAction implementation@>=
  12784. ReportAction::ReportAction(const QString &fileName, const QString &reportName,
  12785. QObject *parent) :
  12786. QAction(reportName, parent), reportFile(fileName)
  12787. {
  12788. connect(this, SIGNAL(triggered()), this, SLOT(createReport()));
  12789. }
  12790. @ The slot method is responsible for creating the new report. This is very
  12791. similar to the old approach and reuses much of the same code. Of particular
  12792. note is the |targetID| variable. This is set to facilitate window geometry
  12793. management, though this should probably be set from the {\tt id} attribute
  12794. of the {\tt <window>} element in the file to preserve window geometry
  12795. settings if the configuration is moved to another location in the file
  12796. system.
  12797. @<ReportAction implementation@>=
  12798. void ReportAction::createReport()
  12799. {
  12800. QFile file(reportFile);
  12801. QDomDocument document;
  12802. if(file.open(QIODevice::ReadOnly))
  12803. {
  12804. document.setContent(&file, true);
  12805. QDomElement element = document.documentElement();
  12806. QScriptEngine *engine = AppInstance->engine;
  12807. QScriptContext *context = engine->pushContext();
  12808. QScriptValue object;
  12809. QString targetID = reportFile;
  12810. @<Display the window@>@;
  12811. file.close();
  12812. engine->popContext();
  12813. }
  12814. }
  12815. @ With the |ReportAction| available, we are now ready to add reports to the
  12816. Reports menu. To do this we check each file in the given directory to determine
  12817. if it is a report file, obtain the report title and location within the menu
  12818. hierarchy from the file data, create the actions, and add them to the menu.
  12819. @<Add report to reports menu@>=
  12820. QString path = reportFile.absoluteFilePath();
  12821. QFile file(path);
  12822. if(file.open(QIODevice::ReadOnly))
  12823. {
  12824. QDomDocument document;
  12825. document.setContent(&file, true);
  12826. QDomElement root = document.documentElement();
  12827. QString translationContext = root.attribute("id");
  12828. QDomNode titleNode = root.elementsByTagName("reporttitle").at(0);
  12829. if(!titleNode.isNull())
  12830. {
  12831. QDomElement titleElement = titleNode.toElement();
  12832. QString title = QCoreApplication::translate("configuration",
  12833. titleElement.text().toUtf8().data());
  12834. if(!title.isEmpty())
  12835. {
  12836. QStringList hierarchy = title.split(":->");
  12837. QMenu *insertionPoint = menu;
  12838. @<Traverse report menu hierarchy@>
  12839. ReportAction *action = new ReportAction(path, hierarchy.last());
  12840. insertionPoint->addAction(action);
  12841. }
  12842. }
  12843. }
  12844. @ \pn{} allows the Reports menu to contain arbitrarily deep menu hierarchies.
  12845. It is advised to keep these hierarchies shallow.
  12846. @<Traverse report menu hierarchy@>=
  12847. for(int j = 0; j < hierarchy.size() - 1; j++)
  12848. {
  12849. QObjectList menuList = insertionPoint->children();
  12850. bool menuFound = @[false@];
  12851. for(int k = 0; k < menuList.size(); k++)
  12852. {
  12853. QMenu *currentItem = qobject_cast<QMenu*>(menuList.at(k));
  12854. if(currentItem)
  12855. {
  12856. if(currentItem->title() == hierarchy.at(j))
  12857. {
  12858. menuFound = @[true@];
  12859. insertionPoint = currentItem;
  12860. break;
  12861. }
  12862. }
  12863. }
  12864. if(!menuFound)
  12865. {
  12866. insertionPoint = insertionPoint->addMenu(hierarchy.at(j));
  12867. }
  12868. }
  12869. @ \noindent The reporting functionality in \pn{} is based on the Scribe framework
  12870. in Qt. This brings several benefits, including making it easy to print reports
  12871. or save reports as plain text or HTML.
  12872. Reports are specified in the \pn{}'@q'@>s configuration document and can include both
  12873. static elements and elements that are populated by external data such as the
  12874. result of a SQL query.
  12875. @<Function prototypes for scripting@>=
  12876. void addReportToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  12877. QStack<QLayout *> *layoutStack);
  12878. @ When adding a report to a layout, we must not only add the widget to the
  12879. layout, but also construct the document.
  12880. @<Functions for scripting@>=
  12881. void addReportToLayout(QDomElement element, QStack<QWidget *> *,@|
  12882. QStack<QLayout *> *layoutStack)
  12883. {
  12884. QTextEdit *widget = new QTextEdit;
  12885. if(element.hasAttribute("id"))
  12886. {
  12887. widget->setObjectName(element.attribute("id"));
  12888. }
  12889. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  12890. layout->addWidget(widget);
  12891. QTextDocument *document = new QTextDocument;
  12892. QFont defaultFont;
  12893. defaultFont.setPointSize(11);
  12894. document->setDefaultFont(defaultFont);
  12895. QTextCursor cursor(document);
  12896. @<Construct report document@>@;
  12897. widget->setDocument(document);
  12898. }
  12899. @ Several child elements are allowed under the {\tt <report>} element. These
  12900. should be processed in order to produce the final report document.
  12901. @<Construct report document@>=
  12902. QDomNodeList children = element.childNodes();
  12903. for(int i = 0; i < children.count(); i++)
  12904. {
  12905. QDomNode current;
  12906. QDomElement currentElement;
  12907. current = children.at(i);
  12908. if(current.isElement())
  12909. {
  12910. currentElement = current.toElement();
  12911. @<Process report document elements@>@;
  12912. }
  12913. }
  12914. @ If any custom styling for HTML content is required, a {\tt <style>} element
  12915. should be placed in the report description before any such content.
  12916. @<Process report document elements@>=
  12917. if(currentElement.tagName() == "style")
  12918. {
  12919. document->setDefaultStyleSheet(currentElement.text());
  12920. }
  12921. @ One common need is the ability to insert static text, such as the title of the
  12922. report. In order to simplify formatting, the text can be interpreted as HTML.
  12923. Note that to avoid having HTML tags eaten by the parser, the text of this
  12924. element should be a CDATA section.
  12925. @<Process report document elements@>=
  12926. if(currentElement.tagName() == "html")
  12927. {
  12928. cursor.insertHtml(currentElement.text());
  12929. }
  12930. @ If no special formatting is needed, a plain text element can be used. This
  12931. might be extended in the future to allow attributes for specifying the character
  12932. formatting to be used with the text.
  12933. @<Process report document elements@>=
  12934. if(currentElement.tagName() == "text")
  12935. {
  12936. cursor.insertText(currentElement.text());
  12937. }
  12938. @ One of the more interesting elements of a report is the {\tt <table>} element.
  12939. This is an element which can change its contents in response to changes in user
  12940. controls.
  12941. @<Process report document elements@>=
  12942. if(currentElement.tagName() == "table")
  12943. {
  12944. QTextFrame *frame = cursor.insertFrame(QTextFrameFormat());
  12945. ReportTable *table = new ReportTable(frame, currentElement);
  12946. table->setParent(widget);
  12947. if(currentElement.hasAttribute("id"))
  12948. {
  12949. table->setObjectName(currentElement.attribute("id"));
  12950. }
  12951. }
  12952. @ The |ReportTable| class is responsible for parsing {\tt <table>} child
  12953. elements and inserting the table into the document at the correct location.
  12954. @<Class declarations@>=
  12955. class ReportTable : public QObject@/
  12956. {@t\1@>@/
  12957. Q_OBJECT@;
  12958. QTextFrame *area;
  12959. QDomElement configuration;
  12960. QMap<QString, QVariant> bindings;
  12961. public:@/
  12962. ReportTable(QTextFrame *frame, QDomElement description);
  12963. ~ReportTable();
  12964. @[Q_INVOKABLE@,@, void@]@, bind(QString placeholder, QVariant value);@t\2\2@>@/
  12965. @t\4@>public slots@t\kern-3pt@>:@/
  12966. void refresh();@t\2@>@/
  12967. }@t\kern-3pt@>;
  12968. @ The |ReportTable| class takes a |QTextFrame| and |QDomElement| pointer in
  12969. its constructor. The former is used to establish the bounds of the table within
  12970. a document and the latter is used for generating the table contents.
  12971. @<ReportTable implementation@>=
  12972. ReportTable::ReportTable(QTextFrame *frame, QDomElement description) :
  12973. area(frame), configuration(description)
  12974. {
  12975. refresh();
  12976. }
  12977. ReportTable::~ReportTable()
  12978. {
  12979. }
  12980. @ In order to change the table contents based on user controls, the |bind()|
  12981. method allows a placeholder to be replaced with a value when evaluating a SQL
  12982. query.
  12983. @<ReportTable implementation@>=
  12984. void ReportTable::bind(QString placeholder, QVariant value)
  12985. {
  12986. bindings.insert(placeholder, value);
  12987. }
  12988. @ All of the interesting work is done in the |refresh()| slot. This method
  12989. deletes the current content of the frame and creates a table based on the
  12990. description of the table in the configuration document.
  12991. @<ReportTable implementation@>=
  12992. void ReportTable::refresh()
  12993. {
  12994. @<Delete current report table content@>@;
  12995. int rows = 1;
  12996. int columns = 1;
  12997. int currentRow = 0;
  12998. QTextTable *table = cursor.insertTable(rows, columns);
  12999. @<Set table formatting@>@;
  13000. @<Reconstruct report table content@>@;
  13001. if(rows > 1)
  13002. {
  13003. table->removeRows(0, 1);
  13004. }
  13005. }
  13006. @ Deleting the current content of the table involves using a cursor to select
  13007. everything in the frame and then removing that selection. There are more optimal
  13008. ways to do this but if there are performance problems with this, you may want to
  13009. reconsider what you are trying to do.
  13010. @<Delete current report table content@>=
  13011. QTextCursor cursor = area->firstCursorPosition();
  13012. while(cursor < area->lastCursorPosition())
  13013. {
  13014. cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
  13015. }
  13016. cursor.removeSelectedText();
  13017. @ When creating a new table, we may need to alter the formatting of that table.
  13018. To do this, we get the current format, modify that based on attributes of the
  13019. {\tt <table>} element, and apply the modified copy to the newly constructed
  13020. table.
  13021. @<Set table formatting@>=
  13022. QTextTableFormat format = table->format();
  13023. format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
  13024. if(configuration.hasAttribute("align"))
  13025. {
  13026. if(configuration.attribute("align") == "center")
  13027. {
  13028. format.setAlignment(Qt::AlignHCenter);
  13029. }
  13030. }
  13031. table->setFormat(format);
  13032. @ To reconstruct the table, we need to parse the description of the table.
  13033. @<Reconstruct report table content@>=
  13034. QDomNodeList children = configuration.childNodes();
  13035. for(int i = 0; i < children.count(); i++)
  13036. {
  13037. QDomNode current;
  13038. QDomElement currentElement;
  13039. current = children.at(i);
  13040. if(current.isElement())
  13041. {
  13042. currentElement = current.toElement();
  13043. if(currentElement.tagName() == "query")
  13044. {
  13045. @<Add SQL query results to report table@>@;
  13046. }
  13047. else if(currentElement.tagName() == "row")
  13048. {
  13049. @<Add new row to report table@>@;
  13050. }
  13051. }
  13052. }
  13053. @ The text of a {\tt <query>} element will be the query desired in the table.
  13054. This might include placeholders that must be bound to values before the query is
  13055. executed. If query execution results in an error (as it will if it contains
  13056. placeholders that have not yet had values bound to them), there will be no
  13057. change to the table and the next child element, if any, will be processed.
  13058. @<Add SQL query results to report table@>=
  13059. SqlQueryConnection h;
  13060. QSqlQuery *query = h.operator->();
  13061. query->prepare(currentElement.text());
  13062. foreach(QString key, bindings.uniqueKeys())
  13063. {
  13064. if(currentElement.text().contains(key))
  13065. {
  13066. query->bindValue(key, bindings.value(key));
  13067. }
  13068. }
  13069. query->exec();
  13070. if(!query->next())
  13071. {
  13072. continue;
  13073. }
  13074. if(query->record().count() > columns)
  13075. {
  13076. table->appendColumns(query->record().count() - columns);
  13077. }
  13078. do
  13079. {
  13080. table->appendRows(1);
  13081. rows++;
  13082. currentRow++;
  13083. for(int j = 0; j < query->record().count(); j++)
  13084. {
  13085. QTextTableCell cell = table->cellAt(currentRow, j);
  13086. cursor = cell.firstCursorPosition();
  13087. cursor.insertText(query->value(j).toString());
  13088. }
  13089. } while(query->next());
  13090. @ It is sometimes desirable to add fixed data such as column headers to a table.
  13091. This is done with the {\tt <row>} element.
  13092. Technically, this isn'@q'@>t needed. The same results can be produced by using a
  13093. {\tt <query>} element to select constant data, but this approach saves a trip to
  13094. the database.
  13095. @<Add new row to report table@>=
  13096. table->appendRows(1);
  13097. currentRow++;
  13098. rows++;
  13099. QDomNodeList rowChildren = currentElement.childNodes();
  13100. int currentColumn = 0;
  13101. for(int j = 0; j < rowChildren.count(); j++)
  13102. {
  13103. QDomNode node;
  13104. QDomElement nodeElement;
  13105. node = rowChildren.at(j);
  13106. if(node.isElement())
  13107. {
  13108. nodeElement = node.toElement();
  13109. if(nodeElement.tagName() == "cell")
  13110. {
  13111. if(currentColumn == columns)
  13112. {
  13113. table->appendColumns(1);
  13114. columns++;
  13115. }
  13116. QTextTableCell cell = table->cellAt(currentRow, currentColumn);
  13117. cursor = cell.firstCursorPosition();
  13118. cursor.insertText(nodeElement.text());
  13119. currentColumn++;
  13120. }
  13121. }
  13122. }
  13123. @ In order to expose report printing capabilities, we provide a property on
  13124. |QTextEdit| objects to handle this.
  13125. @<Function prototypes for scripting@>=
  13126. void setQTextEditProperties(QScriptValue value, QScriptEngine *engine);
  13127. QScriptValue QTextEdit_print(QScriptContext *context, QScriptEngine *engine);
  13128. @ This function is a trivial adaptation from the Qt documentation.
  13129. @<Functions for scripting@>=
  13130. QScriptValue QTextEdit_print(QScriptContext *context, QScriptEngine *)
  13131. {
  13132. QTextEdit *self = getself<QTextEdit *>(context);
  13133. QTextDocument *document = self->document();
  13134. QPrinter printer;
  13135. QPrintDialog printwindow(&printer, self);
  13136. if(printwindow.exec() != QDialog::Accepted)
  13137. {
  13138. return QScriptValue();
  13139. }
  13140. document->print(&printer);
  13141. return QScriptValue();
  13142. }
  13143. @ The host environment must be informed of this function.
  13144. @<Functions for scripting@>=
  13145. void setQTextEditProperties(QScriptValue value, QScriptEngine *engine)
  13146. {
  13147. setQAbstractScrollAreaProperties(value, engine);
  13148. value.setProperty("print", engine->newFunction(QTextEdit_print));
  13149. }
  13150. @i plugins.w
  13151. @i daterangeselector.w
  13152. @** An area for repeated user interface elements.
  13153. \noindent There are multiple use cases in which it is useful to specify a
  13154. complex aggregation of user interface elements to be repeated arbitrarily many
  13155. times. For example, placing multiple copies of a cupping form in a single area
  13156. for conveniently entering observations for all coffees in a particular session
  13157. or providing any number of copies of the form for entering coffee purchase
  13158. information. The |FormArray| widget provides this capability, allowing the
  13159. XML portion of the configuration document to specify the form once and allowing
  13160. the host environment to access the copies.
  13161. Slots and the |Q_INVOKABLE| macro are used to simplify the use of this class
  13162. from the host environment.
  13163. @<Class declarations@>=
  13164. class FormArray : public QScrollArea@/
  13165. {@t\1@>@/
  13166. Q_OBJECT@;
  13167. QDomElement configuration;
  13168. QWidget itemContainer;
  13169. QVBoxLayout itemLayout;
  13170. int maxwidth;
  13171. int maxheight;
  13172. public:@/
  13173. FormArray(QDomElement description);
  13174. @[Q_INVOKABLE@,@, QWidget*@] elementAt(int index);@t\2\2@>@/
  13175. @[Q_INVOKABLE@,@, int@] elements();@t\2\2@>@/
  13176. @t\4@>public slots@t\kern-3pt@>:@/
  13177. void addElements(int copies = 1);
  13178. void removeAllElements();
  13179. void setMaximumElementWidth(int width);
  13180. void setMaximumElementHeight(int height);@t\2@>@/
  13181. }@t\kern-3pt@>;
  13182. @ The |FormArray| is just a |QScrollArea| providing a view onto a |QWidget|
  13183. containing a layout which has arbitrarily many copies of a |QWidget| with
  13184. contents determined by the configuration document used to create the
  13185. |FormArray|.
  13186. @<FormArray implementation@>=
  13187. FormArray::FormArray(QDomElement description) : configuration(description),
  13188. maxwidth(-1), maxheight(-1)@/
  13189. {
  13190. setWidget(&itemContainer);
  13191. itemContainer.setLayout(&itemLayout);
  13192. }
  13193. @ The |FormArray| was initially created by an XML element. A copy of this is
  13194. stored in the private variable |configuration|. This can have the same child
  13195. elements as {\tt <widget>}, allowing us to reuse the function for creating
  13196. populating the widget. When adding a new element, we must resize the
  13197. |itemContainer|, otherwise Qt will attempt to cram all widgets in the layout
  13198. into the same vertical space as was previously required. The result is not
  13199. attractive. We also set a minimum width just in case the newly created widget is
  13200. the first one added to the area.
  13201. @<FormArray implementation@>=
  13202. void FormArray::addElements(int copies)
  13203. {
  13204. QStack<QWidget *> *widgetStack = new QStack<QWidget *>;
  13205. QStack<QLayout *> *layoutStack = new QStack<QLayout *>;
  13206. QWidget *widget;
  13207. for(int i = 0; i < copies; i++)
  13208. {
  13209. widget = new QWidget;
  13210. if(maxwidth > -1)
  13211. {
  13212. widget->setMaximumWidth(maxwidth);
  13213. }
  13214. if(maxheight > -1)
  13215. {
  13216. widget->setMaximumHeight(maxheight);
  13217. }
  13218. if(configuration.hasChildNodes())
  13219. {
  13220. widgetStack->push(widget);
  13221. populateWidget(configuration, widgetStack, layoutStack);
  13222. widgetStack->pop();
  13223. widget->setMinimumHeight(widget->sizeHint().height());
  13224. itemLayout.addWidget(widget);
  13225. if(widget->sizeHint().height() > maxheight && maxheight > -1)
  13226. {
  13227. itemContainer.setMinimumHeight(maxheight * elements() + 50);
  13228. }
  13229. else
  13230. {
  13231. itemContainer.setMinimumHeight(itemContainer.sizeHint().height()
  13232. + widget->sizeHint().height());
  13233. }
  13234. if(maxwidth > -1)
  13235. {
  13236. itemContainer.setMinimumWidth(maxwidth + 50);
  13237. }
  13238. else
  13239. {
  13240. itemContainer.setMinimumWidth(widget->sizeHint().width() + 50);
  13241. }
  13242. }
  13243. }
  13244. }
  13245. @ In order to retrieve a widget from the area, we use the |elementAt()| method.
  13246. The pointer returned by this function can be used as the first argument to
  13247. |findChildObject()| in the host environment in order to find any widget in the
  13248. form.
  13249. @<FormArray implementation@>=
  13250. QWidget* FormArray::elementAt(int index)
  13251. {
  13252. if(index < itemLayout.count())
  13253. {
  13254. QLayoutItem *item = itemLayout.itemAt(index);
  13255. return item->widget();
  13256. }
  13257. else
  13258. {
  13259. return NULL;
  13260. }
  13261. }
  13262. @ Removing all elements is trivial, however we must be sure to reset the size of
  13263. |itemContainer|.
  13264. @<FormArray implementation@>=
  13265. void FormArray::removeAllElements()
  13266. {
  13267. while(itemLayout.count() > 0)
  13268. {
  13269. QLayoutItem *item;
  13270. item = itemLayout.itemAt(0);
  13271. item->widget()->hide();
  13272. itemLayout.removeWidget(item->widget());
  13273. }
  13274. itemContainer.setMinimumHeight(0);
  13275. }
  13276. @ This just leaves a method for determining the number of elements already in
  13277. the view. This is equal to the number of items in the layout.
  13278. @<FormArray implementation@>=
  13279. int FormArray::elements()
  13280. {
  13281. return itemLayout.count();
  13282. }
  13283. @ Some widgets do not behave well in a |FormArray| setting and will try to use
  13284. an excess of screen space. In these cases, constraining the size of the elements
  13285. can be beneficial. These just set private member variables which are used when
  13286. adding new elements.
  13287. @<FormArray implementation@>=
  13288. void FormArray::setMaximumElementWidth(int width)
  13289. {
  13290. maxwidth = width;
  13291. }
  13292. void FormArray::setMaximumElementHeight(int height)
  13293. {
  13294. maxheight = height;
  13295. }
  13296. @ In order to create an instance of this class from the configuration document,
  13297. a {\tt <formarray>} element is used. This can be added to any layout.
  13298. @<Function prototypes for scripting@>=
  13299. void addFormArrayToLayout(QDomElement element, QStack<QWidget *> *widgetStack,@|
  13300. QStack<QLayout *> *layoutStack);
  13301. @ Processing child elements is deferred until a call to
  13302. |FormArray::addElements()| has been made.
  13303. @<Functions for scripting@>=
  13304. void addFormArrayToLayout(QDomElement element, QStack<QWidget *> *,@|
  13305. QStack<QLayout *> *layoutStack)
  13306. {
  13307. FormArray *widget = new FormArray(element);
  13308. if(element.hasAttribute("id"))
  13309. {
  13310. widget->setObjectName(element.attribute("id"));
  13311. }
  13312. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  13313. layout->addWidget(widget);
  13314. }
  13315. @** Scale widgets.
  13316. \noindent One of the most commonly used methods of documenting the properties of
  13317. a coffee is through a cupping form. Several such forms exist to meet different
  13318. needs, however most involve several scales which can be marked to determine some
  13319. aspect of a particular attribute. Some of these scales are scored values which,
  13320. when considered with other scored values produce a numerical representation of
  13321. the quality of a given coffee. Others are unscored values which serve to provide
  13322. additional documentation of a property. For example, when the SCAA cupping form
  13323. expanded to the current form of ten scored properties with defects subtracted
  13324. from the total, a second scale was added for the acidity property. The scored
  13325. scale is used for marking the quality of the acidity while another unscored
  13326. scale is used for marking the intensity of that acidity.
  13327. Previously, in order to enter cupping information in \pn{}, numeric entry fields
  13328. were used. While this was very efficient for transcribing paper cupping forms
  13329. (and this was, in fact, the principal use case for our prototype cupping form
  13330. database software), it is not a form that lends itself to convenient use at the
  13331. cupping table.
  13332. Two new widgets are therefore introduced which allows a cupper to simply click
  13333. at some point on the scale to record that impression. Unfortunately, this is
  13334. still something of a tradeoff. It is not quite so efficient as using a paper
  13335. form in my experience, however it is faster than transcribing a stack of cupping
  13336. forms, particularly when working with cuppers with ambiguous handwriting. By
  13337. moving data acquisition to the point of data generation, a more useful record
  13338. can be produced for use in aggregate analysis.
  13339. @* The Horizontal Scale.
  13340. \noindent Several cupping forms make use of 10 point scales for the quality of
  13341. various attributes. The |ScaleControl| widget provides such a scale. It
  13342. consists of a bar with major ticks at 0, 5, and 10 and minor ticks at integer
  13343. values within this range. The first click sets both the initial and final value
  13344. of the scale while subsequent clicks adjust only the final value. A pair of
  13345. controls at each end of the scale allows the user to adjust either of these
  13346. values to compensate for imprecision at the point of the click. The two values,
  13347. an initial unscored value and a final scored value, provide some limited
  13348. temporal documentation. That is, it documents how the perception of a coffee
  13349. changes as it cools.
  13350. The widget is implemented as a |QGraphicsView| subclass. Please note that the
  13351. scale widgets are not particularly robust. In order to support a broader range
  13352. of cupping forms, there are plans to extend this class to allow user defined
  13353. range and tick patterns and user defined colors for the indicators.
  13354. @<Class declarations@>=
  13355. class ScaleControl : public QGraphicsView@/
  13356. {@t\1@>@/
  13357. Q_OBJECT@/
  13358. Q_PROPERTY(double initialValue READ initialValue WRITE setInitialValue)@/
  13359. Q_PROPERTY(double finalValue READ finalValue WRITE setFinalValue)@/
  13360. @<ScaleControl private members@>@t\2\2@>@/
  13361. public:@/
  13362. ScaleControl();
  13363. double initialValue(void);
  13364. double finalValue(void);
  13365. virtual QSize sizeHint() const;@/
  13366. @[public slots@]:@/
  13367. void setInitialValue(double value);
  13368. void setFinalValue(double value);@/
  13369. signals:@/
  13370. void initialChanged(double);
  13371. void finalChanged(double);@/
  13372. protected:@/
  13373. virtual void mousePressEvent(QMouseEvent *event);
  13374. virtual void mouseReleaseEvent(QMouseEvent *event);@t\2@>@/
  13375. }@t\kern-4pt@>;
  13376. @ The private variables available to instances of this class are used for
  13377. managing various aspects of the widget.
  13378. @<ScaleControl private members@>=
  13379. QGraphicsScene scene;
  13380. QGraphicsPolygonItem initialDecrement;
  13381. QGraphicsPolygonItem initialIncrement;
  13382. QGraphicsPolygonItem finalDecrement;
  13383. QGraphicsPolygonItem finalIncrement;
  13384. QGraphicsPolygonItem initialIndicator;
  13385. QGraphicsPolygonItem finalIndicator;
  13386. QGraphicsPathItem scaleLine;
  13387. QPolygonF left;
  13388. QPolygonF right;
  13389. QPolygonF down;
  13390. QPolygonF up;
  13391. QPainterPath scalePath;
  13392. QBrush initialBrush;
  13393. QBrush finalBrush;
  13394. double nonScoredValue;
  13395. double scoredValue;
  13396. bool initialSet;
  13397. bool finalSet;
  13398. bool scaleDown;
  13399. @ The constructor sets up the scene displayed by this widget. There is
  13400. considerable room for improvement here.
  13401. @<ScaleControl implementation@>=
  13402. ScaleControl::ScaleControl() : QGraphicsView(NULL, NULL), nonScoredValue(-1),
  13403. scoredValue(-1), initialSet(false), finalSet(false), scaleDown(false)
  13404. {
  13405. left << QPointF(0, 5) << QPointF(10, 0) << QPointF(10, 10) <<
  13406. QPointF(0, 5);
  13407. right << QPointF(10, 5) << QPointF(0, 0) << QPointF(0, 10) <<
  13408. QPointF(10, 5);
  13409. down << QPointF(0, 0) << QPointF(-5, -10) << QPointF(5, -10) <<
  13410. QPointF(0, 0);
  13411. up << QPointF(0, 0) << QPointF(-5, 10) << QPointF(4, 10) << QPointF(0, 0);
  13412. initialBrush.setColor(QColor(170, 170, 255));
  13413. initialBrush.setStyle(Qt::SolidPattern);
  13414. finalBrush.setColor(Qt::blue);
  13415. finalBrush.setStyle(Qt::SolidPattern);
  13416. initialDecrement.setPolygon(left);
  13417. initialDecrement.setBrush(initialBrush);
  13418. initialDecrement.setPos(0, 0);
  13419. scene.addItem(&initialDecrement);
  13420. initialIncrement.setPolygon(right);
  13421. initialIncrement.setBrush(initialBrush);
  13422. initialIncrement.setPos(122, 0);
  13423. scene.addItem(&initialIncrement);
  13424. finalDecrement.setPolygon(left);
  13425. finalDecrement.setBrush(finalBrush);
  13426. finalDecrement.setPos(0, 12);
  13427. scene.addItem(&finalDecrement);
  13428. finalIncrement.setPolygon(right);
  13429. finalIncrement.setBrush(finalBrush);
  13430. finalIncrement.setPos(122, 12);
  13431. scene.addItem(&finalIncrement);
  13432. scalePath.moveTo(0, 10);
  13433. scalePath.lineTo(100, 10);
  13434. scalePath.moveTo(0, 0);
  13435. scalePath.lineTo(0, 20);
  13436. scalePath.moveTo(10, 5);
  13437. scalePath.lineTo(10, 15);
  13438. scalePath.moveTo(20, 5);
  13439. scalePath.lineTo(20, 15);
  13440. scalePath.moveTo(30, 5);
  13441. scalePath.lineTo(30, 15);
  13442. scalePath.moveTo(40, 5);
  13443. scalePath.lineTo(40, 15);
  13444. scalePath.moveTo(50, 0);
  13445. scalePath.lineTo(50, 20);
  13446. scalePath.moveTo(60, 5);
  13447. scalePath.lineTo(60, 15);
  13448. scalePath.moveTo(70, 5);
  13449. scalePath.lineTo(70, 15);
  13450. scalePath.moveTo(80, 5);
  13451. scalePath.lineTo(80, 15);
  13452. scalePath.moveTo(90, 5);
  13453. scalePath.lineTo(90, 15);
  13454. scalePath.moveTo(100, 0);
  13455. scalePath.lineTo(100, 20);
  13456. scaleLine.setPath(scalePath);
  13457. scaleLine.setPos(16, 1);
  13458. scene.addItem(&scaleLine);
  13459. setScene(&scene);
  13460. initialIndicator.setPolygon(down);
  13461. initialIndicator.setBrush(initialBrush);
  13462. finalIndicator.setPolygon(up);
  13463. finalIndicator.setBrush(finalBrush);
  13464. setMinimumSize(sizeHint());
  13465. setMaximumSize(sizeHint());
  13466. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13467. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13468. setMinimumSize(sizeHint());
  13469. }
  13470. @ The size hint forces a smaller representation of the widget, making it easier
  13471. to arrange with other widgets.
  13472. @<ScaleControl implementation@>=
  13473. QSize ScaleControl::sizeHint() const
  13474. {
  13475. return QSize(140, 30);
  13476. }
  13477. @ The methods for setting the values represented on the scale must ensure that
  13478. the appropriate indicator is drawn and position it appropriately.
  13479. @<ScaleControl implementation@>=
  13480. void ScaleControl::setInitialValue(double value)@t\2\2@>@/
  13481. {@t\1@>@/
  13482. if(value >= 0 && value <= 10)@/
  13483. {@t\1@>@/
  13484. nonScoredValue = value;
  13485. if(!initialSet)
  13486. {
  13487. scene.addItem(&initialIndicator);
  13488. }
  13489. initialSet = true;
  13490. initialIndicator.setPos(value * 10 + 16, 10);
  13491. emit initialChanged(value);
  13492. if(!finalSet)
  13493. {
  13494. setFinalValue(value);
  13495. }@t\2@>@/
  13496. }@t\2@>@/
  13497. }@/@/
  13498. void ScaleControl::setFinalValue(double value)@t\2\2@>@/
  13499. {@t\1@>@/
  13500. if(value >= 0 && value <= 10)@/
  13501. {@t\1@>@/
  13502. scoredValue = value;
  13503. if(!finalSet)@/
  13504. {
  13505. scene.addItem(&finalIndicator);
  13506. }
  13507. finalSet = true;
  13508. finalIndicator.setPos(value * 10 + 16, 11);
  13509. emit finalChanged(value);@t\2@>@/
  13510. }@t\2@>@/
  13511. }
  13512. @ These values can, of course, be retrieved programmatically.
  13513. @<ScaleControl implementation@>=
  13514. double ScaleControl::initialValue(void)
  13515. {
  13516. return nonScoredValue;
  13517. }
  13518. double ScaleControl::finalValue(void)
  13519. {
  13520. return scoredValue;
  13521. }
  13522. @ This only leaves the matter of handling interaction with the widget. A future
  13523. version of this class might split the various interface elements in the scene
  13524. into distinct classes capable of using the event propagation capabilities
  13525. provided by the graphics view framework, however with the current design, we
  13526. must do a little more work.
  13527. There are two events which must be accepted in order to register a click on a
  13528. given portion of the scale. One event is generated when the mouse button is
  13529. pressed.
  13530. @<ScaleControl implementation@>=
  13531. void ScaleControl::mousePressEvent(QMouseEvent *event)
  13532. {
  13533. @<Check that the left button was pressed@>@;
  13534. scaleDown = @[true@];
  13535. event->accept();
  13536. }
  13537. @ The primary action button on the mouse is the left button. While there might
  13538. be sensible interactions to provide in response to other buttons, these are not
  13539. presently supported.
  13540. @<Check that the left button was pressed@>=
  13541. if(event->button() != Qt::LeftButton)
  13542. {
  13543. event->ignore();
  13544. return;
  13545. }
  13546. @ Most of the click event handling is done in response to releasing the mouse
  13547. button. In this event handler we must determine if the click occurred in a
  13548. clickable portion of the scale and take the appropriate action in response.
  13549. @<ScaleControl implementation@>=
  13550. void ScaleControl::mouseReleaseEvent(QMouseEvent *event)@t\2\2@>@/
  13551. {@t\1@>@/
  13552. @<Check that the left button was pressed@>@;
  13553. if(!scaleDown)
  13554. {
  13555. event->ignore();
  13556. return;
  13557. }
  13558. scaleDown = false;
  13559. QPointF sceneCoordinate = mapToScene(event->x(), event->y());
  13560. @<Handle clicks in the decrement controls@>@;
  13561. @<Handle clicks in the increment controls@>@;
  13562. @<Handle clicks in the scale area@>@;
  13563. event->ignore();
  13564. return;@t\2@>@/
  13565. }
  13566. @ As currently implemented, each horizontal pixel position represents a value
  13567. evenly divisible by $0.1$. It is, however, quite common to see vaues with a
  13568. $.25$ or $.75$ after the whole number on cupping forms. In order to make it
  13569. possible to select such values without vastly increasing the length of the
  13570. scale, the increment and decrement controls adjust the represented value by
  13571. $0.05$.
  13572. @<Handle clicks in the decrement controls@>=
  13573. if(sceneCoordinate.x() >= 0 && sceneCoordinate.x() <= 10)
  13574. {
  13575. if(sceneCoordinate.y() >= 0 && sceneCoordinate.y() <= 10)
  13576. {
  13577. if(initialSet)
  13578. {
  13579. setInitialValue(nonScoredValue - 0.05);
  13580. }
  13581. event->accept();
  13582. return;
  13583. }
  13584. else if(sceneCoordinate.y() >= 12 && sceneCoordinate.y() <= 22)
  13585. {
  13586. if(finalSet)
  13587. {
  13588. setFinalValue(scoredValue - 0.05);
  13589. event->accept();
  13590. return;
  13591. }
  13592. }
  13593. }
  13594. @ Incrementing represented values is done in the same manner as decrementing
  13595. them.
  13596. @<Handle clicks in the increment controls@>=
  13597. else if(sceneCoordinate.x() >= 122 && sceneCoordinate.x() <= 132)
  13598. {
  13599. if(sceneCoordinate.y() >= 0 && sceneCoordinate.y() <= 10)
  13600. {
  13601. if(initialSet)
  13602. {
  13603. setInitialValue(nonScoredValue + 0.05);
  13604. event->accept();
  13605. return;
  13606. }
  13607. }
  13608. else if(sceneCoordinate.y() >= 12 && sceneCoordinate.y() <= 22)
  13609. {
  13610. if(finalSet)
  13611. {
  13612. setFinalValue(scoredValue + 0.05);
  13613. event->accept();
  13614. return;
  13615. }
  13616. }
  13617. }
  13618. @ When handling clicks in the scale area, there is a difference between the
  13619. first click and any subsequent click.
  13620. @<Handle clicks in the scale area@>=
  13621. double relativeX = sceneCoordinate.x() - 16;
  13622. if(initialSet)
  13623. {
  13624. if(relativeX >= 0 && relativeX <= 100)
  13625. {
  13626. setFinalValue(relativeX / 10.0);
  13627. event->accept();
  13628. return;
  13629. }
  13630. }
  13631. else
  13632. {
  13633. if(relativeX >= 0 && relativeX <= 100)
  13634. {
  13635. setInitialValue(relativeX / 10.0);
  13636. event->accept();
  13637. return;
  13638. }
  13639. }
  13640. @* The Vertical Scale.
  13641. \noindent In addition to the horizontal scale, there is also a vertical scale.
  13642. The implementation of this class is in some ways a bit simpler as only one value
  13643. must be retained. While there is no prohibition on using this scale for scored
  13644. values (and this might enable a rather compact representation which might be
  13645. useful in some applications), its intent is for unscored values which are less
  13646. likely to change over time. If the dry aroma of a coffee changes significantly
  13647. during a cupping session, you are most likely waiting far too long to pour the
  13648. water.
  13649. @<Class declarations@>=
  13650. class IntensityControl : public QGraphicsView@/
  13651. {@/
  13652. @[Q_OBJECT@]@;
  13653. @[Q_PROPERTY(double value READ value WRITE setValue)@]@;
  13654. QGraphicsScene scene;
  13655. QGraphicsPolygonItem decrement;
  13656. QGraphicsPolygonItem increment;
  13657. QGraphicsPolygonItem indicator;
  13658. QGraphicsPathItem scaleLine;
  13659. QPolygonF left;
  13660. QPolygonF up;
  13661. QPolygonF down;
  13662. QPainterPath scalePath;
  13663. QBrush theBrush;
  13664. double theValue;
  13665. bool valueSet;
  13666. bool scaleDown;
  13667. public:@/
  13668. IntensityControl();
  13669. double value();
  13670. virtual QSize sizeHint() const;@/
  13671. @[public slots@]:@/
  13672. void setValue(double val);@/
  13673. signals:@/
  13674. void valueChanged(double);@/
  13675. protected:@/
  13676. virtual void mousePressEvent(QMouseEvent *event);
  13677. virtual void mouseReleaseEvent(QMouseEvent *event);@/
  13678. };
  13679. @ Note the similarity between this constructor and the the |ScaleControl|
  13680. constructor.
  13681. @<IntensityControl implementation@>=
  13682. IntensityControl::IntensityControl() : QGraphicsView(NULL, NULL), theValue(-1),
  13683. valueSet(false), scaleDown(false)
  13684. {
  13685. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13686. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13687. left << QPointF(0, 0) << QPointF(10, -5) << QPointF(10, 5) << QPointF(0, 0);
  13688. down << QPointF(0, 0) << QPointF(10, 0) << QPointF(5, 10) << QPointF(0, 0);
  13689. up << QPointF(0, 10) << QPointF(10, 10) << QPointF(5, 0) << QPointF(0, 10);
  13690. theBrush.setColor(Qt::blue);
  13691. theBrush.setStyle(Qt::SolidPattern);
  13692. increment.setPolygon(up);
  13693. increment.setBrush(theBrush);
  13694. increment.setPos(0, 0);
  13695. scene.addItem(&increment);
  13696. decrement.setPolygon(down);
  13697. decrement.setBrush(theBrush);
  13698. decrement.setPos(0, 122);
  13699. scene.addItem(&decrement);
  13700. scalePath.moveTo(5, 0);
  13701. scalePath.lineTo(5, 100);
  13702. scalePath.moveTo(0, 0);
  13703. scalePath.lineTo(10, 0);
  13704. scalePath.moveTo(0, 10);
  13705. scalePath.lineTo(10, 10);
  13706. scalePath.moveTo(0, 20);
  13707. scalePath.lineTo(10, 20);
  13708. scalePath.moveTo(0, 30);
  13709. scalePath.lineTo(10, 30);
  13710. scalePath.moveTo(0, 40);
  13711. scalePath.lineTo(10, 40);
  13712. scalePath.moveTo(0, 50);
  13713. scalePath.lineTo(10, 50);
  13714. scalePath.moveTo(0, 60);
  13715. scalePath.lineTo(10, 60);
  13716. scalePath.moveTo(0, 70);
  13717. scalePath.lineTo(10, 70);
  13718. scalePath.moveTo(0, 80);
  13719. scalePath.lineTo(10, 80);
  13720. scalePath.moveTo(0, 90);
  13721. scalePath.lineTo(10, 90);
  13722. scalePath.moveTo(0, 100);
  13723. scalePath.lineTo(10, 100);
  13724. scaleLine.setPath(scalePath);
  13725. scaleLine.setPos(0, 16);
  13726. scene.addItem(&scaleLine);
  13727. setScene(&scene);
  13728. indicator.setPolygon(left);
  13729. indicator.setBrush(theBrush);
  13730. setMinimumSize(sizeHint());
  13731. setMaximumSize(sizeHint());
  13732. }
  13733. @ Once again, the size hint reduces the default size of the widget.
  13734. @<IntensityControl implementation@>=
  13735. QSize IntensityControl::sizeHint() const
  13736. {
  13737. return QSize(25, 160);
  13738. }
  13739. @ To support a more intuitive numerical representation, higher values should map
  13740. to higher positions on the scale. This is contrary to the coordinate system
  13741. provided by Qt, so the code setting the position of the indicator on the scale
  13742. must account for this.
  13743. During testing of this class, I found that it was impossible to select the
  13744. values 0 or 10 either through a click or with the increment or decrement
  13745. controls. Adding two additional execution branches corrects this issue.
  13746. @<IntensityControl implementation@>=
  13747. void IntensityControl::setValue(double val)
  13748. {
  13749. if(val >= 0 && val <= 10)
  13750. {
  13751. theValue = val;
  13752. if(!valueSet)
  13753. {
  13754. scene.addItem(&indicator);
  13755. }
  13756. valueSet = @[true@];
  13757. indicator.setPos(6, (100 - (val * 10)) + 16);
  13758. emit(valueChanged(val));
  13759. }
  13760. else if(val < 1)
  13761. {
  13762. setValue(0);
  13763. }
  13764. else
  13765. {
  13766. setValue(10);
  13767. }
  13768. }
  13769. double IntensityControl::value()
  13770. {
  13771. return theValue;
  13772. }
  13773. @ Mouse event handling is similar as well. The mouse press event simply notes
  13774. that the button has been pressed.
  13775. @<IntensityControl implementation@>=
  13776. void IntensityControl::mousePressEvent(QMouseEvent *event)
  13777. {
  13778. @<Check that the left button was pressed@>@;
  13779. scaleDown = @[true@];
  13780. event->accept();
  13781. }
  13782. @ Since there are fewer clickable areas there are fewer regions to check while
  13783. handling the mouse release event. Just as the |setValue()| method must
  13784. compensate for a mismatch between the scale and the underlying coordinate
  13785. system, so must click handling in the scale area take this into consideration
  13786. when determining which value the click intends.
  13787. @<IntensityControl implementation@>=
  13788. void IntensityControl::mouseReleaseEvent(QMouseEvent *event)
  13789. {
  13790. @<Check that the left button was pressed@>@;
  13791. if(!scaleDown)
  13792. {
  13793. event->ignore();
  13794. return;
  13795. }
  13796. scaleDown = @[false@];
  13797. QPointF sceneCoordinate = mapToScene(event->x(), event->y());
  13798. if(sceneCoordinate.x() >= 0 && sceneCoordinate.x() <= 16)
  13799. {
  13800. if(sceneCoordinate.y() >= 0 && sceneCoordinate.y() <= 10)
  13801. {
  13802. if(valueSet)
  13803. {
  13804. setValue(theValue + 0.05);
  13805. }
  13806. event->accept();
  13807. return;
  13808. }
  13809. else if(sceneCoordinate.y() >= 122 && sceneCoordinate.y() <= 132)
  13810. {
  13811. if(valueSet)
  13812. {
  13813. setValue(theValue - 0.05);
  13814. }
  13815. event->accept();
  13816. return;
  13817. }
  13818. else if(sceneCoordinate.y() >= 16 && sceneCoordinate.y() <= 116)
  13819. {
  13820. setValue(10 - ((sceneCoordinate.y() - 16) / 10.0));
  13821. event->accept();
  13822. return;
  13823. }
  13824. }
  13825. }
  13826. @* Scripting the Scale Widgets.
  13827. \noindent Scale widgets can be added through the configuration system with
  13828. {\tt <hscale>} and {\tt <vscale>} elements. These can be added to layouts.
  13829. @<Function prototypes for scripting@>=
  13830. void addScaleControlToLayout(QDomElement element,
  13831. QStack<QWidget *> *widgetStack,
  13832. QStack<QLayout *> *layoutStack);
  13833. void addIntensityControlToLayout(QDomElement element,
  13834. QStack<QWidget *> *widgetStack,
  13835. QStack<QLayout *> *layoutStack);
  13836. @ Adding these widgets is done in the same way as adding other widgets.
  13837. @<Functions for scripting@>=
  13838. void addScaleControlToLayout(QDomElement element, QStack<QWidget *> *,
  13839. QStack<QLayout *> *layoutStack)
  13840. {
  13841. ScaleControl *scale = new ScaleControl;
  13842. if(element.hasAttribute("id"))
  13843. {
  13844. scale->setObjectName(element.attribute("id"));
  13845. }
  13846. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  13847. layout->addWidget(scale);
  13848. }
  13849. void addIntensityControlToLayout(QDomElement element, QStack<QWidget *> *,
  13850. QStack<QLayout *> *layoutStack)
  13851. {
  13852. IntensityControl *scale = new IntensityControl;
  13853. if(element.hasAttribute("id"))
  13854. {
  13855. scale->setObjectName(element.attribute("id"));
  13856. }
  13857. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  13858. layout->addWidget(scale);
  13859. }
  13860. @** Device Configuration.
  13861. \noindent Starting in \pn{} 1.4, all supported measurement hardware is
  13862. available from the same build and it is possible to use multiple devices from
  13863. differernt vendors at the same time. In previous versions, there were very few
  13864. available hardware configurations and a small number of example configuration
  13865. documents covered the needs of most people using the software. As more hardware
  13866. is supported and more people with distinct needs start using \pn{}, that
  13867. approach becomes unsustainable and the need for in-program configuration
  13868. becomes increasingly pronounced.
  13869. Device configuration is coupled to configuration of the logging window and
  13870. it is possible to configure multiple different roasters with different hardware
  13871. and other customizations of the logging window specific to a particular
  13872. machine.
  13873. The core of this is the use of an XML document saved with |QSettings| under
  13874. the |"DeviceConfiguration"| key. Within the root {\tt <DeviceConfiguration>}
  13875. element there are two grouping elements: {\tt <devices>} and
  13876. {\tt <references>}. The terminology was developed at a time when it was
  13877. thought that hardware configuration and logging view configuration would be
  13878. decoupled from each other, however the benefits of combining these far
  13879. outweighed the minor loss of flexibility with this approach.
  13880. Within the {\tt <devices>} tag there are arbitrarily many {\tt <node>} tags
  13881. which themselves may contain {\tt <node>} tags nested to any depth. Each of
  13882. these has two attributes, a {\tt name} attribute which specifies the display
  13883. text used to represent that node and a {\tt reference} attribute with a
  13884. unique value. Typica will generate a UUID for each node to use as the unique
  13885. value but this is not strictly required. The {\tt name} attribute does not
  13886. need to be unique and will generally be supplied by the person using the
  13887. software. The top level {\tt node} elements represent a coffee roaster and
  13888. the sub-elements can represent hardware, annotation controls, and possibly
  13889. other configurable features.
  13890. Within the {\tt <references>} tag there are as many {\tt <reference>} tags as
  13891. there are {\tt <node>} tags. Each of these has an {\tt id} attribute which
  13892. matches the {\tt reference} attribute in a {\tt <node>} tag and a {\tt driver}
  13893. attribute which is used to determine which class should be used to interact
  13894. with these settings. In the case of device configuration, this allows a
  13895. determination of which class should be used to generate an editor for settings
  13896. related to that node. Within each {\tt <reference>} tag is an arbitrary number
  13897. of {\tt <attribute>} tags with {\tt name} and {\tt value} attributes. Code for
  13898. providing the settings widgets and device interfaces can use these for any
  13899. desired purpose, but it is common to have one {\tt <attribute>} tag per setting
  13900. appropriate for a given node and possibly more to identify the concept a
  13901. particular node represents.
  13902. The global |Application| object is extended to maintain a |QDomDocument|
  13903. representation of this XML document.
  13904. @<Application private data members@>=
  13905. QDomDocument deviceConfigurationDocument;
  13906. @ Two methods are also provided for interacting with this document. The
  13907. |deviceConfiguration()| method returns a pointer to the private data member,
  13908. loading the XML from |QSettings| if required and creating a new document with
  13909. no {\tt <node>} tags if the document does not exist in settings. The
  13910. |saveDeviceConfiguration()| method serializes the |QDomDocument| to settings.
  13911. @<Device configuration members@>=
  13912. QDomDocument deviceConfiguration();
  13913. @ The method for saving should be a slot so a model representing this data
  13914. can persist changes through the signals and slots mechanism rather than
  13915. requiring the calls to be explicitn.
  13916. @<Extended Application slots@>=
  13917. void saveDeviceConfiguration();
  13918. @ Serializing the current configuration is trivial.
  13919. @<Application Implementation@>=
  13920. void Application::saveDeviceConfiguration()
  13921. {
  13922. QSettings settings;
  13923. settings.setValue("DeviceConfiguration",
  13924. QVariant(deviceConfigurationDocument.toByteArray()));
  13925. }
  13926. @ Producing a pointer to a loaded configuration only slightly more complicated.
  13927. If the configuration has been previously loaded we just return the pointer.
  13928. Otherwise, we attempt to load the document.
  13929. @<Application Implementation@>=
  13930. QDomDocument Application::deviceConfiguration()
  13931. {
  13932. if(deviceConfigurationDocument.isNull())
  13933. {
  13934. @<Load device configuration document from settings@>@;
  13935. }
  13936. return deviceConfigurationDocument;
  13937. }
  13938. @ In most cases a document will already exist in settings, but we must verify
  13939. this and create a new document if it does not exist. We also clear device
  13940. configuration settings if the configuration document is invalid.
  13941. @<Load device configuration document from settings@>=
  13942. QSettings settings;
  13943. QByteArray document = settings.value("DeviceConfiguration").toByteArray();
  13944. QString etext;
  13945. int eline;
  13946. int ecol;
  13947. if(document.length() == 0)
  13948. {
  13949. qDebug() << "Loaded settings length is 0. Creating new configuration.";
  13950. @<Create device configuration document@>@;
  13951. }
  13952. else
  13953. {
  13954. if(!deviceConfigurationDocument.setContent(document, false,
  13955. &etext, &eline, &ecol))
  13956. {
  13957. @<Report configuration loading error@>@;
  13958. @<Create device configuration document@>@;
  13959. }
  13960. }
  13961. @ Rather than generate the empty device configuration programmatically, we keep
  13962. an empty device configuration document as a resource.
  13963. @<Create device configuration document@>=
  13964. QFile emptyDocument(":/resources/xml/EmptyDeviceConfiguration.xml");
  13965. emptyDocument.open(QIODevice::ReadOnly);
  13966. if(!deviceConfigurationDocument.setContent(&emptyDocument, false,
  13967. &etext, &eline, &ecol))
  13968. {
  13969. @<Report configuration loading error@>@;
  13970. }
  13971. else
  13972. {
  13973. saveDeviceConfiguration();
  13974. }
  13975. @ There isn'@q'@>t really anything that can be done if the device configuration data
  13976. is corrupt, but an error message can be produced if the program happens to have
  13977. access to a debugging console.
  13978. @<Report configuration loading error@>=
  13979. qDebug() << QString(tr("An error occurred loading device configuration."));
  13980. qDebug() << QString(tr("Line %1, Column %2")).arg(eline).arg(ecol);
  13981. qDebug() << etext;
  13982. @* Model and view for device configuration hierarchy.
  13983. \noindent When manipulating device configuration data, it can be useful to
  13984. present the device hierarchy in a tree view. To do this, we use two classes to
  13985. produce a tree model. This is slightly extended and modified from an example
  13986. in the Qt documentation.\nfnote{Simple DOM Model Example:\par\indent\pdfURL{%
  13987. http://doc.qt.nokia.com/4.7-snapshot/itemviews-simpledommodel.html}
  13988. {http://doc.qt.nokia.com/4.7-snapshot/itemviews-simpledommodel.html}}
  13989. Our model uses the |DeviceTreeModelNode| class to cache the |QDomNode|
  13990. instances with the data we want.
  13991. @<Class declarations@>=
  13992. class DeviceTreeModelNode
  13993. {
  13994. public:
  13995. DeviceTreeModelNode(QDomNode &node, int row,
  13996. DeviceTreeModelNode *parent = NULL);
  13997. ~DeviceTreeModelNode();
  13998. DeviceTreeModelNode *child(int index);
  13999. DeviceTreeModelNode *parent();
  14000. QDomNode node() const;
  14001. int row();
  14002. private:
  14003. QDomNode domNode;
  14004. QHash<int, DeviceTreeModelNode*> children;
  14005. int rowNumber;
  14006. DeviceTreeModelNode *parentItem;
  14007. };
  14008. @ Implementation of this helper class is trivial.
  14009. @<DeviceTreeModelNode implementation@>=
  14010. DeviceTreeModelNode::DeviceTreeModelNode(QDomNode &node, int row,
  14011. DeviceTreeModelNode *parent)
  14012. : domNode(node), rowNumber(row), parentItem(parent)
  14013. {
  14014. /* Nothing needs to be done here. */
  14015. }
  14016. DeviceTreeModelNode::~DeviceTreeModelNode()
  14017. {
  14018. QHash<int, DeviceTreeModelNode *>::iterator@, i;
  14019. for(i = children.begin(); i != children.end(); i++)
  14020. {
  14021. delete i.value();
  14022. }
  14023. }
  14024. DeviceTreeModelNode *DeviceTreeModelNode::parent()
  14025. {
  14026. return parentItem;
  14027. }
  14028. int DeviceTreeModelNode::row()
  14029. {
  14030. return rowNumber;
  14031. }
  14032. QDomNode DeviceTreeModelNode::node() const
  14033. {
  14034. return domNode;
  14035. }
  14036. DeviceTreeModelNode *DeviceTreeModelNode::child(int index)
  14037. {
  14038. if(children.contains(index))
  14039. {
  14040. return children[index];
  14041. }
  14042. if(index >= 0 && index < domNode.childNodes().count())
  14043. {
  14044. QDomNode childNode = domNode.childNodes().item(index);
  14045. DeviceTreeModelNode *childItem = new DeviceTreeModelNode(childNode,
  14046. index, this);
  14047. children[index] = childItem;
  14048. return childItem;
  14049. }
  14050. return NULL;
  14051. }
  14052. @ The model class provides a single column representation of the {\tt devices}
  14053. section of the device configuration document. It provides methods for editing
  14054. the name of any node, for adding new nodes to the model, for deleting any node
  14055. in the model, and for obtaining the {\tt reference} element corresponding to
  14056. a given node.
  14057. @<Class declarations@>=
  14058. class DeviceTreeModel : public QAbstractItemModel@/
  14059. {@/
  14060. @[Q_OBJECT@]@;
  14061. public:@/
  14062. DeviceTreeModel(QObject *parent = NULL);
  14063. ~DeviceTreeModel();
  14064. QVariant data(const QModelIndex &index, int role) const;
  14065. Qt::ItemFlags flags(const QModelIndex &index) const;
  14066. QVariant headerData(int section, Qt::Orientation orientation,
  14067. int role = Qt::DisplayRole) const;
  14068. QModelIndex index(int row, int column,
  14069. const QModelIndex &parent = QModelIndex()) const;
  14070. QModelIndex parent(const QModelIndex &child) const;
  14071. Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const;
  14072. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  14073. bool setData(const QModelIndex &index, const QVariant &value,
  14074. int role);
  14075. bool removeRows(int row, int count, const QModelIndex &parent);
  14076. QDomElement referenceElement(const QString &id);
  14077. @[public slots@]:@/
  14078. void newNode(const QString &name, const QString &driver,
  14079. const QModelIndex &parent);
  14080. private:@/
  14081. QDomDocument document;
  14082. DeviceTreeModelNode *root;
  14083. QDomNode referenceSection;
  14084. QDomNode treeRoot;
  14085. };
  14086. @ In the constructor we locate the {\tt devices} and {\tt references} sections
  14087. of the passed in document. Our tree of cached |QDomNode| elements starts with
  14088. the former and is expanded as needed.
  14089. @<DeviceTreeModel implementation@>=
  14090. DeviceTreeModel::DeviceTreeModel(QObject *parent)
  14091. : QAbstractItemModel(parent)
  14092. {
  14093. document = AppInstance->deviceConfiguration();
  14094. QDomNodeList elements = document.elementsByTagName("devices");
  14095. if(elements.size() != 1)
  14096. {
  14097. qDebug() << "Unexpected result when loading device map.";
  14098. }
  14099. treeRoot = elements.at(0);
  14100. root = new DeviceTreeModelNode(treeRoot, 0);
  14101. elements = document.elementsByTagName("references");
  14102. if(elements.size() != 1)
  14103. {
  14104. qDebug() << "No references section. Creating.";
  14105. referenceSection = document.createElement("references");
  14106. document.appendChild(referenceSection);
  14107. }
  14108. else
  14109. {
  14110. referenceSection = elements.at(0);
  14111. }
  14112. connect(this, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
  14113. AppInstance, SLOT(saveDeviceConfiguration()));
  14114. connect(this, SIGNAL(modelReset()),
  14115. AppInstance, SLOT(saveDeviceConfiguration()));
  14116. connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)),
  14117. AppInstance, SLOT(saveDeviceConfiguration()));
  14118. }
  14119. @ We only provide a single column for our model, so |columnCount()| can simply
  14120. return a constant. The |rowCount()| method can return a variety of values
  14121. depending on the parent node.
  14122. @<DeviceTreeModel implementation@>=
  14123. int DeviceTreeModel::columnCount(const QModelIndex &) const
  14124. {
  14125. return 1;
  14126. }
  14127. int DeviceTreeModel::rowCount(const QModelIndex &parent) const
  14128. {
  14129. if(parent.column() > 0)
  14130. {
  14131. return 0;
  14132. }
  14133. @<Get parent item from index@>@;
  14134. return parentItem->node().childNodes().count();
  14135. }
  14136. @ If an invalid index is passed as the parent index, we assume the parent to
  14137. be the root element.
  14138. @<Get parent item from index@>=
  14139. DeviceTreeModelNode *parentItem;
  14140. if(!parent.isValid())
  14141. {
  14142. parentItem = root;
  14143. }
  14144. else
  14145. {
  14146. parentItem = static_cast<DeviceTreeModelNode *>(parent.internalPointer());
  14147. }
  14148. @ As seen in |rowCount()|, we keep a pointer to the cached node in our model
  14149. indices.
  14150. @<DeviceTreeModel implementation@>=
  14151. QModelIndex DeviceTreeModel::index(int row, int column,
  14152. const QModelIndex &parent) const
  14153. {
  14154. if(!hasIndex(row, column, parent))
  14155. {
  14156. return QModelIndex();
  14157. }
  14158. @<Get parent item from index@>@;
  14159. DeviceTreeModelNode *childItem = parentItem->child(row);
  14160. if(childItem)
  14161. {
  14162. return createIndex(row, column, childItem);
  14163. }
  14164. return QModelIndex();
  14165. }
  14166. @ We can also request an index for the parent of a given index.
  14167. @<DeviceTreeModel implementation@>=
  14168. QModelIndex DeviceTreeModel::parent(const QModelIndex &child) const
  14169. {
  14170. if(!child.isValid())
  14171. {
  14172. return QModelIndex();
  14173. }
  14174. DeviceTreeModelNode *childItem =
  14175. static_cast<DeviceTreeModelNode *>(child.internalPointer());
  14176. DeviceTreeModelNode *parentItem = childItem->parent();
  14177. if(!parentItem || parentItem == root)
  14178. {
  14179. return QModelIndex();
  14180. }
  14181. return createIndex(parentItem->row(), 0, parentItem);
  14182. }
  14183. @ All items should be enabled, selectable, and editable.
  14184. @<DeviceTreeModel implementation@>=
  14185. Qt::ItemFlags DeviceTreeModel::flags(const QModelIndex &index) const
  14186. {
  14187. if(!index.isValid())
  14188. {
  14189. return 0;
  14190. }
  14191. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
  14192. }
  14193. @ Each node in the model maintains two pieces of information. One is the
  14194. display value for the node which is held in the {\tt name} attribute of the
  14195. corresponding XML element. The other is a reference ID held in the
  14196. {\tt reference} attribute.
  14197. @<DeviceTreeModel implementation@>=
  14198. QVariant DeviceTreeModel::data(const QModelIndex &index, int role) const
  14199. {
  14200. if(!index.isValid())
  14201. {
  14202. return QVariant();
  14203. }
  14204. if(role != Qt::DisplayRole && role != Qt::UserRole && role != Qt::EditRole)
  14205. {
  14206. return QVariant();
  14207. }
  14208. DeviceTreeModelNode *item =
  14209. static_cast<DeviceTreeModelNode *>(index.internalPointer());
  14210. QDomNode node = item->node();
  14211. QDomElement element = node.toElement();
  14212. switch(role)
  14213. {
  14214. case Qt::DisplayRole:@/
  14215. case Qt::EditRole:@/
  14216. return QVariant(element.attribute("name"));
  14217. case Qt::UserRole:@/
  14218. return QVariant(element.attribute("reference"));
  14219. default:@/
  14220. return QVariant();
  14221. }
  14222. return QVariant();
  14223. }
  14224. @ The reference value is managed by the model and should never be changed. The
  14225. display value for a node is, however, editable. These changes must propagate
  14226. back to the XML document underlying the model.
  14227. @<DeviceTreeModel implementation@>=
  14228. bool DeviceTreeModel::setData(const QModelIndex &index,
  14229. const QVariant &value, int)@;@/
  14230. {@t\1@>@/
  14231. if(!index.isValid())@/
  14232. {@t\1@>@/
  14233. return false;@t\2@>@/
  14234. }@/
  14235. DeviceTreeModelNode *item =
  14236. static_cast<DeviceTreeModelNode *>(index.internalPointer());
  14237. QDomNode node = item->node();
  14238. QDomElement element = node.toElement();
  14239. element.setAttribute("name", value.toString());
  14240. emit dataChanged(index, index);@;
  14241. return true;@t\2@>@/
  14242. }
  14243. @ A custom method is provided for adding new nodes to the model. This generates
  14244. the two XML elements needed for the node. The |name| parameter is the display
  14245. name of the new node, the |driver| parameter is used as the value for the
  14246. {\tt driver} attribute in the {\tt reference} element which will be used to
  14247. determine what classes are used to work with that data.
  14248. @<DeviceTreeModel implementation@>=
  14249. void DeviceTreeModel::newNode(const QString &name, const QString &driver,
  14250. const QModelIndex &parent)
  14251. {
  14252. QString referenceID = QUuid::createUuid().toString();
  14253. @<Get parent item from index@>@;
  14254. QDomNode parentNode = parentItem->node();
  14255. int newRowNumber = rowCount(parent);
  14256. beginInsertRows(parent, newRowNumber, newRowNumber);
  14257. QDomElement deviceElement = document.createElement("node");
  14258. deviceElement.setAttribute("name", name);
  14259. deviceElement.setAttribute("reference", referenceID);
  14260. parentNode.appendChild(deviceElement);
  14261. QDomElement referenceElement = document.createElement("reference");
  14262. referenceElement.setAttribute("id", referenceID);
  14263. referenceElement.setAttribute("driver", driver);
  14264. referenceSection.appendChild(referenceElement);
  14265. endInsertRows();
  14266. }
  14267. @ We can also delete nodes. When deleting a node, both XML elements are
  14268. removed and our node cache is invalidated.
  14269. @<DeviceTreeModel implementation@>=
  14270. bool DeviceTreeModel::removeRows(int row, int count, const QModelIndex &parent)@t\2\2@>@/
  14271. {@t\1@>@/
  14272. @<Get parent item from index@>@;
  14273. QDomNode parentNode = parentItem->node();
  14274. QDomNodeList childNodes = parentNode.childNodes();@;
  14275. if(childNodes.size() < row + count)@/
  14276. {@t\1@>@/
  14277. return false;@t\2@>@/
  14278. }@/
  14279. beginRemoveRows(parent, row, row + count - 1);
  14280. QList<QDomElement> removalList;
  14281. for(int i = row; i < row + count; i++)
  14282. {
  14283. removalList.append(childNodes.at(i).toElement());
  14284. }
  14285. QDomElement element;
  14286. QDomElement reference;
  14287. for(int i = 0; i < count; i++)
  14288. {
  14289. element = removalList.at(i);
  14290. if(element.hasAttribute("reference"))
  14291. {
  14292. reference = referenceElement(element.attribute("reference"));
  14293. if(!reference.isNull())
  14294. {
  14295. referenceSection.removeChild(reference);
  14296. }
  14297. }
  14298. parentNode.removeChild(element);
  14299. }
  14300. endRemoveRows();
  14301. beginResetModel();
  14302. delete root;
  14303. root = new DeviceTreeModelNode(treeRoot, 0);
  14304. endResetModel();@;
  14305. return true;@t\2@>@/
  14306. }
  14307. @ Another custom method obtains the {\tt reference} element for a given
  14308. reference ID.
  14309. @<DeviceTreeModel implementation@>=
  14310. QDomElement DeviceTreeModel::referenceElement(const QString &id)
  14311. {
  14312. QDomNodeList childNodes = referenceSection.childNodes();
  14313. QDomElement element;
  14314. for(int i = 0; i < childNodes.size(); i++)
  14315. {
  14316. element = childNodes.at(i).toElement();
  14317. if(element.hasAttribute("id"))
  14318. {
  14319. if(element.attribute("id") == id)
  14320. {
  14321. return element;
  14322. }
  14323. }
  14324. }
  14325. return QDomElement();
  14326. }
  14327. @ We don'@q'@>t want any headers, so |headerData()| is very simple.
  14328. @<DeviceTreeModel implementation@>=
  14329. QVariant DeviceTreeModel::headerData(int, Qt::Orientation, int) const
  14330. {
  14331. return QVariant();
  14332. }
  14333. @ The destructor destroys the node cache. The destructor for the top level node
  14334. will recursively destroy all child nodes.
  14335. @<DeviceTreeModel implementation@>=
  14336. DeviceTreeModel::~DeviceTreeModel()
  14337. {
  14338. delete root;
  14339. }
  14340. @ Exposing this class to the host environment allows a number of interesting
  14341. possibilities. Setting the model to a combo box, for example, allows the
  14342. selection of top level nodes representing a particular coffee roaster. It is
  14343. also useful to have the ability to traverse a specified sub-tree of the model
  14344. to set up a logging view that matches the configuration for such a selected
  14345. roaster.
  14346. @<Function prototypes for scripting@>=
  14347. QScriptValue constructDeviceTreeModel(QScriptContext *context,
  14348. QScriptEngine *engine);
  14349. void setDeviceTreeModelProperties(QScriptValue value, QScriptEngine *engine);
  14350. void setQAbstractItemModelProperties(QScriptValue value, QScriptEngine *engine);
  14351. QScriptValue DeviceTreeModel_referenceElement(QScriptContext *context,
  14352. QScriptEngine *engine);
  14353. QScriptValue QAbstractItemModel_data(QScriptContext *context, QScriptEngine *engine);
  14354. QScriptValue QAbstractItemModel_index(QScriptContext *context, QScriptEngine *engine);
  14355. QScriptValue QAbstractItemModel_rowCount(QScriptContext *context, QScriptEngine *engine);
  14356. QScriptValue QAbstractItemModel_hasChildren(QScriptContext *context, QScriptEngine *engine);
  14357. @ The constructor is trivial.
  14358. @<Functions for scripting@>=
  14359. QScriptValue constructDeviceTreeModel(QScriptContext *, QScriptEngine *engine)
  14360. {
  14361. QScriptValue object = engine->newQObject(new DeviceTreeModel);
  14362. setDeviceTreeModelProperties(object, engine);
  14363. return object;
  14364. }
  14365. @ As usual the host environment is informed of this constructor.
  14366. @<Set up the scripting engine@>=
  14367. constructor = engine->newFunction(constructDeviceTreeModel);
  14368. value = engine->newQMetaObject(&DeviceTreeModel::staticMetaObject,
  14369. constructor);
  14370. engine->globalObject().setProperty("DeviceTreeModel", value);
  14371. @ A number of properties are set to allow script code to traverse the model.
  14372. Most of these properties are properly members of |QAbstractItemModel| and
  14373. the code is written to allow any models that may be exposed to the host
  14374. environment in the future to make use of these as well. Note that this is not
  14375. a full set of functionality but only what I needed to implement a particular
  14376. feature set.
  14377. @<Functions for scripting@>=
  14378. void setDeviceTreeModelProperties(QScriptValue value, QScriptEngine *engine)
  14379. {
  14380. setQAbstractItemModelProperties(value, engine);
  14381. value.setProperty("referenceElement",
  14382. engine->newFunction(DeviceTreeModel_referenceElement));
  14383. }
  14384. void setQAbstractItemModelProperties(QScriptValue value, QScriptEngine *engine)
  14385. {
  14386. setQObjectProperties(value, engine);
  14387. value.setProperty("data", engine->newFunction(QAbstractItemModel_data));
  14388. value.setProperty("index", engine->newFunction(QAbstractItemModel_index));
  14389. value.setProperty("rowCount", engine->newFunction(QAbstractItemModel_rowCount));
  14390. value.setProperty("hasChildren", engine->newFunction(QAbstractItemModel_hasChildren));
  14391. }
  14392. @ The wrapped call to |referenceElement| does a little more than might be
  14393. expected. Rather than returning a |QDomElement| and leaving it up to script
  14394. code to traverse the sub-tree, we create a |QVariantMap| which in script code
  14395. is translated as an object with the keys as properties of the object containing
  14396. the values of those keys. This is populated by first specifying a {\tt driver}
  14397. key with its value from the {\tt driver} attribute of the {\tt reference} node.
  14398. We then examine the {\tt <attribute>} sub-elements and use the {\tt name}
  14399. attribute as keys and the {\tt value} attribute as values to fill out the rest
  14400. of the map.
  14401. @<Functions for scripting@>=
  14402. QScriptValue DeviceTreeModel_referenceElement(QScriptContext *context,
  14403. QScriptEngine *engine)
  14404. {
  14405. DeviceTreeModel *model = getself<DeviceTreeModel *>(context);
  14406. QDomElement referenceElement = model->referenceElement(argument<QString>(0, context));
  14407. QDomNodeList configData = referenceElement.elementsByTagName("attribute");
  14408. QDomElement node;
  14409. QVariantMap retval;
  14410. retval.insert("driver", referenceElement.attribute("driver"));
  14411. for(int i = 0; i < configData.size(); i++)
  14412. {
  14413. node = configData.at(i).toElement();
  14414. retval.insert(node.attribute("name"), node.attribute("value"));
  14415. }
  14416. return engine->toScriptValue(retval);
  14417. }
  14418. QScriptValue QAbstractItemModel_data(QScriptContext *context, QScriptEngine *engine)
  14419. {
  14420. QAbstractItemModel *model = getself<QAbstractItemModel *>(context);
  14421. QModelIndex index = argument<QModelIndex>(0, context);
  14422. int role = argument<int>(1, context);
  14423. return engine->toScriptValue(model->data(index, role));
  14424. }
  14425. QScriptValue QAbstractItemModel_index(QScriptContext *context, QScriptEngine *engine)
  14426. {
  14427. QAbstractItemModel *model = getself<QAbstractItemModel *>(context);
  14428. int row = 0;
  14429. int column = 0;
  14430. QModelIndex index;
  14431. if(context->argumentCount() > 1)
  14432. {
  14433. row = argument<int>(0, context);
  14434. column = argument<int>(1, context);
  14435. }
  14436. if(context->argumentCount() > 2)
  14437. {
  14438. index = argument<QModelIndex>(2, context);
  14439. }
  14440. QModelIndex retval = model->index(row, column, index);
  14441. return engine->toScriptValue(retval);
  14442. }
  14443. QScriptValue QAbstractItemModel_rowCount(QScriptContext *context,
  14444. QScriptEngine *)
  14445. {
  14446. QAbstractItemModel *model = getself<QAbstractItemModel *>(context);
  14447. QModelIndex index;
  14448. if(context->argumentCount() == 1)
  14449. {
  14450. index = argument<QModelIndex>(0, context);
  14451. }
  14452. return QScriptValue(model->rowCount(index));
  14453. }
  14454. QScriptValue QAbstractItemModel_hasChildren(QScriptContext *context,
  14455. QScriptEngine *engine)
  14456. {
  14457. QAbstractItemModel *model = getself<QAbstractItemModel *>(context);
  14458. QModelIndex index;
  14459. if(context->argumentCount() == 1)
  14460. {
  14461. index = argument<QModelIndex>(0, context);
  14462. }
  14463. return QScriptValue(engine, model->hasChildren(index));
  14464. }
  14465. @ Some additional work is needed to handle |QModelIndex| appropriately. First
  14466. we declare that as a metatype.
  14467. @<Class declarations@>=
  14468. Q_DECLARE_METATYPE(QModelIndex)
  14469. @ Next we need a pair of functions to convert |QModelIndex| to and from script
  14470. values. Some |QModelIndex| methods are also exposed to the host environment.
  14471. @<Function prototypes for scripting@>=
  14472. QScriptValue QModelIndex_toScriptValue(QScriptEngine *engine, const QModelIndex &index);
  14473. void QModelIndex_fromScriptValue(const QScriptValue &value, QModelIndex &index);
  14474. void setQModelIndexProperties(QScriptValue value, QScriptEngine *engine);
  14475. QScriptValue QModelIndex_row(QScriptContext *context, QScriptEngine *engine);
  14476. @ These are implemented thusly.
  14477. @<Functions for scripting@>=
  14478. QScriptValue QModelIndex_toScriptValue(QScriptEngine *engine, const QModelIndex &index)
  14479. {
  14480. QVariant var;
  14481. var.setValue(index);
  14482. QScriptValue object = engine->newVariant(var);
  14483. setQModelIndexProperties(object, engine);
  14484. return object;
  14485. }
  14486. void QModelIndex_fromScriptValue(const QScriptValue &value, QModelIndex &index)
  14487. {
  14488. index = value.toVariant().value<QModelIndex>();
  14489. }
  14490. void setQModelIndexProperties(QScriptValue value, QScriptEngine *engine)
  14491. {
  14492. value.setProperty("row", engine->newFunction(QModelIndex_row));
  14493. }
  14494. QScriptValue QModelIndex_row(QScriptContext *context, QScriptEngine *engine)
  14495. {
  14496. QModelIndex self = getself<QModelIndex>(context);
  14497. return QScriptValue(engine, self.row());
  14498. }
  14499. @ Finally we register this with the engine.
  14500. @<Set up the scripting engine@>=
  14501. qScriptRegisterMetaType(engine, QModelIndex_toScriptValue, QModelIndex_fromScriptValue);
  14502. @* Device Configuration Widgets.
  14503. \noindent Each node in the {\tt devices} section of the |DeviceTreeModel| is
  14504. associated with a {\tt reference} element that provides a driver string which
  14505. can be used to identify the classes used to interact with the device
  14506. configuration data. An example of this is selecting which widget to use when
  14507. selecting a node in a configuration window. These widgets must be registered
  14508. to the appropriate driver string in advance. This is currently handled through
  14509. the |Application| instance, though it would probably be better to split this
  14510. into its own class at some point in the future.
  14511. @<Application private data members@>=
  14512. QHash<QString, QMetaObject> deviceConfigurationWidgets;
  14513. @ Two methods register widgets and retrieve an instance of the appropriate
  14514. widget for a given node in the device configuration model.
  14515. @<Device configuration members@>=
  14516. void registerDeviceConfigurationWidget(QString driver, QMetaObject widget);
  14517. QWidget* deviceConfigurationWidget(DeviceTreeModel *model,
  14518. const QModelIndex &index);
  14519. @ Registration is a simple wrapper around the underlying |QHash|.
  14520. @<Application Implementation@>=
  14521. void Application::registerDeviceConfigurationWidget(QString driver,
  14522. QMetaObject widget)
  14523. {
  14524. deviceConfigurationWidgets.insert(driver, widget);
  14525. }
  14526. @ Obtaining the configuration widget for a given node involves looking up the
  14527. reference element, extracting the driver string, looking up the associated
  14528. meta-object, and returning a new instance of that object.
  14529. As there is no concept of an invalid |QMetaObject| we default to the static
  14530. meta-object for a |QWidget| if a widget for the specified driver string is not
  14531. registered and check for this prior to creating a new instance of the
  14532. configuration widget.
  14533. @<Application Implementation@>=
  14534. QWidget* Application::deviceConfigurationWidget(DeviceTreeModel *model,
  14535. const QModelIndex &index)
  14536. {
  14537. QVariant nodeReference = index.data(Qt::UserRole);
  14538. QDomElement referenceElement = model->referenceElement(
  14539. model->data(index, Qt::UserRole).toString());
  14540. QMetaObject metaObject =
  14541. deviceConfigurationWidgets.value(referenceElement.attribute("driver"),
  14542. QWidget::staticMetaObject);
  14543. QWidget *editor;
  14544. if(metaObject.className() == QWidget::staticMetaObject.className())
  14545. {
  14546. editor = NULL;
  14547. }
  14548. else
  14549. {
  14550. editor = qobject_cast<QWidget *>(
  14551. metaObject.newInstance(Q_ARG(DeviceTreeModel *, model),
  14552. Q_ARG(QModelIndex, index)));
  14553. }
  14554. return editor;
  14555. }
  14556. @ Every node type should have an associated editor and the editors for nodes
  14557. which can have child nodes should be able to handle creating these child nodes.
  14558. This leaves the problem of creating the top level nodes. For this we must have
  14559. a way to register three key pieces of information: the text which should appear
  14560. for selecting a new top level node to add to the configuration, the default
  14561. name for a node of that type, and the registered driver string associated with
  14562. that node type. The most likely use for this information is in constructing a
  14563. menu. |QAction| seems like a good fit, but this cannot pass all of the
  14564. required information. Part of the chosen solution is a |QAction| subclass
  14565. which takes all three pieces of information and provides a new signal to
  14566. supply the information needed to add a new top level node.
  14567. @<Class declarations@>=
  14568. class NodeInserter : public QAction@/
  14569. {@/
  14570. @[Q_OBJECT@]@;
  14571. public:@/
  14572. NodeInserter(const QString &title, const QString &name,
  14573. const QString &driver, QObject *parent = NULL);
  14574. signals:@/
  14575. void triggered(QString name, QString driver);
  14576. @[private slots@]:@/
  14577. void onTriggered();
  14578. private:@/
  14579. QString defaultNodeName;
  14580. QString driverString;
  14581. };
  14582. @ The constructor saves the information that will later be emitted and connects
  14583. the |triggered()| signal from |QAction| to a private slot which emits our new
  14584. |triggered()| signal.
  14585. @<NodeInserter implementation@>=
  14586. NodeInserter::NodeInserter(const QString &title, const QString &name,
  14587. const QString &driver, QObject *parent) :
  14588. QAction(title, parent), defaultNodeName(name), driverString(driver)
  14589. {
  14590. connect(this, SIGNAL(triggered()), this, SLOT(onTriggered()));
  14591. }
  14592. void NodeInserter::onTriggered()
  14593. {
  14594. emit triggered(defaultNodeName, driverString);
  14595. }
  14596. @ An interface for adding top level nodes to the device configuration needs to
  14597. be able to access a list of these actions so we make this available through the
  14598. |Application| instance. Once again, it would be better to split device
  14599. configuration registration data to a separate class and there should be
  14600. accessors around this.
  14601. Note that this terminology was introduced when it was assumed that device
  14602. configuration and logging view configuration would be separate. It is likely
  14603. that a future code cleanup will remove this in favor of handling the top level
  14604. of the device configuration hierarchy (under roaster specification) in the same
  14605. way that sub-nodes are handled.
  14606. @<Device configuration members@>=
  14607. QList<NodeInserter *> topLevelNodeInserters;
  14608. @ With this done, we can now produce a window which allows someone to easily
  14609. edit the device configuration.
  14610. As of version 1.6 this class is no longer a window but just a |QWidget| which
  14611. is inserted into another more general settings window. The name of the class
  14612. should be changed in a future version to reflect this change.
  14613. @<Class declarations@>=
  14614. class DeviceConfigurationWindow : public QWidget
  14615. {
  14616. @[Q_OBJECT@]@;
  14617. public:@/
  14618. DeviceConfigurationWindow();
  14619. @[public slots@]:@/
  14620. void addDevice();
  14621. void removeNode();
  14622. void newSelection(const QModelIndex &index);
  14623. @[private slots@]:@/
  14624. void resizeColumn();
  14625. private:@/
  14626. QDomDocument document;
  14627. DeviceTreeModel *model;
  14628. QTreeView *view;
  14629. QScrollArea *configArea;
  14630. };
  14631. @ This window consists of two main panels separated by a splitter. The left
  14632. panel presents a tree view of the current device configuration and a set of
  14633. controls that allows someone to either add a new top level node to the
  14634. configuration or delete any node in the configuration along with all of its
  14635. child nodes.
  14636. The right panel provides a |QScrollArea|. When a node is selected from the tree
  14637. view, the appropriate configuration widget will be inserted into that area.
  14638. When a configuration widget adds a new node to the device model, the parent
  14639. node (which should be the currently selected node but the code does not assume
  14640. this) is expanded to show the new child node if it has not already been
  14641. expanded.
  14642. @<DeviceConfigurationWindow implementation@>=
  14643. DeviceConfigurationWindow::DeviceConfigurationWindow() : QWidget(NULL),
  14644. view(new QTreeView), configArea(new QScrollArea)
  14645. {
  14646. QSplitter *splitter = new QSplitter;
  14647. QWidget *leftWidget = new QWidget;
  14648. leftWidget->setMinimumWidth(200);
  14649. QVBoxLayout *left = new QVBoxLayout;
  14650. view->setAnimated(true);
  14651. view->setSelectionMode(QAbstractItemView::SingleSelection);
  14652. document = AppInstance->deviceConfiguration();
  14653. model = new DeviceTreeModel;
  14654. view->setModel(model);
  14655. view->setTextElideMode(Qt::ElideNone);
  14656. view->expandAll();
  14657. view->resizeColumnToContents(0);
  14658. connect(model, SIGNAL(modelReset()), view, SLOT(expandAll()));
  14659. QHBoxLayout *treeButtons = new QHBoxLayout;
  14660. QToolButton *addDeviceButton = new QToolButton;
  14661. addDeviceButton->setIcon(QIcon::fromTheme("list-add"));
  14662. addDeviceButton->setToolTip(tr("New Roaster"));
  14663. connect(addDeviceButton, SIGNAL(clicked()),
  14664. this, SLOT(addDevice()));
  14665. QToolButton *removeNodeButton = new QToolButton;
  14666. removeNodeButton->setIcon(QIcon::fromTheme("list-remove"));
  14667. removeNodeButton->setToolTip(tr("Delete Selection"));
  14668. connect(removeNodeButton, SIGNAL(clicked()), this, SLOT(removeNode()));
  14669. treeButtons->addWidget(addDeviceButton);
  14670. treeButtons->addWidget(removeNodeButton);
  14671. left->addWidget(view);
  14672. left->addLayout(treeButtons);
  14673. leftWidget->setLayout(left);
  14674. splitter->addWidget(leftWidget);
  14675. configArea->setMinimumWidth(580);
  14676. configArea->setMinimumHeight(460);
  14677. configArea->setWidgetResizable(true);
  14678. splitter->addWidget(configArea);
  14679. QVBoxLayout *centralLayout = new QVBoxLayout;
  14680. centralLayout->addWidget(splitter);
  14681. setLayout(centralLayout);
  14682. connect(view, SIGNAL(activated(QModelIndex)),
  14683. this, SLOT(newSelection(QModelIndex)));
  14684. connect(view, SIGNAL(clicked(QModelIndex)),
  14685. this, SLOT(newSelection(QModelIndex)));
  14686. connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)),
  14687. view, SLOT(expand(QModelIndex)));
  14688. connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)),
  14689. this, SLOT(resizeColumn()));
  14690. connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
  14691. this, SLOT(resizeColumn()));
  14692. }
  14693. @ Adding a new top level node to the model is just a matter of extracting the
  14694. required information from the signal requesting that addition.
  14695. @<DeviceConfigurationWindow implementation@>=
  14696. void DeviceConfigurationWindow::addDevice()
  14697. {
  14698. model->newNode(tr("New Roaster"), "roaster", QModelIndex());
  14699. }
  14700. @ Removing the currently selected node is also simple.
  14701. @<DeviceConfigurationWindow implementation@>=
  14702. void DeviceConfigurationWindow::removeNode()
  14703. {
  14704. QModelIndex index = view->currentIndex();
  14705. if(index.isValid())
  14706. {
  14707. int row = index.row();
  14708. QModelIndex parent = index.parent();
  14709. model->removeRow(row, parent);
  14710. }
  14711. }
  14712. @ Due to most of the required logic being implemented in
  14713. |Application::deviceConfigurationWidget()|, inserting the proper editor in the
  14714. right area is also trivial.
  14715. @<DeviceConfigurationWindow implementation@>=
  14716. void DeviceConfigurationWindow::newSelection(const QModelIndex &index)
  14717. {
  14718. QWidget *editor = AppInstance->deviceConfigurationWidget(model, index);
  14719. if(editor)
  14720. {
  14721. configArea->setWidget(editor);
  14722. editor->show();
  14723. }
  14724. }
  14725. @ As nodes are added deeper in the device hierarchy or as nodes obtain longer
  14726. names, the nodes names may be elided by default rather than indicate that the
  14727. view can be scrolled horizontally. There has been feedback that this behavior
  14728. is not preferred so instead as the model data changes we expand the column
  14729. instead.
  14730. @<DeviceConfigurationWindow implementation@>=
  14731. void DeviceConfigurationWindow::resizeColumn()
  14732. {
  14733. view->resizeColumnToContents(0);
  14734. }
  14735. @ At least for the initial testing of this feature it will be useful if we can
  14736. instantiate this from the host environment. For this we at least require a
  14737. constructor.
  14738. Now that this widget is available through a more general settings window it may
  14739. be better to remove direct access to this class from the host environment.
  14740. @<Function prototypes for scripting@>=
  14741. QScriptValue constructDeviceConfigurationWindow(QScriptContext *context,
  14742. QScriptEngine *engine);
  14743. @ The constructor is trivial.
  14744. @<Functions for scripting@>=
  14745. QScriptValue constructDeviceConfigurationWindow(QScriptContext *,
  14746. QScriptEngine *engine)
  14747. {
  14748. QScriptValue object = engine->newQObject(new DeviceConfigurationWindow);
  14749. return object;
  14750. }
  14751. @ Finally we inform the host environment of this constructor.
  14752. @<Set up the scripting engine@>=
  14753. constructor = engine->newFunction(constructDeviceConfigurationWindow);
  14754. value = engine->newQMetaObject(&DeviceConfigurationWindow::staticMetaObject,
  14755. constructor);
  14756. engine->globalObject().setProperty("DeviceConfigurationWindow", value);
  14757. @* A Base Class for Device Configuration Widgets.
  14758. \noindent There are certain operations that are very commonly required
  14759. among device configuration widgets. These common elements have been implemented
  14760. in a base class.
  14761. @<Class declarations@>=
  14762. class BasicDeviceConfigurationWidget : public QWidget
  14763. {
  14764. @[Q_OBJECT@]@;
  14765. public:@/
  14766. BasicDeviceConfigurationWidget(DeviceTreeModel *model,
  14767. const QModelIndex &index);
  14768. @[public slots@]:@/
  14769. void insertChildNode(const QString &name, const QString &driver);
  14770. void updateAttribute(const QString &name, const QString &value);
  14771. protected:@/
  14772. DeviceTreeModel *deviceModel;
  14773. QModelIndex currentNode;
  14774. };
  14775. @ The constructor just passes its arguments to a pair of protected data
  14776. members. These are commonly required in subclasses but need not be exposed
  14777. outside of this branch of the object hierarchy.
  14778. @<BasicDeviceConfigurationWidget implementation@>=
  14779. BasicDeviceConfigurationWidget::BasicDeviceConfigurationWidget(
  14780. DeviceTreeModel *model, const QModelIndex &index)
  14781. : QWidget(NULL), deviceModel(model), currentNode(index)
  14782. {
  14783. /* Nothing needs to be done here. */
  14784. }
  14785. @ The |updateAttribute()| method sets the value property of an attribute
  14786. element of a given name that is a child of the current node, creating the
  14787. element if it does not exist.
  14788. @<BasicDeviceConfigurationWidget implementation@>=
  14789. void BasicDeviceConfigurationWidget::updateAttribute(const QString &name,
  14790. const QString &value)
  14791. {
  14792. QDomElement referenceElement = deviceModel->referenceElement(
  14793. deviceModel->data(currentNode, Qt::UserRole).toString());
  14794. QDomNodeList configData = referenceElement.elementsByTagName("attribute");
  14795. QDomElement node;
  14796. bool found = @[false@];
  14797. for(int i = 0; i < configData.size(); i++)
  14798. {
  14799. node = configData.at(i).toElement();
  14800. if(node.attribute("name") == name)
  14801. {
  14802. node.setAttribute("value", value);
  14803. found = @[true@];
  14804. break;
  14805. }
  14806. }
  14807. if(!found)
  14808. {
  14809. node = AppInstance->deviceConfiguration().createElement("attribute");
  14810. node.setAttribute("name", name);
  14811. node.setAttribute("value", value);
  14812. referenceElement.appendChild(node);
  14813. }
  14814. AppInstance->saveDeviceConfiguration();
  14815. }
  14816. @ The |insertChildNode()| method constructs a new node with the specified name
  14817. and driver as a child of the current node. Node insertion is a generic
  14818. operation that does not require any knowledge of the configuration options that
  14819. will be presented in that node.
  14820. @<BasicDeviceConfigurationWidget implementation@>=
  14821. void BasicDeviceConfigurationWidget::insertChildNode(const QString &name,
  14822. const QString &driver)
  14823. {
  14824. deviceModel->newNode(name, driver, currentNode);
  14825. }
  14826. @** Configuration of Top Level Roaster Nodes.
  14827. \noindent The first configuration widget required is one for defining a coffee
  14828. roaster. This stores the identification number that will be used for machine
  14829. references in the database and also provides controls for adding all of the
  14830. required child nodes for hardware and configurable elements of the logging
  14831. window that may vary from one machine to the next.
  14832. All of the configuration widgets follow a similar pattern. One important detail
  14833. to note is that these configuration widgets are instantiated through Qt'@q'@>s
  14834. meta-object system. All of these constructors take a |DeviceTreeModel *| and a
  14835. |QModelIndex &| as arguments and they are marked as |Q_INVOKABLE|.
  14836. @<Class declarations@>=
  14837. class RoasterConfWidget : public BasicDeviceConfigurationWidget
  14838. {
  14839. @[Q_OBJECT@]@;
  14840. public:@/
  14841. @[Q_INVOKABLE@]@, RoasterConfWidget(DeviceTreeModel *model,
  14842. const QModelIndex &index);
  14843. @[private slots@]:@/
  14844. void updateRoasterId(int id);
  14845. void updateCapacityCheck(int value);
  14846. void updateCapacity(const QString &value);
  14847. void updateCapacityUnit(const QString &value);
  14848. };
  14849. @ Aside from the ID number used to identify the roaster in the database we also
  14850. need controls to add child nodes. In order to limit the number of options in
  14851. menus for adding child nodes, these are organized into groups that are
  14852. available through different controls.
  14853. @<RoasterConfWidget implementation@>=
  14854. RoasterConfWidget::RoasterConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  14855. : BasicDeviceConfigurationWidget(model, index)
  14856. {
  14857. QVBoxLayout *layout = new QVBoxLayout;
  14858. QPushButton *addDeviceButton = new QPushButton(tr("Add Device"));
  14859. QMenu *deviceMenu = new QMenu;
  14860. NodeInserter *insertAction;
  14861. foreach(insertAction, AppInstance->topLevelNodeInserters)
  14862. {
  14863. connect(insertAction, SIGNAL(triggered(QString, QString)),
  14864. this, SLOT(insertChildNode(QString, QString)));
  14865. deviceMenu->addAction(insertAction);
  14866. }
  14867. addDeviceButton->setMenu(deviceMenu);
  14868. layout->addWidget(addDeviceButton);
  14869. QPushButton *addAnnotationControlButton = new QPushButton(tr("Add Annotation Control"));
  14870. QMenu *annotationMenu = new QMenu;
  14871. NodeInserter *basicButtonInserter = new NodeInserter(tr("Annotation Button"), tr("Annotation Button"), "annotationbutton");
  14872. NodeInserter *countingButtonInserter = new NodeInserter(tr("Counting Button"), tr("Counting Button"), "reconfigurablebutton");
  14873. NodeInserter *spinBoxInserter = new NodeInserter(tr("Numeric Entry"), tr("Numeric Entry"), "annotationspinbox");
  14874. NodeInserter *freeAnnotationInserter = new NodeInserter(tr("Free Text"),
  14875. tr("Free Text"),
  14876. "freeannotation");
  14877. annotationMenu->addAction(basicButtonInserter);
  14878. annotationMenu->addAction(countingButtonInserter);
  14879. annotationMenu->addAction(spinBoxInserter);
  14880. annotationMenu->addAction(freeAnnotationInserter);
  14881. connect(basicButtonInserter, SIGNAL(triggered(QString, QString)),
  14882. this, SLOT(insertChildNode(QString, QString)));
  14883. connect(countingButtonInserter, SIGNAL(triggered(QString, QString)),
  14884. this, SLOT(insertChildNode(QString, QString)));
  14885. connect(spinBoxInserter, SIGNAL(triggered(QString, QString)),
  14886. this, SLOT(insertChildNode(QString, QString)));
  14887. connect(freeAnnotationInserter, SIGNAL(triggered(QString, QString)),
  14888. this, SLOT(insertChildNode(QString, QString)));
  14889. @<Add annotation control node inserters@>@;
  14890. addAnnotationControlButton->setMenu(annotationMenu);
  14891. layout->addWidget(addAnnotationControlButton);
  14892. QPushButton *timersButton = new QPushButton(tr("Extra Timers"));
  14893. QMenu *timersMenu = new QMenu;
  14894. NodeInserter *coolingTimerInserter = new NodeInserter(tr("Cooling Timer"), tr("Cooling Timer"), "coolingtimer");
  14895. NodeInserter *rangeTimerInserter = new NodeInserter(tr("Range Timer"), tr("Range Timer"), "rangetimer");
  14896. NodeInserter *multirangeTimerInserter = new NodeInserter(tr("Multi-Range Timer"), tr("Multi-Range Timer"), "multirangetimer");
  14897. timersMenu->addAction(coolingTimerInserter);
  14898. timersMenu->addAction(rangeTimerInserter);
  14899. timersMenu->addAction(multirangeTimerInserter);
  14900. connect(coolingTimerInserter, SIGNAL(triggered(QString, QString)),
  14901. this, SLOT(insertChildNode(QString, QString)));
  14902. connect(rangeTimerInserter, SIGNAL(triggered(QString, QString)),
  14903. this, SLOT(insertChildNode(QString, QString)));
  14904. connect(multirangeTimerInserter, SIGNAL(triggered(QString, QString)),
  14905. this, SLOT(insertChildNode(QString, QString)));
  14906. timersButton->setMenu(timersMenu);
  14907. layout->addWidget(timersButton);
  14908. QPushButton *advancedButton = new QPushButton(tr("Advanced Features"));
  14909. QMenu *advancedMenu = new QMenu;
  14910. NodeInserter *linearsplineinserter = new NodeInserter(tr("Linear Spline Interpolated Series"), tr("Linear Spline Interpolated Series"), "linearspline");
  14911. advancedMenu->addAction(linearsplineinserter);
  14912. NodeInserter *translationinserter = new NodeInserter(tr("Profile Translation"), tr("Profile Translation"), "translation");
  14913. advancedMenu->addAction(translationinserter);
  14914. connect(linearsplineinserter, SIGNAL(triggered(QString, QString)), this, SLOT(insertChildNode(QString, QString)));
  14915. connect(translationinserter, SIGNAL(triggered(QString, QString)), this, SLOT(insertChildNode(QString, QString)));
  14916. @<Add node inserters to advanced features menu@>@;
  14917. advancedButton->setMenu(advancedMenu);
  14918. layout->addWidget(advancedButton);
  14919. QHBoxLayout *idLayout = new QHBoxLayout;
  14920. QLabel *idLabel = new QLabel(tr("Machine ID for database:"));
  14921. idLayout->addWidget(idLabel);
  14922. QSpinBox *id = new QSpinBox;
  14923. idLayout->addWidget(id);
  14924. idLayout->addStretch();
  14925. layout->addLayout(idLayout);
  14926. QHBoxLayout *capacityLayout = new QHBoxLayout;
  14927. QCheckBox *capacityCheckEnabled = new QCheckBox(tr("Maximum batch size:"));
  14928. QDoubleSpinBox *capacity = new QDoubleSpinBox;
  14929. capacity->setMinimum(0.0);
  14930. capacity->setDecimals(3);
  14931. capacity->setMaximum(999999.999);
  14932. QComboBox *capacityUnit = new QComboBox;
  14933. capacityUnit->addItem("g");
  14934. capacityUnit->addItem("Kg");
  14935. capacityUnit->addItem("oz");
  14936. capacityUnit->addItem("Lb");
  14937. capacityUnit->setCurrentIndex(3);
  14938. capacityLayout->addWidget(capacityCheckEnabled);
  14939. capacityLayout->addWidget(capacity);
  14940. capacityLayout->addWidget(capacityUnit);
  14941. capacityLayout->addStretch();
  14942. layout->addLayout(capacityLayout);
  14943. layout->addStretch();
  14944. @<Get device configuration data for current node@>@;
  14945. for(int i = 0; i < configData.size(); i++)
  14946. {
  14947. node = configData.at(i).toElement();
  14948. if(node.attribute("name") == "databaseid")
  14949. {
  14950. id->setValue(node.attribute("value").toInt());
  14951. }
  14952. else if(node.attribute("name") == "checkcapacity")
  14953. {
  14954. capacityCheckEnabled->setChecked(node.attribute("value") == "true");
  14955. }
  14956. else if(node.attribute("name") == "capacity")
  14957. {
  14958. capacity->setValue(node.attribute("value").toDouble());
  14959. }
  14960. else if(node.attribute("name") == "capacityunit")
  14961. {
  14962. capacityUnit->setCurrentIndex(capacityUnit->findText(node.attribute("value")));
  14963. }
  14964. }
  14965. updateRoasterId(id->value());
  14966. connect(id, SIGNAL(valueChanged(int)), this, SLOT(updateRoasterId(int)));
  14967. connect(capacityCheckEnabled, SIGNAL(stateChanged(int)), this, SLOT(updateCapacityCheck(int)));
  14968. connect(capacity, SIGNAL(valueChanged(QString)), this, SLOT(updateCapacity(QString)));
  14969. connect(capacityUnit, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateCapacityUnit(QString)));
  14970. setLayout(layout);
  14971. }
  14972. @ Iterating over the configuration data associated with the current node is
  14973. required in nearly every configuration widget. The specifics of the loop
  14974. vary, but there is likely a better way to generalize that. Until then,
  14975. obtaining a |QDomNodeList| with the required data to iterate over has been
  14976. split off as its own chunk to reduce the risk of errors associated with typing
  14977. the same code many times.
  14978. @<Get device configuration data for current node@>=
  14979. QDomElement referenceElement =
  14980. model->referenceElement(model->data(index, Qt::UserRole).toString());
  14981. QDomNodeList configData = referenceElement.elementsByTagName("attribute");
  14982. QDomElement node;
  14983. @ We need to propagate changes to the configuration fields to the device
  14984. configuration document. The |updateAttribute()| method in the base class
  14985. makes this trivial.
  14986. @<RoasterConfWidget implementation@>=
  14987. void RoasterConfWidget::updateRoasterId(int id)
  14988. {
  14989. updateAttribute("databaseid", QString("%1").arg(id));
  14990. }
  14991. void RoasterConfWidget::updateCapacityCheck(int value)
  14992. {
  14993. updateAttribute("checkcapacity", value == Qt::Checked ? "true" : "false");
  14994. }
  14995. void RoasterConfWidget::updateCapacity(const QString &value)
  14996. {
  14997. updateAttribute("capacity", value);
  14998. }
  14999. void RoasterConfWidget::updateCapacityUnit(const QString &value)
  15000. {
  15001. updateAttribute("capacityunit", value);
  15002. }
  15003. @ Finally we must register the configuration widget so that it can be
  15004. instantiated at the appropriate time.
  15005. @<Register device configuration widgets@>=
  15006. app.registerDeviceConfigurationWidget("roaster", RoasterConfWidget::staticMetaObject);
  15007. @** Configuration for NI-DAQmx Base devices.
  15008. \noindent The primary concern in supporting hardware that communicates through
  15009. NI-DAQmx Base is in configurations using a single NI USB 9211 (for NI-DAQmx
  15010. Base 2.x) or NI USB 9211A (for NI-DAQmx Base 3.x), but if it is reasonable to
  15011. do so I'@q'@>d like to later add support for multiple device configurations and
  15012. limited support for other devices including the ability to use devices with
  15013. voltage inputs for non-temperature measurement data. The top priority, however,
  15014. is in continuing to support hardware that people are already using with Typica.
  15015. In order to more easily implement these future plans, device configuration is
  15016. handled with three configuration tiers. The top level configuration node
  15017. indicates that we are using NI-DAQmx Base. Here we can add a child node
  15018. representing either a NI USB 9211 or NI USB 9211A. From a configuration
  15019. perspective these are identical with the default node name as the only
  15020. difference. From the device configuration we can specify the device identifier
  15021. and add channels to the device. In the channel nodes we specify the
  15022. thermocouple type.
  15023. @<Class declarations@>=
  15024. class NiDaqMxBaseDriverConfWidget : public BasicDeviceConfigurationWidget
  15025. {
  15026. @[Q_OBJECT@]@;
  15027. public:@/
  15028. @[Q_INVOKABLE@]@, NiDaqMxBaseDriverConfWidget(DeviceTreeModel *model,@|
  15029. const QModelIndex &index);
  15030. };
  15031. @ There is very little to configure here so there isn'@q'@>t much for the
  15032. constructor to do. We do need to keep a reference to the node we are
  15033. configuring so that child nodes can later be added. At present, no real
  15034. configuration data other than the existence of the node is required so
  15035. there is no need to read any configuration data here.
  15036. @<NiDaqMxBaseDriverConfWidget implementation@>=
  15037. NiDaqMxBaseDriverConfWidget::NiDaqMxBaseDriverConfWidget(
  15038. DeviceTreeModel *model, const QModelIndex &index) :
  15039. BasicDeviceConfigurationWidget(model, index)
  15040. {
  15041. QHBoxLayout *layout = new QHBoxLayout;
  15042. QToolButton *addDeviceButton = new QToolButton;
  15043. addDeviceButton->setText(tr("Add Device"));
  15044. NodeInserter *add9211 = new NodeInserter("NI USB 9211", "NI USB 9211",
  15045. "nidaqmxbase9211series");
  15046. NodeInserter *add9211a = new NodeInserter("NI USB 9211A", "NI USB 9211A",
  15047. "nidaqmxbase9211series");
  15048. connect(add9211, SIGNAL(triggered(QString, QString)),
  15049. this, SLOT(insertChildNode(QString, QString)));
  15050. connect(add9211a, SIGNAL(triggered(QString, QString)),
  15051. this, SLOT(insertChildNode(QString, QString)));
  15052. QMenu *deviceMenu = new QMenu;
  15053. deviceMenu->addAction(add9211);
  15054. deviceMenu->addAction(add9211a);
  15055. addDeviceButton->setMenu(deviceMenu);
  15056. addDeviceButton->setPopupMode(QToolButton::InstantPopup);
  15057. layout->addWidget(addDeviceButton);
  15058. setLayout(layout);
  15059. }
  15060. @ Both the NI USB 9211 and NI USB 9211A are identical from a configuration
  15061. perspective. The only difference is the version of NI-DAQmx Base required for
  15062. use. As the API does not provide a way of determining which version is
  15063. installed, ensuring that the appropriate software is installed without
  15064. conflicts is left as an exercise for the person attempting to use \pn{}.
  15065. @<Class declarations@>=
  15066. class NiDaqMxBase9211ConfWidget : public BasicDeviceConfigurationWidget
  15067. {
  15068. Q_OBJECT
  15069. public:
  15070. Q_INVOKABLE NiDaqMxBase9211ConfWidget(DeviceTreeModel *device,
  15071. const QModelIndex &index);
  15072. private slots:
  15073. void addChannel();
  15074. void updateDeviceId(const QString &newId);
  15075. };
  15076. @ There are two controls required in a configuration widget for this device.
  15077. The first is the device identifier (for example, "Dev1"), the second is a
  15078. button for adding channels to the device. On a generic device we would also
  15079. need to set the clock rate, but with this hardware it is possible to determine
  15080. the maximum clock rate from the channels defined.
  15081. @<NiDaqMxBase9211ConfWidget implementation@>=
  15082. NiDaqMxBase9211ConfWidget::NiDaqMxBase9211ConfWidget(DeviceTreeModel *model,
  15083. const QModelIndex &index)
  15084. : BasicDeviceConfigurationWidget(model, index)
  15085. {
  15086. QVBoxLayout *layout = new QVBoxLayout;
  15087. QHBoxLayout *deviceIdLayout = new QHBoxLayout;
  15088. QLabel *label = new QLabel(tr("Device ID:"));
  15089. QLineEdit *deviceId = new QLineEdit;
  15090. deviceIdLayout->addWidget(label);
  15091. deviceIdLayout->addWidget(deviceId);
  15092. QPushButton *addChannelButton = new QPushButton(tr("Add Channel"));
  15093. layout->addLayout(deviceIdLayout);
  15094. layout->addWidget(addChannelButton);
  15095. @<Get device configuration data for current node@>@;
  15096. for(int i = 0; i < configData.size(); i++)
  15097. {
  15098. node = configData.at(i).toElement();
  15099. if(node.attribute("name") == "deviceID")
  15100. {
  15101. deviceId->setText(node.attribute("value", "Dev1"));
  15102. break;
  15103. }
  15104. }
  15105. updateDeviceId(deviceId->text());
  15106. connect(addChannelButton, SIGNAL(clicked()),
  15107. this, SLOT(addChannel()));
  15108. connect(deviceId, SIGNAL(textEdited(QString)),
  15109. this, SLOT(updateDeviceId(QString)));
  15110. setLayout(layout);
  15111. }
  15112. @ Updating the attribute tag under the reference element associated with the
  15113. current node is handled in the base class so we just need to pass in the
  15114. appropriate name value pair.
  15115. @<NiDaqMxBase9211ConfWidget implementation@>=
  15116. void NiDaqMxBase9211ConfWidget::updateDeviceId(const QString &newId)
  15117. {
  15118. updateAttribute("deviceID", newId);
  15119. }
  15120. @ Adding channels is just like adding any other sort of node.
  15121. @<NiDaqMxBase9211ConfWidget implementation@>=
  15122. void NiDaqMxBase9211ConfWidget::addChannel()
  15123. {
  15124. insertChildNode(tr("Thermocouple channel"), "ni9211seriestc");
  15125. }
  15126. @ Finally, we need a configuration widget to handle our thermocouple channels.
  15127. Ordinarily we would need three pieces of information for each channel. First
  15128. there is the thermocouple channel. Previously this was left implied by the
  15129. order of requests for a new channel, but more flexible configuration options
  15130. become possible with a more explicit specification. Since this widget is device
  15131. specific, all of the options can be easily enumerated to match markings on the
  15132. device. Next is the thermocouple type. Many options are supported, but I would
  15133. like to ensure that the most commonly used choices are listed first. The other
  15134. piece of information that DAQmx or DAQmx Base require is the measurement unit.
  15135. As all of Typica'@q'@>s internal operations are in Fahrenheit there is no need to
  15136. make this configurable so long as everything else that can display temperature
  15137. measurements can perform the appropriate conversions.
  15138. Note that as there are no configuration differences between the various
  15139. device combinations using an NI 9211 module with regard to thermocouple channel
  15140. configuration, we can use this widget with all device combinations that make
  15141. use of such a module.
  15142. @<Class declarations@>=
  15143. class Ni9211TcConfWidget : public BasicDeviceConfigurationWidget
  15144. {@/
  15145. @[Q_OBJECT@]@;
  15146. public:@/
  15147. Q_INVOKABLE@,@, Ni9211TcConfWidget(DeviceTreeModel *device,
  15148. const QModelIndex &index);
  15149. @[private slots@]:@/
  15150. void updateThermocoupleType(const QString &type);
  15151. void updateColumnName(const QString &name);
  15152. void updateHidden(bool hidden);
  15153. };
  15154. @ This follows the same pattern of previous device configuration widgets. The
  15155. constructor provides the required configuration controls and slot methods
  15156. catch configuration changes and update the underlying XML document
  15157. appropriately.
  15158. @<Ni9211TcConfWidget implementation@>=
  15159. Ni9211TcConfWidget::Ni9211TcConfWidget(DeviceTreeModel *model,
  15160. const QModelIndex &index) :
  15161. BasicDeviceConfigurationWidget(model, index)
  15162. {
  15163. QFormLayout *layout = new QFormLayout;
  15164. QLineEdit *columnName = new QLineEdit;
  15165. layout->addRow(tr("Column Name:"), columnName);
  15166. QComboBox *typeSelector = new QComboBox;
  15167. typeSelector->addItem("J");
  15168. typeSelector->addItem("K");
  15169. typeSelector->addItem("T");
  15170. typeSelector->addItem("B");
  15171. typeSelector->addItem("E");
  15172. typeSelector->addItem("N");
  15173. typeSelector->addItem("R");
  15174. typeSelector->addItem("S");
  15175. layout->addRow(tr("Thermocouple Type:"), typeSelector);
  15176. QCheckBox *hideSeries = new QCheckBox("Hide this channel");
  15177. layout->addRow(hideSeries);
  15178. setLayout(layout);
  15179. @<Get device configuration data for current node@>@;
  15180. for(int i = 0; i < configData.size(); i++)
  15181. {
  15182. node = configData.at(i).toElement();
  15183. if(node.attribute("name") == "type")
  15184. {
  15185. typeSelector->setCurrentIndex(
  15186. typeSelector->findText(node.attribute("value")));
  15187. }
  15188. else if(node.attribute("name") == "columnname")
  15189. {
  15190. columnName->setText(node.attribute("value"));
  15191. }
  15192. else if(node.attribute("name") == "hidden")
  15193. {
  15194. hideSeries->setChecked(node.attribute("value") == "true");
  15195. }
  15196. }
  15197. updateThermocoupleType(typeSelector->currentText());
  15198. updateColumnName(columnName->text());
  15199. updateHidden(hideSeries->isChecked());
  15200. connect(typeSelector, SIGNAL(currentIndexChanged(QString)),
  15201. this, SLOT(updateThermocoupleType(QString)));
  15202. connect(columnName, SIGNAL(textEdited(QString)), this, SLOT(updateColumnName(QString)));
  15203. connect(hideSeries, SIGNAL(toggled(bool)), this, SLOT(updateHidden(bool)));
  15204. }
  15205. @ Two slots are used to pass configuration changes back to the underlying XML
  15206. representation.
  15207. @<Ni9211TcConfWidget implementation@>=
  15208. void Ni9211TcConfWidget::updateThermocoupleType(const QString &type)
  15209. {
  15210. updateAttribute("type", type);
  15211. }
  15212. void Ni9211TcConfWidget::updateColumnName(const QString &name)
  15213. {
  15214. updateAttribute("columnname", name);
  15215. }
  15216. void Ni9211TcConfWidget::updateHidden(bool hidden)
  15217. {
  15218. updateAttribute("hidden", hidden ? "true" : "false");
  15219. }
  15220. @ These three widgets need to be registered so the configuration widget can
  15221. instantiate them when the nodes are selected.
  15222. @<Register device configuration widgets@>=
  15223. app.registerDeviceConfigurationWidget("nidaqmxbase",
  15224. NiDaqMxBaseDriverConfWidget::staticMetaObject);
  15225. app.registerDeviceConfigurationWidget("nidaqmxbase9211series",
  15226. NiDaqMxBase9211ConfWidget::staticMetaObject);
  15227. app.registerDeviceConfigurationWidget("ni9211seriestc",
  15228. Ni9211TcConfWidget::staticMetaObject);
  15229. @ Furthermore, we should create the NodeInserter objects for adding top level
  15230. nodes to the configuration. Preferably we would only allow top level nodes to
  15231. be inserted when all prerequisite software is available.
  15232. @<Register top level device configuration nodes@>=
  15233. NodeInserter *inserter = new NodeInserter(tr("NI DAQmx Base Device"),
  15234. tr("NI DAQmx Base"),
  15235. "nidaqmxbase", NULL);
  15236. topLevelNodeInserters.append(inserter);
  15237. @** Configuration of NI-DAQmx devices.
  15238. \noindent The other main class of hardware currently supported in Typica is a
  15239. small set of devices that require NI-DAQmx. This includes a few combinations of
  15240. the NI 9211 in different carriers and the NI USB TC01. Additional hardware may
  15241. be added to this set in the future.
  15242. The approach here is very similar to the approach used to configure NI-DAQmx
  15243. Base devices, starting with a widget for adding child device nodes.
  15244. @<Class declarations@>=
  15245. class NiDaqMxDriverConfWidget : public BasicDeviceConfigurationWidget
  15246. {
  15247. @[Q_OBJECT@]@;
  15248. public:
  15249. @[Q_INVOKABLE@]@, NiDaqMxDriverConfWidget(DeviceTreeModel *model,
  15250. const QModelIndex &index);
  15251. };
  15252. @ Under our driver node we want to have the ability to insert device specific
  15253. child nodes.
  15254. @<NiDaqMxDriverConfWidget implementation@>=
  15255. NiDaqMxDriverConfWidget::NiDaqMxDriverConfWidget(DeviceTreeModel *model,
  15256. const QModelIndex &index)
  15257. : BasicDeviceConfigurationWidget(model, index)
  15258. {
  15259. QHBoxLayout *layout = new QHBoxLayout;
  15260. QToolButton *addDeviceButton = new QToolButton;
  15261. addDeviceButton->setText(tr("Add Device"));
  15262. NodeInserter *add9211a = new NodeInserter("NI USB 9211A", "NI USB 9211A",
  15263. "nidaqmx9211series");
  15264. NodeInserter *addtc01 = new NodeInserter("NI USB TC01", "NI USB TC01",
  15265. "nidaqmxtc01");
  15266. connect(add9211a, SIGNAL(triggered(QString, QString)),
  15267. this, SLOT(insertChildNode(QString, QString)));
  15268. connect(addtc01, SIGNAL(triggered(QString, QString)),
  15269. this, SLOT(insertChildNode(QString, QString)));
  15270. QMenu *deviceMenu = new QMenu;
  15271. deviceMenu->addAction(add9211a);
  15272. deviceMenu->addAction(addtc01);
  15273. addDeviceButton->setMenu(deviceMenu);
  15274. addDeviceButton->setPopupMode(QToolButton::InstantPopup);
  15275. layout->addWidget(addDeviceButton);
  15276. setLayout(layout);
  15277. }
  15278. @ Devices based on the 9211 module are essentially the same aside from device
  15279. naming convention. Configuring these is very similar to configuring similar
  15280. devices when using NI-DAQmx Base.
  15281. @<Class declarations@>=
  15282. class NiDaqMx9211ConfWidget : public BasicDeviceConfigurationWidget
  15283. {
  15284. @[Q_OBJECT@]@;
  15285. public:@/
  15286. @[Q_INVOKABLE@]@, NiDaqMx9211ConfWidget(DeviceTreeModel *model,
  15287. const QModelIndex &index);
  15288. @[private slots@]:@/
  15289. void addChannel();
  15290. void updateDeviceId(const QString &newId);
  15291. };
  15292. @ Implementation is essentially identical to the NI-DAQmx Base class. It is
  15293. likely that there will be differences if this is ever extended to support
  15294. automatic detection of connected hardware. While NI-DAQmx Base does not
  15295. provide a way to query device identifiers, it uses a consistent naming scheme
  15296. by which device identifiers can be discovered. While NI-DAQmx lacks the same
  15297. consistency in device identifiers, it does provide a way to query that
  15298. information.
  15299. @<NiDaqMx9211ConfWidget implementation@>=
  15300. NiDaqMx9211ConfWidget::NiDaqMx9211ConfWidget(DeviceTreeModel *model,
  15301. const QModelIndex &index)
  15302. : BasicDeviceConfigurationWidget(model, index)
  15303. {
  15304. QVBoxLayout *layout = new QVBoxLayout;
  15305. QHBoxLayout *deviceIdLayout = new QHBoxLayout;
  15306. QLabel *label = new QLabel(tr("Device ID:"));
  15307. QLineEdit *deviceId = new QLineEdit;
  15308. deviceIdLayout->addWidget(label);
  15309. deviceIdLayout->addWidget(deviceId);
  15310. QPushButton *addChannelButton = new QPushButton(tr("Add Channel"));
  15311. layout->addLayout(deviceIdLayout);
  15312. layout->addWidget(addChannelButton);
  15313. @<Get device configuration data for current node@>@;
  15314. for(int i = 0; i < configData.size(); i++)
  15315. {
  15316. node = configData.at(i).toElement();
  15317. if(node.attribute("name") == "deviceID")
  15318. {
  15319. deviceId->setText(node.attribute("value","Dev1"));
  15320. break;
  15321. }
  15322. }
  15323. updateDeviceId(deviceId->text());
  15324. connect(addChannelButton, SIGNAL(clicked()), this, SLOT(addChannel()));
  15325. connect(deviceId, SIGNAL(textEdited(QString)),
  15326. this, SLOT(updateDeviceId(QString)));
  15327. setLayout(layout);
  15328. }
  15329. void NiDaqMx9211ConfWidget::updateDeviceId(const QString &newId)
  15330. {
  15331. updateAttribute("deviceID", newId);
  15332. }
  15333. void NiDaqMx9211ConfWidget::addChannel()
  15334. {
  15335. insertChildNode(tr("Thermocouple channel"), "ni9211seriestc");
  15336. }
  15337. @ There is no need to create a configuration widget specific to the 9211 module
  15338. used in NI-DAQmx. The widget already used for NI-DAQmx Base can be used without
  15339. modification.
  15340. Configuring Typica for use with the NI USB-TC01 can be slightly simplified as
  15341. the device only has a single thermocouple channel. We can configure this without
  15342. requiring another node.
  15343. @<Class declarations@>=
  15344. class NiDaqMxTc01ConfWidget : public BasicDeviceConfigurationWidget
  15345. {
  15346. @[Q_OBJECT@]@;
  15347. public:@/
  15348. @[Q_INVOKABLE@]@, NiDaqMxTc01ConfWidget(DeviceTreeModel *model,
  15349. const QModelIndex &index);
  15350. @[private slots@]:@/
  15351. void updateDeviceId(const QString &newId);
  15352. void updateThermocoupleType(const QString &type);
  15353. void updateColumnName(const QString &name);
  15354. void updateHidden(bool hidden);
  15355. };
  15356. @ The implementation is similar to the other configuration widgets.
  15357. @<NiDaqMxTc01ConfWidget implementation@>=
  15358. NiDaqMxTc01ConfWidget::NiDaqMxTc01ConfWidget(DeviceTreeModel *model,
  15359. const QModelIndex &index)
  15360. : BasicDeviceConfigurationWidget(model, index)
  15361. {
  15362. QFormLayout *layout = new QFormLayout;
  15363. QLineEdit *deviceId = new QLineEdit;
  15364. layout->addRow(tr("Device ID:"), deviceId);
  15365. QLineEdit *columnName = new QLineEdit;
  15366. layout->addRow(tr("Column Name:"), columnName);
  15367. QComboBox *typeSelector = new QComboBox;
  15368. typeSelector->addItem("J");
  15369. typeSelector->addItem("K");
  15370. typeSelector->addItem("T");
  15371. typeSelector->addItem("B");
  15372. typeSelector->addItem("E");
  15373. typeSelector->addItem("N");
  15374. typeSelector->addItem("R");
  15375. typeSelector->addItem("S");
  15376. layout->addRow(tr("Thermocouple Type:"), typeSelector);
  15377. QCheckBox *hideSeries = new QCheckBox(tr("Hide this channel"));
  15378. layout->addRow(hideSeries);
  15379. @<Get device configuration data for current node@>@;
  15380. for(int i = 0; i < configData.size(); i++)
  15381. {
  15382. node = configData.at(i).toElement();
  15383. if(node.attribute("name") == "deviceID")
  15384. {
  15385. deviceId->setText(node.attribute("value"));
  15386. }
  15387. else if(node.attribute("name") == "type")
  15388. {
  15389. typeSelector->setCurrentIndex(typeSelector->findText(node.attribute("value")));
  15390. }
  15391. else if(node.attribute("name") == "columnname")
  15392. {
  15393. columnName->setText(node.attribute("value"));
  15394. }
  15395. else if(node.attribute("name") == "hidden")
  15396. {
  15397. hideSeries->setChecked(node.attribute("value") == "true");
  15398. }
  15399. }
  15400. updateDeviceId(deviceId->text());
  15401. updateThermocoupleType(typeSelector->currentText());
  15402. updateColumnName(columnName->text());
  15403. updateHidden(hideSeries->isChecked());
  15404. connect(deviceId, SIGNAL(textEdited(QString)), this, SLOT(updateDeviceId(QString)));
  15405. connect(typeSelector, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateThermocoupleType(QString)));
  15406. connect(columnName, SIGNAL(textEdited(QString)), this, SLOT(updateColumnName(QString)));
  15407. setLayout(layout);
  15408. connect(hideSeries, SIGNAL(toggled(bool)), this, SLOT(updateHidden(bool)));
  15409. }
  15410. void NiDaqMxTc01ConfWidget::updateDeviceId(const QString &newId)
  15411. {
  15412. updateAttribute("deviceID", newId);
  15413. }
  15414. void NiDaqMxTc01ConfWidget::updateThermocoupleType(const QString &type)
  15415. {
  15416. updateAttribute("type", type);
  15417. }
  15418. void NiDaqMxTc01ConfWidget::updateColumnName(const QString &name)
  15419. {
  15420. updateAttribute("columnname", name);
  15421. }
  15422. void NiDaqMxTc01ConfWidget::updateHidden(bool hidden)
  15423. {
  15424. updateAttribute("hidden", hidden ? "true" : "false");
  15425. }
  15426. @ These configuration widgets need to be registered so they can be instantiated
  15427. in response to node selections.
  15428. @<Register device configuration widgets@>=
  15429. app.registerDeviceConfigurationWidget("nidaqmx", NiDaqMxDriverConfWidget::staticMetaObject);
  15430. app.registerDeviceConfigurationWidget("nidaqmx9211series", NiDaqMx9211ConfWidget::staticMetaObject);
  15431. app.registerDeviceConfigurationWidget("nidaqmxtc01", NiDaqMxTc01ConfWidget::staticMetaObject);
  15432. @ A |NodeInserter| for the driver configuration widget is also needed. Note that
  15433. at present NI DAQmx is only available on Windows so we do not bother to show
  15434. the option on other platforms. It would be generally preferable to replace this
  15435. with a check at runtime to determine if the required library exists. That could
  15436. be done with anything that requires third party installed code, leaving by
  15437. default only those options which have no external dependencies.
  15438. @<Register top level device configuration nodes@>=
  15439. #ifdef Q_OS_WIN32
  15440. inserter = new NodeInserter(tr("NI DAQmx Device"), tr("NI DAQmx"), "nidaqmx", NULL);
  15441. topLevelNodeInserters.append(inserter);
  15442. #endif
  15443. @** Configuration of Serial Port Devices.
  15444. \noindent It is possible to communicate with a number of devices through a
  15445. serial port. To do this, the appropriate settings for opening the port are
  15446. required and the communications protocol understood by the device must be
  15447. known. Serial port communications are provided by QextSerialPort. That
  15448. project was released under the MIT license.\nfnote{See the license text for
  15449. more information.} Additional headers are required.
  15450. @<Header files to include@>=
  15451. #include "qextserialport.h"
  15452. #include "qextserialenumerator.h"
  15453. @ Some custom widgets are provided which allow selecting the relevant
  15454. connection options from combo boxes. First there is a widget for selecting
  15455. the desired serial port. The drop down is pre-populated with any serial ports
  15456. that could be automatically detected, but the field can also be edited to
  15457. other values as may be required if the hardware is not connected during
  15458. configuration.
  15459. @<Class declarations@>=
  15460. class PortSelector : public QComboBox
  15461. {
  15462. Q_OBJECT
  15463. public:
  15464. PortSelector(QWidget *parent = NULL);
  15465. private slots:
  15466. void addDevice(QextPortInfo port);
  15467. private:
  15468. QextSerialEnumerator *lister;
  15469. };
  15470. @ The implementation is trivial.
  15471. @<PortSelector implementation@>=
  15472. PortSelector::PortSelector(QWidget *parent) : QComboBox(parent),
  15473. lister(new QextSerialEnumerator)
  15474. {
  15475. QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
  15476. QextPortInfo port;
  15477. foreach(port, ports)
  15478. {
  15479. #ifdef Q_OS_WIN32
  15480. addItem(port.portName);
  15481. #else
  15482. addItem(port.physName);
  15483. #endif
  15484. }
  15485. lister->setUpNotifications();
  15486. connect(lister, SIGNAL(deviceDiscovered(QextPortInfo)),
  15487. this, SLOT(addDevice(QextPortInfo)));
  15488. setEditable(true);
  15489. }
  15490. void PortSelector::addDevice(QextPortInfo port)
  15491. {
  15492. addItem(port.portName);
  15493. }
  15494. @ Next is a widget which allows selecting the baud rate. Only rates supported
  15495. by the current operating system are available to select.
  15496. A later version of QextSerialPort than is used by \pn{} provides a helper
  15497. class which can be used more conveniently to create this sort of control. As
  15498. this is not yet available to \pn{}, we instead copy the |enum| specifying
  15499. the appropriate values into the class and use Qt'@q'@>s meta-object system to
  15500. populate the combo box based on the values in that |enum|.
  15501. @<Class declarations@>=
  15502. class BaudSelector : public QComboBox
  15503. {
  15504. Q_OBJECT
  15505. Q_ENUMS(BaudRateType)
  15506. public:
  15507. BaudSelector(QWidget *parent = NULL);
  15508. enum BaudRateType
  15509. {
  15510. #if defined(Q_OS_UNIX) || defined(qdoc)
  15511. BAUD50 = 50, //POSIX ONLY
  15512. BAUD75 = 75, //POSIX ONLY
  15513. BAUD134 = 134, //POSIX ONLY
  15514. BAUD150 = 150, //POSIX ONLY
  15515. BAUD200 = 200, //POSIX ONLY
  15516. BAUD1800 = 1800, //POSIX ONLY
  15517. #if defined(B76800) || defined(qdoc)
  15518. BAUD76800 = 76800, //POSIX ONLY
  15519. #endif
  15520. #if (defined(B230400) && defined(B4000000)) || defined(qdoc)
  15521. BAUD230400 = 230400, //POSIX ONLY
  15522. BAUD460800 = 460800, //POSIX ONLY
  15523. BAUD500000 = 500000, //POSIX ONLY
  15524. BAUD576000 = 576000, //POSIX ONLY
  15525. BAUD921600 = 921600, //POSIX ONLY
  15526. BAUD1000000 = 1000000, //POSIX ONLY
  15527. BAUD1152000 = 1152000, //POSIX ONLY
  15528. BAUD1500000 = 1500000, //POSIX ONLY
  15529. BAUD2000000 = 2000000, //POSIX ONLY
  15530. BAUD2500000 = 2500000, //POSIX ONLY
  15531. BAUD3000000 = 3000000, //POSIX ONLY
  15532. BAUD3500000 = 3500000, //POSIX ONLY
  15533. BAUD4000000 = 4000000, //POSIX ONLY
  15534. #endif
  15535. #endif
  15536. #if defined(Q_OS_WIN) || defined(qdoc)
  15537. BAUD14400 = 14400, //WINDOWS ONLY
  15538. BAUD56000 = 56000, //WINDOWS ONLY
  15539. BAUD128000 = 128000, //WINDOWS ONLY
  15540. BAUD256000 = 256000, //WINDOWS ONLY
  15541. #endif
  15542. BAUD110 = 110,
  15543. BAUD300 = 300,
  15544. BAUD600 = 600,
  15545. BAUD1200 = 1200,
  15546. BAUD2400 = 2400,
  15547. BAUD4800 = 4800,
  15548. BAUD9600 = 9600,
  15549. BAUD19200 = 19200,
  15550. BAUD38400 = 38400,
  15551. BAUD57600 = 57600,
  15552. BAUD115200 = 115200
  15553. };
  15554. };
  15555. @ As the |enum| values are identical to the baud rates represented, we only
  15556. the numeric values, ignoring the names which are rather ugly.
  15557. @<BaudSelector implementation@>=
  15558. BaudSelector::BaudSelector(QWidget *parent) : QComboBox(parent)
  15559. {
  15560. QMetaObject meta = BaudSelector::staticMetaObject;
  15561. QMetaEnum type = meta.enumerator(meta.indexOfEnumerator("BaudRateType"));
  15562. for(int i = 0; i < type.keyCount(); i++)
  15563. {
  15564. addItem(QString("%1").arg(type.value(i)));
  15565. }
  15566. }
  15567. @ This same technique is used in a widget for selecting parity.
  15568. @<Class declarations@>=
  15569. class ParitySelector : public QComboBox
  15570. {
  15571. Q_OBJECT
  15572. Q_ENUMS(ParityType)
  15573. public:
  15574. ParitySelector(QWidget *parent = NULL);
  15575. enum ParityType
  15576. {
  15577. PAR_NONE,
  15578. PAR_ODD,
  15579. PAR_EVEN,
  15580. #if defined(Q_OS_WIN) || defined(qdoc)
  15581. PAR_MARK, //WINDOWS ONLY
  15582. #endif
  15583. PAR_SPACE
  15584. };
  15585. };
  15586. @ Implementation is similar to |BaudSelector| but as the values have no
  15587. apparent relation to what is represented we present the value names, placing
  15588. the corresponding value in the user data space associated with each entry.
  15589. The names here are ugly and not amenable to localization so this approach
  15590. should be reconsidered later.
  15591. @<ParitySelector implementation@>=
  15592. ParitySelector::ParitySelector(QWidget *parent) : QComboBox(parent)
  15593. {
  15594. QMetaObject meta = ParitySelector::staticMetaObject;
  15595. QMetaEnum type = meta.enumerator(meta.indexOfEnumerator("ParityType"));
  15596. for(int i = 0; i < type.keyCount(); i++)
  15597. {
  15598. addItem(QString(type.key(i)), QVariant(type.value(i)));
  15599. }
  15600. }
  15601. @ Similarly, we have a widget for selecting a method for flow control.
  15602. @<Class declarations@>=
  15603. class FlowSelector : public QComboBox
  15604. {
  15605. @[Q_OBJECT@]@;
  15606. @[Q_ENUMS(FlowType)@]@;
  15607. public:@/
  15608. FlowSelector(QWidget *parent = NULL);
  15609. enum FlowType
  15610. {
  15611. FLOW_OFF,
  15612. FLOW_HARDWARE,
  15613. FLOW_XONXOFF
  15614. };
  15615. };
  15616. @ Implementation follows the same pattern as in |ParitySelector|.
  15617. @<FlowSelector implementation@>=
  15618. FlowSelector::FlowSelector(QWidget *parent) : QComboBox(parent)
  15619. {
  15620. QMetaObject meta = FlowSelector::staticMetaObject;
  15621. QMetaEnum type = meta.enumerator(meta.indexOfEnumerator("FlowType"));
  15622. for(int i = 0; i < type.keyCount(); i++)
  15623. {
  15624. addItem(QString(type.key(i)), QVariant(type.value(i)));
  15625. }
  15626. }
  15627. @ We assume that the number of data bits will always be 8, though it may be
  15628. useful to later provide a control for selecting this for use with other devices
  15629. where this may not be assumed or for the sake of completion. This only leaves
  15630. specifying the number of stop bits.
  15631. @<Class declarations@>=
  15632. class StopSelector : public QComboBox
  15633. {
  15634. @[Q_OBJECT@]@;
  15635. @[Q_ENUMS(StopBitsType)@]@;
  15636. public:@/
  15637. StopSelector(QWidget *parent = NULL);
  15638. enum StopBitsType
  15639. {
  15640. STOP_1,
  15641. #if defined(Q_OS_WIN) || defined(qdoc)
  15642. STOP_1_5, //WINDOWS ONLY
  15643. #endif
  15644. STOP_2
  15645. };
  15646. };
  15647. @ Implementation should be familiar by now.
  15648. @<StopSelector implementation@>=
  15649. StopSelector::StopSelector(QWidget *parent) : QComboBox(parent)
  15650. {
  15651. QMetaObject meta = StopSelector::staticMetaObject;
  15652. QMetaEnum type = meta.enumerator(meta.indexOfEnumerator("StopBitsType"));
  15653. for(int i = 0; i < type.keyCount(); i++)
  15654. {
  15655. addItem(QString(type.key(i)), QVariant(type.value(i)));
  15656. }
  15657. }
  15658. @** Configuration of Serial Devices Using Modbus RTU.
  15659. \noindent The following sections contain the details of older code related to
  15660. Modbus RTU support. While the various selector widgets are shared with the new
  15661. code, the configuration widgets and device interfaces are being replaced.
  15662. One protocol that is used across a broad class of devices is called
  15663. Modbus RTU. This protocol allows multiple devices to be chained together on a
  15664. two wire bus which can be connected to a single serial port. The communication
  15665. protocol involves a single message which is sent from a master device (in this
  15666. case the computer running Typica) to a slave device (the device we would like
  15667. to obtain information from) which is followed by a response message from the
  15668. slave to the master. After a brief wait the master can then send another
  15669. message to any slave on the bus and this process repeats. Every outgoing
  15670. message provides a station address to identify which slave on the bus should
  15671. respond, a function code to identify which of a broad class of operations has
  15672. been requested, the required data for the function specified, and a cyclic
  15673. redundancy check to validate the message.
  15674. @** A Spin Box with Hexadecimal Representation.
  15675. \noindent Common convention for communications documentation for devices that
  15676. use Modbus RTU is that relative addresses are specified in hexadecimal
  15677. representation. In order to simplify initial device configuration, it would be
  15678. best that input widgets both accept input in base 16 and display values as a
  15679. four digit hexadecimal value.
  15680. @<Class declarations@>=
  15681. class ShortHexSpinBox : public QSpinBox
  15682. {
  15683. @[Q_OBJECT@]@;
  15684. public:@/
  15685. ShortHexSpinBox(QWidget *parent = NULL);
  15686. virtual QValidator::State validate(QString &input, int &pos) const;@/
  15687. protected:@/
  15688. virtual int valueFromText(const QString &text) const;
  15689. virtual QString textFromValue(int value) const;@/
  15690. };
  15691. @ For this we can set some new defaults in the constructor and must override
  15692. three methods.
  15693. @<ShortHexSpinBox implementation@>=
  15694. ShortHexSpinBox::ShortHexSpinBox(QWidget *parent) : QSpinBox(parent)
  15695. {
  15696. setMinimum(0);
  15697. setMaximum(0xFFFF);
  15698. setPrefix("0x");
  15699. setMinimumWidth(65);
  15700. }
  15701. QValidator::State ShortHexSpinBox::validate(QString &input, int &) const
  15702. {
  15703. if(input.size() == 2)
  15704. {
  15705. return QValidator::Intermediate;
  15706. }
  15707. bool okay;
  15708. input.toInt(&okay, 16);
  15709. if(okay)
  15710. {
  15711. return QValidator::Acceptable;
  15712. }
  15713. return QValidator::Invalid;
  15714. }
  15715. int ShortHexSpinBox::valueFromText(const QString &text) const
  15716. {
  15717. return text.toInt(NULL, 16);
  15718. }
  15719. QString ShortHexSpinBox::textFromValue(int value) const
  15720. {
  15721. QString retval;
  15722. retval.setNum(value, 16);
  15723. while(retval.size() < 4)
  15724. {
  15725. retval.prepend("0");
  15726. }
  15727. return retval.toUpper();
  15728. }
  15729. @** Configuration Widgets for Modbus RTU Devices.
  15730. \noindent While the top level configuration widgets seen so far have not had
  15731. any configuration details beyond the ability to add devices under the driver,
  15732. in the case of a serial port with Modbus RTU devices it is reasonable to
  15733. provide the connection details which will be shared by all devices on the bus.
  15734. @<Class declarations@>=
  15735. class ModbusRtuPortConfWidget : public BasicDeviceConfigurationWidget
  15736. {
  15737. @[Q_OBJECT@]@;
  15738. public:
  15739. @[Q_INVOKABLE@]@, ModbusRtuPortConfWidget(DeviceTreeModel *model,
  15740. const QModelIndex &index);
  15741. @[private slots@]:@/
  15742. void updatePort(const QString &newPort);
  15743. void updateBaudRate(const QString &newRate);
  15744. void updateParity(const QString &newParity);
  15745. void updateFlowControl(const QString &newFlow);
  15746. void updateStopBits(const QString &newStopBits);
  15747. };
  15748. @ Aside from the extra information compared with other configuration widgets
  15749. previously described, there is nothing surprising about the implementation.
  15750. @<ModbusRtuPortConfWidget implementation@>=
  15751. ModbusRtuPortConfWidget::ModbusRtuPortConfWidget(DeviceTreeModel *model,
  15752. const QModelIndex &index)
  15753. : BasicDeviceConfigurationWidget(model, index)
  15754. {
  15755. QFormLayout *layout = new QFormLayout;
  15756. QToolButton *addDeviceButton = new QToolButton;
  15757. addDeviceButton->setText(tr("Add Device"));
  15758. NodeInserter *addModbusRtuDevice = new NodeInserter("Modbus RTU Device",
  15759. "Modbus RTU Device",
  15760. "modbusrtudevice");
  15761. connect(addModbusRtuDevice, SIGNAL(triggered(QString, QString)),
  15762. this, SLOT(insertChildNode(QString, QString)));
  15763. QMenu *deviceMenu = new QMenu;
  15764. deviceMenu->addAction(addModbusRtuDevice);
  15765. addDeviceButton->setMenu(deviceMenu);
  15766. addDeviceButton->setPopupMode(QToolButton::InstantPopup);
  15767. layout->addRow(QString(), addDeviceButton);
  15768. PortSelector *port = new PortSelector;
  15769. layout->addRow(tr("Port:"), port);
  15770. connect(port, SIGNAL(currentIndexChanged(QString)),
  15771. this, SLOT(updatePort(QString)));
  15772. connect(port, SIGNAL(editTextChanged(QString)),
  15773. this, SLOT(updatePort(QString)));
  15774. BaudSelector *rate = new BaudSelector;
  15775. layout->addRow(tr("Baud:"), rate);
  15776. connect(rate, SIGNAL(currentIndexChanged(QString)),
  15777. this, SLOT(updateBaudRate(QString)));
  15778. ParitySelector *parity = new ParitySelector;
  15779. layout->addRow(tr("Parity:"), parity);
  15780. connect(parity, SIGNAL(currentIndexChanged(QString)),
  15781. this, SLOT(updateParity(QString)));
  15782. FlowSelector *flow = new FlowSelector;
  15783. layout->addRow(tr("Flow Control:"), flow);
  15784. connect(flow, SIGNAL(currentIndexChanged(QString)),
  15785. this, SLOT(updateFlowControl(QString)));
  15786. StopSelector *stop = new StopSelector;
  15787. layout->addRow(tr("Stop Bits:"), stop);
  15788. connect(stop, SIGNAL(currentIndexChanged(QString)),
  15789. this, SLOT(updateStopBits(QString)));
  15790. @<Get device configuration data for current node@>@;
  15791. for(int i = 0; i < configData.size(); i++)
  15792. {
  15793. node = configData.at(i).toElement();
  15794. if(node.attribute("name") == "port")
  15795. {
  15796. int j = port->findText(node.attribute("value"));
  15797. if(j >= 0)
  15798. {
  15799. port->setCurrentIndex(j);
  15800. }
  15801. else
  15802. {
  15803. port->insertItem(0, node.attribute("value"));
  15804. port->setCurrentIndex(0);
  15805. }
  15806. }
  15807. else if(node.attribute("name") == "baudrate")
  15808. {
  15809. rate->setCurrentIndex(rate->findText(node.attribute("value")));
  15810. }
  15811. else if(node.attribute("name") == "parity")
  15812. {
  15813. parity->setCurrentIndex(parity->findText(node.attribute("value")));
  15814. }
  15815. else if(node.attribute("name") == "flowcontrol")
  15816. {
  15817. flow->setCurrentIndex(flow->findText(node.attribute("value")));
  15818. }
  15819. else if(node.attribute("name") == "stopbits")
  15820. {
  15821. stop->setCurrentIndex(stop->findText(node.attribute("value")));
  15822. }
  15823. }
  15824. updatePort(port->currentText());
  15825. updateBaudRate(rate->currentText());
  15826. updateParity(parity->currentText());
  15827. updateFlowControl(flow->currentText());
  15828. updateStopBits(stop->currentText());
  15829. setLayout(layout);
  15830. }
  15831. void ModbusRtuPortConfWidget::updatePort(const QString &newPort)
  15832. {
  15833. updateAttribute("port", newPort);
  15834. }
  15835. void ModbusRtuPortConfWidget::updateBaudRate(const QString &newRate)
  15836. {
  15837. updateAttribute("baudrate", newRate);
  15838. }
  15839. void ModbusRtuPortConfWidget::updateParity(const QString &newParity)
  15840. {
  15841. updateAttribute("parity", newParity);
  15842. }
  15843. void ModbusRtuPortConfWidget::updateFlowControl(const QString &newFlow)
  15844. {
  15845. updateAttribute("flowcontrol", newFlow);
  15846. }
  15847. void ModbusRtuPortConfWidget::updateStopBits(const QString &newStopBits)
  15848. {
  15849. updateAttribute("stopbits", newStopBits);
  15850. }
  15851. @ From here we need to provide a widget for configuring a particular device.
  15852. At a minimum this would require setting the station number to a value between
  15853. 0 and 255. Zero is typically the broadcast address which reaches all devices
  15854. on the bus and is not generally recommended for use except in particular
  15855. circumstances. There are, however, a number of settings that influence all of
  15856. the currently supported child nodes and these settings are in the device
  15857. configuration widget instead of requiring that information to be duplicated
  15858. across multiple child nodes.
  15859. The Modbus RTU protocol is very general in scope and leaves many of the
  15860. details of how to do certain things up to the manufacturer. For rudimentary
  15861. support of devices using this protocol, the documentation for several devices
  15862. was consulted and a test rig with one device was set up. There are a number of
  15863. assumptions made for this initial support and to better support additional
  15864. device classes it may become necessary to expand on what is provided initially.
  15865. The primary focus presently is on the use of PID controllers as temperature
  15866. indicators with the ability to modify a set value in the case where this is
  15867. used as a controller rather than just a display.
  15868. All of the devices studied prior to adding this support made use of scaled
  15869. integer representation. In order to correctly determine the measured process
  15870. value it is necessary to know the unit of the measurement and the position of
  15871. the decimal point. It is generally possible to query this information, however
  15872. it may be useful to provide a way to specify fixed values in the event that a
  15873. device exposes these details in a way that is incompatible with my assumptions.
  15874. @<Class declarations@>=
  15875. class ModbusRtuDeviceConfWidget : public BasicDeviceConfigurationWidget
  15876. {
  15877. @[Q_OBJECT@]@;
  15878. public:@/
  15879. @[Q_INVOKABLE@]@, ModbusRtuDeviceConfWidget(DeviceTreeModel *model,
  15880. const QModelIndex &index);
  15881. @[private slots@]:@/
  15882. void updateStationNumber(int newStation);
  15883. void updateFixedUnit(bool newFixed);
  15884. void updateFixedDecimal(bool newFixed);
  15885. void updateUnit(const QString &newUnit);
  15886. void updateUnitAddress(int newAddress);
  15887. void updateValueF(int newValue);
  15888. void updateValueC(int newValue);
  15889. void updatePrecisionAddress(int newAddress);
  15890. void updatePrecisionValue(int newValue);
  15891. private:@/
  15892. QStackedLayout *unitSpecificationLayout;
  15893. QStackedLayout *decimalSpecificationLayout;
  15894. };
  15895. @ This widget has a number of differences from previous configuration widgets.
  15896. Perhaps most significantly there are controls which do not provide a text based
  15897. signal on state change. We also set certain controls as disabled when the
  15898. provided values are not relevant to operations such as when switching between
  15899. fixed decimal position and looking up decimal position from the device. Aside
  15900. from these details the widget operates according to the same principles as the
  15901. other widgets already seen.
  15902. @<ModbusRtuDeviceConfWidget implementation@>=
  15903. ModbusRtuDeviceConfWidget::ModbusRtuDeviceConfWidget(DeviceTreeModel *model,
  15904. const QModelIndex &index)
  15905. : BasicDeviceConfigurationWidget(model, index),
  15906. unitSpecificationLayout(new QStackedLayout),
  15907. decimalSpecificationLayout(new QStackedLayout)
  15908. {
  15909. QVBoxLayout *layout = new QVBoxLayout;
  15910. QToolButton *addChannelButton = new QToolButton;
  15911. addChannelButton->setText(tr("Add Channel"));
  15912. NodeInserter *addTemperaturePV = new NodeInserter("Temperature Process Value",
  15913. "Temperature Process Value",
  15914. "modbustemperaturepv");
  15915. NodeInserter *addTemperatureSV = new NodeInserter("Temperature Set Value",
  15916. "Temperature Set Value",
  15917. "modbustemperaturesv");
  15918. connect(addTemperaturePV, SIGNAL(triggered(QString, QString)),
  15919. this, SLOT(insertChildNode(QString, QString)));
  15920. connect(addTemperatureSV, SIGNAL(triggered(QString, QString)),
  15921. this, SLOT(insertChildNode(QString, QString)));
  15922. QMenu *channelMenu = new QMenu;
  15923. channelMenu->addAction(addTemperaturePV);
  15924. channelMenu->addAction(addTemperatureSV);
  15925. addChannelButton->setMenu(channelMenu);
  15926. addChannelButton->setPopupMode(QToolButton::InstantPopup);
  15927. layout->addWidget(addChannelButton);
  15928. QHBoxLayout *stationLayout = new QHBoxLayout;
  15929. QLabel *stationLabel = new QLabel(tr("Station:"));
  15930. QSpinBox *stationNumber = new QSpinBox;
  15931. stationNumber->setMinimum(0);
  15932. stationNumber->setMaximum(255);
  15933. stationLayout->addWidget(stationLabel);
  15934. stationLayout->addWidget(stationNumber);
  15935. layout->addLayout(stationLayout);
  15936. QCheckBox *fixedUnit = new QCheckBox(tr("Fixed Temperature Unit"));
  15937. layout->addWidget(fixedUnit);
  15938. QWidget *fixedUnitPlaceholder = new QWidget(this);
  15939. QHBoxLayout *fixedUnitLayout = new QHBoxLayout;
  15940. QLabel *fixedUnitLabel = new QLabel(tr("Temperature Unit:"));
  15941. QComboBox *fixedUnitSelector = new QComboBox;
  15942. fixedUnitSelector->addItem("Fahrenheit");
  15943. fixedUnitSelector->addItem("Celsius");
  15944. fixedUnitLayout->addWidget(fixedUnitLabel);
  15945. fixedUnitLayout->addWidget(fixedUnitSelector);
  15946. fixedUnitPlaceholder->setLayout(fixedUnitLayout);
  15947. unitSpecificationLayout->addWidget(fixedUnitPlaceholder);
  15948. QWidget *queriedUnitPlaceholder = new QWidget(this);
  15949. QFormLayout *queriedUnitLayout = new QFormLayout;
  15950. ShortHexSpinBox *unitAddress = new ShortHexSpinBox;
  15951. queriedUnitLayout->addRow(tr("Function 0x03 Unit Address:"), unitAddress);
  15952. QSpinBox *valueF = new QSpinBox;
  15953. valueF->setMinimum(0);
  15954. valueF->setMaximum(65535);
  15955. queriedUnitLayout->addRow(tr("Value for Fahrenheit"), valueF);
  15956. QSpinBox *valueC = new QSpinBox;
  15957. valueC->setMinimum(0);
  15958. valueC->setMaximum(65535);
  15959. queriedUnitLayout->addRow(tr("Value for Celsius"), valueC);
  15960. queriedUnitPlaceholder->setLayout(queriedUnitLayout);
  15961. unitSpecificationLayout->addWidget(queriedUnitPlaceholder);
  15962. layout->addLayout(unitSpecificationLayout);
  15963. QCheckBox *fixedPrecision = new QCheckBox(tr("Fixed Precision"));
  15964. layout->addWidget(fixedPrecision);
  15965. QWidget *fixedPrecisionPlaceholder = new QWidget(this);
  15966. QFormLayout *fixedPrecisionLayout = new QFormLayout;
  15967. QSpinBox *fixedPrecisionValue = new QSpinBox;
  15968. fixedPrecisionValue->setMinimum(0);
  15969. fixedPrecisionValue->setMaximum(9);
  15970. fixedPrecisionLayout->addRow("Places after the decimal point:",
  15971. fixedPrecisionValue);
  15972. fixedPrecisionPlaceholder->setLayout(fixedPrecisionLayout);
  15973. decimalSpecificationLayout->addWidget(fixedPrecisionPlaceholder);
  15974. QWidget *queriedPrecisionPlaceholder = new QWidget(this);
  15975. QFormLayout *queriedPrecisionLayout = new QFormLayout;
  15976. ShortHexSpinBox *precisionAddress = new ShortHexSpinBox;
  15977. queriedPrecisionLayout->addRow("Function 0x03 Decimal Position Address:",
  15978. precisionAddress);
  15979. queriedPrecisionPlaceholder->setLayout(queriedPrecisionLayout);
  15980. decimalSpecificationLayout->addWidget(queriedPrecisionPlaceholder);
  15981. layout->addLayout(decimalSpecificationLayout);
  15982. @<Get device configuration data for current node@>@;
  15983. for(int i = 0; i < configData.size(); i++)
  15984. {
  15985. node = configData.at(i).toElement();
  15986. if(node.attribute("name") == "station")
  15987. {
  15988. stationNumber->setValue(node.attribute("value").toInt());
  15989. }
  15990. else if(node.attribute("name") == "fixedunit")
  15991. {
  15992. if(node.attribute("value") == "true")
  15993. {
  15994. fixedUnit->setCheckState(Qt::Checked);
  15995. }
  15996. else if(node.attribute("value") == "false")
  15997. {
  15998. fixedUnit->setCheckState(Qt::Unchecked);
  15999. }
  16000. }
  16001. else if(node.attribute("name") == "fixedprecision")
  16002. {
  16003. fixedPrecisionValue->setValue(node.attribute("value").toInt());
  16004. }
  16005. else if(node.attribute("name") == "unit")
  16006. {
  16007. fixedUnitSelector->setCurrentIndex(fixedUnitSelector->findText(node.attribute("value")));
  16008. }
  16009. else if(node.attribute("name") == "unitaddress")
  16010. {
  16011. unitAddress->setValue(node.attribute("value").toInt());
  16012. }
  16013. else if(node.attribute("name") == "fvalue")
  16014. {
  16015. valueF->setValue(node.attribute("value").toInt());
  16016. }
  16017. else if(node.attribute("name") == "cvalue")
  16018. {
  16019. valueC->setValue(node.attribute("value").toInt());
  16020. }
  16021. else if(node.attribute("name") == "precisionaddress")
  16022. {
  16023. precisionAddress->setValue(node.attribute("value").toInt());
  16024. }
  16025. else if(node.attribute("name") == "precision")
  16026. {
  16027. fixedPrecisionValue->setValue(node.attribute("value").toInt());
  16028. }
  16029. }
  16030. updateStationNumber(stationNumber->value());
  16031. updateFixedUnit(fixedUnit->isChecked());
  16032. updateFixedDecimal(fixedPrecision->isChecked());
  16033. updateUnit(fixedUnitSelector->currentText());
  16034. updateUnitAddress(unitAddress->value());
  16035. updateValueF(valueF->value());
  16036. updateValueC(valueC->value());
  16037. updatePrecisionAddress(precisionAddress->value());
  16038. updatePrecisionValue(fixedPrecisionValue->value());
  16039. connect(stationNumber, SIGNAL(valueChanged(int)),
  16040. this, SLOT(updateStationNumber(int)));
  16041. connect(fixedUnitSelector, SIGNAL(currentIndexChanged(QString)),
  16042. this, SLOT(updateUnit(QString)));
  16043. connect(unitAddress, SIGNAL(valueChanged(int)),
  16044. this, SLOT(updateUnitAddress(int)));
  16045. connect(valueF, SIGNAL(valueChanged(int)),
  16046. this, SLOT(updateValueF(int)));
  16047. connect(valueC, SIGNAL(valueChanged(int)),
  16048. this, SLOT(updateValueC(int)));
  16049. connect(fixedUnit, SIGNAL(toggled(bool)),
  16050. this, SLOT(updateFixedUnit(bool)));
  16051. connect(fixedPrecision, SIGNAL(toggled(bool)),
  16052. this, SLOT(updateFixedDecimal(bool)));
  16053. connect(fixedPrecisionValue, SIGNAL(valueChanged(int)),
  16054. this, SLOT(updatePrecisionValue(int)));
  16055. connect(precisionAddress, SIGNAL(valueChanged(int)),
  16056. this, SLOT(updatePrecisionAddress(int)));
  16057. setLayout(layout);
  16058. }
  16059. void ModbusRtuDeviceConfWidget::updateStationNumber(int newStation)
  16060. {
  16061. updateAttribute("station", QString("%1").arg(newStation));
  16062. }
  16063. void ModbusRtuDeviceConfWidget::updateFixedUnit(bool newFixed)
  16064. {
  16065. if(newFixed)
  16066. {
  16067. unitSpecificationLayout->setCurrentIndex(0);
  16068. updateAttribute("fixedunit", "true");
  16069. }
  16070. else
  16071. {
  16072. unitSpecificationLayout->setCurrentIndex(1);
  16073. updateAttribute("fixedunit", "false");
  16074. }
  16075. }
  16076. void ModbusRtuDeviceConfWidget::updateFixedDecimal(bool newFixed)
  16077. {
  16078. if(newFixed)
  16079. {
  16080. decimalSpecificationLayout->setCurrentIndex(0);
  16081. updateAttribute("fixedprecision", "true");
  16082. }
  16083. else
  16084. {
  16085. decimalSpecificationLayout->setCurrentIndex(1);
  16086. updateAttribute("fixedprecision", "false");
  16087. }
  16088. }
  16089. void ModbusRtuDeviceConfWidget::updateUnit(const QString &newUnit)
  16090. {
  16091. updateAttribute("unit", newUnit);
  16092. }
  16093. void ModbusRtuDeviceConfWidget::updateUnitAddress(int newAddress)
  16094. {
  16095. updateAttribute("unitaddress", QString("%1").arg(newAddress));
  16096. }
  16097. void ModbusRtuDeviceConfWidget::updateValueF(int newValue)
  16098. {
  16099. updateAttribute("fvalue", QString("%1").arg(newValue));
  16100. }
  16101. void ModbusRtuDeviceConfWidget::updateValueC(int newValue)
  16102. {
  16103. updateAttribute("cvalue", QString("%1").arg(newValue));
  16104. }
  16105. void ModbusRtuDeviceConfWidget::updatePrecisionAddress(int newAddress)
  16106. {
  16107. updateAttribute("precisionaddress", QString("%1").arg(newAddress));
  16108. }
  16109. void ModbusRtuDeviceConfWidget::updatePrecisionValue(int newValue)
  16110. {
  16111. updateAttribute("precision", QString("%1").arg(newValue));
  16112. }
  16113. @ Initial Modbus RTU support is very limited and only considers temperature
  16114. process and set values. While in some cases it would be possible to cleverly
  16115. adapt this support to broader categories this is an area that must be extended
  16116. later to cover at least unitless control values and on/off status values. It
  16117. would be ideal to cover a broad range of useful properties. To read process
  16118. values we need to know the address that the current process value can be read
  16119. from.
  16120. @<Class declarations@>=
  16121. class ModbusRtuDeviceTPvConfWidget : public BasicDeviceConfigurationWidget
  16122. {
  16123. @[Q_OBJECT@]@/
  16124. public:@/
  16125. @[Q_INVOKABLE@]@, ModbusRtuDeviceTPvConfWidget(DeviceTreeModel *model,
  16126. const QModelIndex &index);
  16127. @[private slots@]:@/
  16128. void updateAddress(int newAddress);
  16129. };
  16130. @ This requires only a single field to store the address to query the current
  16131. process value.
  16132. @<ModbusRtuDeviceTPvConfWidget implementation@>=
  16133. ModbusRtuDeviceTPvConfWidget::ModbusRtuDeviceTPvConfWidget(DeviceTreeModel *model,
  16134. const QModelIndex &index)
  16135. : BasicDeviceConfigurationWidget(model, index)
  16136. {
  16137. QFormLayout *layout = new QFormLayout;
  16138. ShortHexSpinBox *address = new ShortHexSpinBox;
  16139. layout->addRow(tr("Function 0x04 Process Value Address"), address);
  16140. @<Get device configuration data for current node@>@;
  16141. for(int i = 0; i < configData.size(); i++)
  16142. {
  16143. node = configData.at(i).toElement();
  16144. if(node.attribute("name") == "address")
  16145. {
  16146. address->setValue(node.attribute("value").toInt());
  16147. break;
  16148. }
  16149. }
  16150. updateAddress(address->value());
  16151. connect(address, SIGNAL(valueChanged(int)), this, SLOT(updateAddress(int)));
  16152. setLayout(layout);
  16153. }
  16154. void ModbusRtuDeviceTPvConfWidget::updateAddress(int newAddress)
  16155. {
  16156. updateAttribute("address", QString("%1").arg(newAddress));
  16157. }
  16158. @ Set values are slightly more complicated as we may want either a fixed range
  16159. or the ability to query the device for its current allowed range, but nothing
  16160. is here that hasn'@q'@>t been seen elsewhere.
  16161. @<Class declarations@>=
  16162. class ModbusRtuDeviceTSvConfWidget : public BasicDeviceConfigurationWidget@/
  16163. {@/
  16164. @[Q_OBJECT@]@;
  16165. public:@/
  16166. Q_INVOKABLE@, ModbusRtuDeviceTSvConfWidget(DeviceTreeModel *model,
  16167. const QModelIndex &index);
  16168. @[private slots@]:@/
  16169. void updateReadAddress(int newAddress);
  16170. void updateWriteAddress(int newAddress);
  16171. void updateFixedRange(bool fixed);
  16172. void updateLower(const QString &lower);
  16173. void updateUpper(const QString &upper);
  16174. void updateLowerAddress(int newAddress);
  16175. void updateUpperAddress(int newAddress);@/
  16176. private:@/
  16177. QStackedLayout *boundsLayout;
  16178. };
  16179. @ Upper and lower bounds when operating on a fixed range are still subject to
  16180. decimal position rules in the parent node. It may be a good idea to enforce
  16181. this, however at present the person configuring the system is trusted to know
  16182. what they are doing.
  16183. @<ModbusRtuDeviceTSvConfWidget implementation@>=
  16184. ModbusRtuDeviceTSvConfWidget::ModbusRtuDeviceTSvConfWidget(DeviceTreeModel *model,
  16185. const QModelIndex &index)
  16186. : BasicDeviceConfigurationWidget(model, index), boundsLayout(new QStackedLayout)
  16187. {
  16188. QVBoxLayout *layout = new QVBoxLayout;
  16189. QFormLayout *addressLayout = new QFormLayout;
  16190. ShortHexSpinBox *readAddress = new ShortHexSpinBox;
  16191. ShortHexSpinBox *writeAddress = new ShortHexSpinBox;
  16192. addressLayout->addRow(tr("Function 0x04 Read Set Value Address:"), readAddress);
  16193. addressLayout->addRow(tr("Function 0x06 Write Set Value Address:"), writeAddress);
  16194. layout->addLayout(addressLayout);
  16195. QCheckBox *fixedRange = new QCheckBox(tr("Fixed Set Value Range"));
  16196. layout->addWidget(fixedRange);
  16197. QWidget *queriedRangePlaceholder = new QWidget(this);
  16198. QFormLayout *queriedRangeLayout = new QFormLayout;
  16199. ShortHexSpinBox *lowerAddress = new ShortHexSpinBox;
  16200. ShortHexSpinBox *upperAddress = new ShortHexSpinBox;
  16201. queriedRangeLayout->addRow(tr("Function 0x03 Minimum Set Value Address"),
  16202. lowerAddress);
  16203. queriedRangeLayout->addRow(tr("Function 0x03 Maximum Set Value Address"),
  16204. upperAddress);
  16205. queriedRangePlaceholder->setLayout(queriedRangeLayout);
  16206. boundsLayout->addWidget(queriedRangePlaceholder);
  16207. QWidget *fixedRangePlaceholder = new QWidget(this);
  16208. QFormLayout *fixedRangeLayout = new QFormLayout;
  16209. QLineEdit *fixedLower = new QLineEdit;
  16210. QLineEdit *fixedUpper = new QLineEdit;
  16211. fixedRangeLayout->addRow(tr("Minimum Set Value:"), fixedLower);
  16212. fixedRangeLayout->addRow(tr("Maximum Set Value:"), fixedUpper);
  16213. fixedRangePlaceholder->setLayout(fixedRangeLayout);
  16214. boundsLayout->addWidget(fixedRangePlaceholder);
  16215. layout->addLayout(boundsLayout);
  16216. @<Get device configuration data for current node@>@;
  16217. for(int i = 0; i < configData.size(); i++)
  16218. {
  16219. node = configData.at(i).toElement();
  16220. if(node.attribute("name") == "readaddress")
  16221. {
  16222. readAddress->setValue(node.attribute("value").toInt());
  16223. }
  16224. else if(node.attribute("name") == "writeaddress")
  16225. {
  16226. writeAddress->setValue(node.attribute("value").toInt());
  16227. }
  16228. else if(node.attribute("name") == "fixedrange")
  16229. {
  16230. if(node.attribute("value") == "true")
  16231. {
  16232. fixedRange->setCheckState(Qt::Checked);
  16233. }
  16234. else if(node.attribute("value") == "false")
  16235. {
  16236. fixedRange->setCheckState(Qt::Unchecked);
  16237. }
  16238. }
  16239. else if(node.attribute("name") == "fixedlower")
  16240. {
  16241. fixedLower->setText(node.attribute("value"));
  16242. }
  16243. else if(node.attribute("name") == "fixedupper")
  16244. {
  16245. fixedUpper->setText(node.attribute("value"));
  16246. }
  16247. else if(node.attribute("name") == "loweraddress")
  16248. {
  16249. lowerAddress->setValue(node.attribute("value").toInt());
  16250. }
  16251. else if(node.attribute("name") == "upperaddress")
  16252. {
  16253. upperAddress->setValue(node.attribute("value").toInt());
  16254. }
  16255. }
  16256. updateReadAddress(readAddress->value());
  16257. updateWriteAddress(writeAddress->value());
  16258. updateFixedRange(fixedRange->isChecked());
  16259. updateLower(fixedLower->text());
  16260. updateUpper(fixedUpper->text());
  16261. updateLowerAddress(lowerAddress->value());
  16262. updateUpperAddress(upperAddress->value());
  16263. connect(readAddress, SIGNAL(valueChanged(int)),
  16264. this, SLOT(updateReadAddress(int)));
  16265. connect(writeAddress, SIGNAL(valueChanged(int)),
  16266. this, SLOT(updateWriteAddress(int)));
  16267. connect(fixedRange, SIGNAL(toggled(bool)), this, SLOT(updateFixedRange(bool)));
  16268. connect(fixedLower, SIGNAL(textChanged(QString)),
  16269. this, SLOT(updateLower(QString)));
  16270. connect(fixedUpper, SIGNAL(textChanged(QString)),
  16271. this, SLOT(updateUpper(QString)));
  16272. connect(lowerAddress, SIGNAL(valueChanged(int)),
  16273. this, SLOT(updateLowerAddress(int)));
  16274. connect(upperAddress, SIGNAL(valueChanged(int)),
  16275. this, SLOT(updateUpperAddress(int)));
  16276. setLayout(layout);
  16277. }
  16278. void ModbusRtuDeviceTSvConfWidget::updateReadAddress(int newAddress)
  16279. {
  16280. updateAttribute("readaddress", QString("%1").arg(newAddress));
  16281. }
  16282. void ModbusRtuDeviceTSvConfWidget::updateWriteAddress(int newAddress)
  16283. {
  16284. updateAttribute("writeaddress", QString("%1").arg(newAddress));
  16285. }
  16286. void ModbusRtuDeviceTSvConfWidget::updateFixedRange(bool fixed)
  16287. {
  16288. if(fixed)
  16289. {
  16290. updateAttribute("fixedrange", "true");
  16291. boundsLayout->setCurrentIndex(1);
  16292. }
  16293. else
  16294. {
  16295. updateAttribute("fixedrange", "false");
  16296. boundsLayout->setCurrentIndex(0);
  16297. }
  16298. }
  16299. void ModbusRtuDeviceTSvConfWidget::updateLower(const QString &lower)
  16300. {
  16301. updateAttribute("fixedlower", lower);
  16302. }
  16303. void ModbusRtuDeviceTSvConfWidget::updateUpper(const QString &upper)
  16304. {
  16305. updateAttribute("fixedupper", upper);
  16306. }
  16307. void ModbusRtuDeviceTSvConfWidget::updateLowerAddress(int newAddress)
  16308. {
  16309. updateAttribute("loweraddress", QString("%1").arg(newAddress));
  16310. }
  16311. void ModbusRtuDeviceTSvConfWidget::updateUpperAddress(int newAddress)
  16312. {
  16313. updateAttribute("upperaddress", QString("%1").arg(newAddress));
  16314. }
  16315. @ The configuration widgets need to be registered.
  16316. @<Register device configuration widgets@>=
  16317. app.registerDeviceConfigurationWidget("modbusrtuport", ModbusRtuPortConfWidget::staticMetaObject);
  16318. app.registerDeviceConfigurationWidget("modbusrtudevice", ModbusRtuDeviceConfWidget::staticMetaObject);
  16319. app.registerDeviceConfigurationWidget("modbustemperaturepv", ModbusRtuDeviceTPvConfWidget::staticMetaObject);
  16320. app.registerDeviceConfigurationWidget("modbustemperaturesv", ModbusRtuDeviceTSvConfWidget::staticMetaObject);
  16321. @ A |NodeInserter| for the driver configuration widget is also needed. Note
  16322. that this is temporarily disabled. These configuration widgets will become
  16323. useful when I rearchitect the Modbus RTU support in a future release.
  16324. @<Register top level device configuration nodes@>=
  16325. #if 0
  16326. inserter = new NodeInserter(tr("Modbus RTU Port"), tr("Modbus RTU Port"), "modbusrtuport", NULL);
  16327. topLevelNodeInserters.append(inserter);
  16328. #endif
  16329. @** Configuration of Annotation Controls.
  16330. \noindent Aside from the details of hardware devices, the logging view must
  16331. also be able to set up log annotation controls. A few different control types
  16332. are offered. These include simple push buttons which insert a fixed annotation
  16333. when activated, push buttons which insert a value that includes a number which
  16334. is incremented every time the button is pressed, free text entry fields, and
  16335. numeric entry fields.
  16336. The basic push button control should allow configuration of both the button
  16337. text and the annotation text.
  16338. @<Class declarations@>=
  16339. class AnnotationButtonConfWidget : public BasicDeviceConfigurationWidget
  16340. {
  16341. @[Q_OBJECT@]@;
  16342. public:@/
  16343. @[Q_INVOKABLE@]@, AnnotationButtonConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  16344. @[private slots@]:@/
  16345. void updateButtonText(const QString &text);
  16346. void updateAnnotationText(const QString &text);
  16347. };
  16348. @ The constructor sets up the controls for editing this data.
  16349. @<AnnotationButtonConfWidget implementation@>=
  16350. AnnotationButtonConfWidget::AnnotationButtonConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  16351. : BasicDeviceConfigurationWidget(model, index)
  16352. {
  16353. QFormLayout *layout = new QFormLayout;
  16354. QLineEdit *buttonTextEdit = new QLineEdit;
  16355. QLineEdit *annotationTextEdit = new QLineEdit;
  16356. layout->addRow(tr("Button Text:"), buttonTextEdit);
  16357. layout->addRow(tr("Annotation Text:"), annotationTextEdit);
  16358. @<Get device configuration data for current node@>@;
  16359. for(int i = 0; i < configData.size(); i++)
  16360. {
  16361. node = configData.at(i).toElement();
  16362. if(node.attribute("name") == "buttontext")
  16363. {
  16364. buttonTextEdit->setText(node.attribute("value"));
  16365. }
  16366. else if(node.attribute("name") == "annotationtext")
  16367. {
  16368. annotationTextEdit->setText(node.attribute("value"));
  16369. }
  16370. }
  16371. updateButtonText(buttonTextEdit->text());
  16372. updateAnnotationText(annotationTextEdit->text());
  16373. connect(buttonTextEdit, SIGNAL(textEdited(QString)), this, SLOT(updateButtonText(QString)));
  16374. connect(annotationTextEdit, SIGNAL(textEdited(QString)), this, SLOT(updateAnnotationText(QString)));
  16375. setLayout(layout);
  16376. }
  16377. @ The slots are implemented trivially.
  16378. @<AnnotationButtonConfWidget implementation@>=
  16379. void AnnotationButtonConfWidget::updateButtonText(const QString &text)
  16380. {
  16381. updateAttribute("buttontext", text);
  16382. }
  16383. void AnnotationButtonConfWidget::updateAnnotationText(const QString &text)
  16384. {
  16385. updateAttribute("annotationtext", text);
  16386. }
  16387. @ The control must be registered.
  16388. @<Register device configuration widgets@>=
  16389. app.registerDeviceConfigurationWidget("annotationbutton", AnnotationButtonConfWidget::staticMetaObject);
  16390. @ Closely related to the previous control is one which provides parameterized
  16391. text. Technically this is not needed as both this and the previous
  16392. configuration control map to the same widget in the logging view and
  16393. parameterized annotation text can be used with either. The reason for
  16394. separating these is to indicate that it should be possible to change the text
  16395. and reset the number without altering the default configuration or requiring a
  16396. reinitialization of the logging view.
  16397. @<Class declarations@>=
  16398. class ReconfigurableAnnotationButtonConfWidget : public BasicDeviceConfigurationWidget@/
  16399. {@/
  16400. @[Q_OBJECT@]@;
  16401. public:@/
  16402. Q_INVOKABLE ReconfigurableAnnotationButtonConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  16403. @[private slots@]:@/
  16404. void updateButtonText(const QString &text);
  16405. void updateAnnotationText(const QString &text);
  16406. };
  16407. @ The key difference in implementation is the addition of some documentation on
  16408. how to specify a numeric placeholder in the annotation text.
  16409. @<AnnotationButtonConfWidget implementation@>=
  16410. ReconfigurableAnnotationButtonConfWidget::ReconfigurableAnnotationButtonConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  16411. : BasicDeviceConfigurationWidget(model, index)
  16412. {
  16413. QFormLayout *layout = new QFormLayout;
  16414. QLineEdit *buttonTextEdit = new QLineEdit;
  16415. QLineEdit *annotationTextEdit = new QLineEdit;
  16416. annotationTextEdit->setText("%A-%1");
  16417. layout->addRow(tr("Button Text:"), buttonTextEdit);
  16418. layout->addRow(tr("Annotation Text:"), annotationTextEdit);
  16419. @<Get device configuration data for current node@>@;
  16420. for(int i = 0; i < configData.size(); i++)
  16421. {
  16422. node = configData.at(i).toElement();
  16423. if(node.attribute("name") == "buttontext")
  16424. {
  16425. buttonTextEdit->setText(node.attribute("value"));
  16426. }
  16427. else if(node.attribute("name") == "annotationtext")
  16428. {
  16429. annotationTextEdit->setText(node.attribute("value"));
  16430. }
  16431. }
  16432. updateButtonText(buttonTextEdit->text());
  16433. updateAnnotationText(annotationTextEdit->text());
  16434. connect(buttonTextEdit, SIGNAL(textEdited(QString)), this, SLOT(updateButtonText(QString)));
  16435. connect(annotationTextEdit, SIGNAL(textEdited(QString)), this, SLOT(updateAnnotationText(QString)));
  16436. QTextEdit *documentation = new QTextEdit;
  16437. documentation->setHtml(tr("If the <b>Annotation Text</b> contains <tt>%1</tt>, this will be replaced in the annotation with a number that increments each time the button is pressed."));
  16438. documentation->setReadOnly(true);
  16439. layout->addRow("", documentation);
  16440. setLayout(layout);
  16441. }
  16442. void ReconfigurableAnnotationButtonConfWidget::updateButtonText(const QString &text)
  16443. {
  16444. updateAttribute("buttontext", text);
  16445. }
  16446. void ReconfigurableAnnotationButtonConfWidget::updateAnnotationText(const QString &text)
  16447. {
  16448. updateAttribute("annotationtext", text);
  16449. }
  16450. @ The control must be registered as usual.
  16451. @<Register device configuration widgets@>=
  16452. app.registerDeviceConfigurationWidget("reconfigurablebutton", ReconfigurableAnnotationButtonConfWidget::staticMetaObject);
  16453. @ While it is generally better to have all measurements logged automatically,
  16454. many roasters would like to keep track of infrequently altered control
  16455. variables which have not been set up for automated logging. A reading from the
  16456. manometer after a fuel adjustment, for example, is frequently not available by
  16457. automated means. In cases such as this, providing for numeric annotation entry
  16458. may be desired. The |AnnotationSpinBox| provides for this. There are a few
  16459. details that are important in this. First is a label to better indicate to the
  16460. operator what values in this control represent. The range of allowed values and
  16461. the number of decimal places is important. This control also allows the
  16462. specification of text to precede and/or follow the numeric value and this must
  16463. be configurable.
  16464. @<Class declarations@>=
  16465. class NoteSpinConfWidget : public BasicDeviceConfigurationWidget
  16466. {
  16467. @[Q_OBJECT@]@;
  16468. public:@/
  16469. @[Q_INVOKABLE@]@, NoteSpinConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  16470. @[private slots@]:@/
  16471. void updateLabel(const QString &text);
  16472. void updateMinimum(const QString &minimum);
  16473. void updateMaximum(const QString &maximum);
  16474. void updatePrecision(int precision);
  16475. void updatePretext(const QString &text);
  16476. void updatePosttext(const QString &text);
  16477. };
  16478. @ There is nothing new in the implementation of note.
  16479. @<NoteSpinConfWidget implementation@>=
  16480. NoteSpinConfWidget::NoteSpinConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  16481. : BasicDeviceConfigurationWidget(model, index)
  16482. {
  16483. QFormLayout *layout = new QFormLayout;
  16484. QLineEdit *labelEdit = new QLineEdit;
  16485. layout->addRow(tr("Control Label: "), labelEdit);
  16486. QLineEdit *minimumEdit = new QLineEdit;
  16487. layout->addRow(tr("Minimum Value: "), minimumEdit);
  16488. QLineEdit *maximumEdit = new QLineEdit;
  16489. layout->addRow(tr("Maximum Value: "), maximumEdit);
  16490. QSpinBox *precisionEdit = new QSpinBox;
  16491. precisionEdit->setMinimum(0);
  16492. precisionEdit->setMaximum(9);
  16493. layout->addRow(tr("Precision"), precisionEdit);
  16494. QLineEdit *pretext = new QLineEdit;
  16495. layout->addRow(tr("Prefix text"), pretext);
  16496. QLineEdit *posttext = new QLineEdit;
  16497. layout->addRow(tr("Suffix text"), posttext);
  16498. @<Get device configuration data for current node@>@;
  16499. for(int i = 0; i < configData.size(); i++)
  16500. {
  16501. node = configData.at(i).toElement();
  16502. if(node.attribute("name") == "label")
  16503. {
  16504. labelEdit->setText(node.attribute("value"));
  16505. }
  16506. else if(node.attribute("name") == "minimum")
  16507. {
  16508. minimumEdit->setText(node.attribute("value"));
  16509. }
  16510. else if(node.attribute("name") == "maximum")
  16511. {
  16512. maximumEdit->setText(node.attribute("value"));
  16513. }
  16514. else if(node.attribute("name") == "precision")
  16515. {
  16516. precisionEdit->setValue(node.attribute("value").toInt());
  16517. }
  16518. else if(node.attribute("name") == "pretext")
  16519. {
  16520. pretext->setText(node.attribute("value"));
  16521. }
  16522. else if(node.attribute("name") == "posttext")
  16523. {
  16524. posttext->setText(node.attribute("value"));
  16525. }
  16526. }
  16527. updateLabel(labelEdit->text());
  16528. updateMinimum(minimumEdit->text());
  16529. updateMaximum(maximumEdit->text());
  16530. updatePrecision(precisionEdit->value());
  16531. updatePretext(pretext->text());
  16532. updatePosttext(posttext->text());
  16533. connect(labelEdit, SIGNAL(textEdited(QString)), this, SLOT(updateLabel(QString)));
  16534. connect(minimumEdit, SIGNAL(textEdited(QString)), this, SLOT(updateMinimum(QString)));
  16535. connect(maximumEdit, SIGNAL(textEdited(QString)), this, SLOT(updateMaximum(QString)));
  16536. connect(precisionEdit, SIGNAL(valueChanged(int)), this, SLOT(updatePrecision(int)));
  16537. connect(pretext, SIGNAL(textEdited(QString)), this, SLOT(updatePretext(QString)));
  16538. connect(posttext, SIGNAL(textEdited(QString)), this, SLOT(updatePosttext(QString)));
  16539. setLayout(layout);
  16540. }
  16541. void NoteSpinConfWidget::updateLabel(const QString &text)
  16542. {
  16543. updateAttribute("label", text);
  16544. }
  16545. void NoteSpinConfWidget::updateMinimum(const QString &minimum)
  16546. {
  16547. updateAttribute("minimum", minimum);
  16548. }
  16549. void NoteSpinConfWidget::updateMaximum(const QString &maximum)
  16550. {
  16551. updateAttribute("maximum", maximum);
  16552. }
  16553. void NoteSpinConfWidget::updatePrecision(int precision)
  16554. {
  16555. updateAttribute("precision", QString("%1").arg(precision));
  16556. }
  16557. void NoteSpinConfWidget::updatePretext(const QString &text)
  16558. {
  16559. updateAttribute("pretext", text);
  16560. }
  16561. void NoteSpinConfWidget::updatePosttext(const QString &text)
  16562. {
  16563. updateAttribute("posttext", text);
  16564. }
  16565. @ Configuration widget registration is as usual.
  16566. @<Register device configuration widgets@>=
  16567. app.registerDeviceConfigurationWidget("annotationspinbox", NoteSpinConfWidget::staticMetaObject);
  16568. @i freeannotation.w
  16569. @i settings.w
  16570. @** Communicating with a Device through Modbus RTU.
  16571. \noindent The classes described here need to be further generalized to support
  16572. communications with multiple devices on the same port. The interface is based
  16573. on the |DAQ| class but extended to support additional functionality.
  16574. @<Class declarations@>=
  16575. class ModbusRTUDevice : public QObject
  16576. {
  16577. @[Q_OBJECT@]@;
  16578. public:@/
  16579. ModbusRTUDevice(DeviceTreeModel *model, const QModelIndex &index);
  16580. ~ModbusRTUDevice();
  16581. void queueMessage(QByteArray request, QObject *object, const char *callback);
  16582. @[Q_INVOKABLE@,@, double@]@, SVLower();@t\2\2@>@/
  16583. @[Q_INVOKABLE@,@, double@]@, SVUpper();@t\2\2@>@/
  16584. @[Q_INVOKABLE@,@, int@]@, decimals();@t\2\2@>@/
  16585. QList<Channel*> channels;@/
  16586. @[public slots@]:@/
  16587. void outputSV(double sv);@/
  16588. signals:@/
  16589. void SVLowerChanged(double);
  16590. void SVUpperChanged(double);
  16591. void SVDecimalChanged(int);
  16592. void queueEmpty();@/
  16593. @[private slots@]:@/
  16594. void dataAvailable();
  16595. void sendNextMessage();
  16596. void decimalResponse(QByteArray response);
  16597. void unitResponse(QByteArray response);
  16598. void svlResponse(QByteArray response);
  16599. void svuResponse(QByteArray response);
  16600. void requestMeasurement();
  16601. void mResponse(QByteArray response);
  16602. void ignore(QByteArray response);
  16603. void timeout();@/
  16604. private:@/
  16605. QextSerialPort *port;
  16606. QByteArray responseBuffer;
  16607. QList<QByteArray> messageQueue;
  16608. QList<QObject *> retObjQueue;
  16609. QList<char *> callbackQueue;
  16610. quint16 calculateCRC(QByteArray data);
  16611. QTimer *messageDelayTimer;
  16612. QTimer *commTimeout;
  16613. int delayTime;
  16614. char station;
  16615. int decimalPosition;
  16616. int valueF;
  16617. int valueC;
  16618. bool unitIsF;
  16619. double outputSVLower;
  16620. double outputSVUpper;
  16621. QByteArray outputSVStub;
  16622. QByteArray pvStub;
  16623. QByteArray svStub;
  16624. QByteArray mStub;
  16625. quint16 pvaddress;
  16626. quint16 svaddress;
  16627. bool svenabled;
  16628. bool readingsv;
  16629. double savedpv;
  16630. bool waiting;
  16631. };
  16632. @ The constructor reads its configuration from the configuration sub-tree of the
  16633. port node. This was adapted from a prototype implementation which used
  16634. |QSettings| to store this data. Note that this will only process the first
  16635. device specified on the port, the first process value on that device, and the
  16636. first set value on that device. A much more versatile architecture has been
  16637. planned for a future release which allows multiple devices per bus and
  16638. arbitrarily many monitored addresses per device. Communications are initiated
  16639. immediately upon construction.
  16640. @<ModbusRTUDevice implementation@>=
  16641. ModbusRTUDevice::ModbusRTUDevice(DeviceTreeModel *model,@| const QModelIndex &index)
  16642. : QObject(NULL), messageDelayTimer(new QTimer), commTimeout(new QTimer), unitIsF(@[true@]), readingsv(@[false@]),
  16643. waiting(@[false@])@/
  16644. {@/
  16645. QDomElement portReferenceElement = model->referenceElement(model->data(index,
  16646. Qt::UserRole).toString());
  16647. QDomNodeList portConfigData = portReferenceElement.elementsByTagName("attribute");
  16648. QDomElement node;
  16649. QVariantMap attributes;
  16650. for(int i = 0; i < portConfigData.size(); i++)
  16651. {
  16652. node = portConfigData.at(i).toElement();
  16653. attributes.insert(node.attribute("name"), node.attribute("value"));
  16654. }
  16655. port = new QextSerialPort(attributes.value("port").toString(),
  16656. QextSerialPort::EventDriven);
  16657. int baudRate = attributes.value("baud").toInt();
  16658. port->setBaudRate((BaudRateType)baudRate);
  16659. double temp = ((double)(1) / (double)(baudRate)) * 48;
  16660. delayTime = (int)(temp * 3000);
  16661. messageDelayTimer->setSingleShot(true);
  16662. commTimeout->setSingleShot(true);
  16663. connect(messageDelayTimer, SIGNAL(timeout()), this, SLOT(sendNextMessage()));
  16664. connect(commTimeout, SIGNAL(timeout()), this, SLOT(timeout()));
  16665. port->setDataBits(DATA_8);
  16666. port->setParity((ParityType)attributes.value("parity").toInt());
  16667. port->setStopBits((StopBitsType)attributes.value("stop").toInt());
  16668. port->setFlowControl((FlowType)attributes.value("flow").toInt());
  16669. connect(port, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
  16670. port->open(QIODevice::ReadWrite);
  16671. station = (char)attributes.value("station").toInt();
  16672. if(attributes.value("decimalQuery") == "true")
  16673. {
  16674. decimalPosition = 0;
  16675. QByteArray message;
  16676. message.append(station);
  16677. message.append((char)0x03);
  16678. quint16 address = (quint16)attributes.value("decimalAddress").toInt();
  16679. char *addressBytes = (char*)&address;
  16680. message.append(addressBytes[1]);
  16681. message.append(addressBytes[0]);
  16682. message.append((char)0x00);
  16683. message.append((char)0x01);
  16684. queueMessage(message, this, "decimalResponse(QByteArray)");
  16685. }
  16686. else
  16687. {
  16688. decimalPosition = attributes.value("decimalPosition").toInt();
  16689. }
  16690. valueF = attributes.value("valueF").toInt();
  16691. valueC = attributes.value("valueC").toInt();
  16692. if(attributes.value("unitQuery") == "true")
  16693. {
  16694. QByteArray message;
  16695. message.append(station);
  16696. message.append((char)0x03);
  16697. quint16 address = (quint16)attributes.value("unitAddress").toInt();
  16698. char *addressBytes = (char*)&address;
  16699. message.append(addressBytes[1]);
  16700. message.append(addressBytes[0]);
  16701. message.append((char)0x00);
  16702. message.append((char)0x01);
  16703. queueMessage(message, this, "unitResponse(QByteArray)");
  16704. }
  16705. else
  16706. {
  16707. if(attributes.value("fixedUnit") == "Celsius")
  16708. {
  16709. unitIsF = @[false@];
  16710. }
  16711. }
  16712. if(attributes.value("sVWritable") == "true")
  16713. {
  16714. if(attributes.value("deviceLimit") == "true")
  16715. {
  16716. QByteArray lmessage;
  16717. lmessage.append(station);
  16718. lmessage.append((char)0x03);
  16719. quint16 laddress = (quint16)attributes.value("sVLowerAddr").toInt();
  16720. char *addressBytes = (char*)&laddress;
  16721. lmessage.append(addressBytes[1]);
  16722. lmessage.append(addressBytes[0]);
  16723. lmessage.append((char)0x00);
  16724. lmessage.append((char)0x01);
  16725. queueMessage(lmessage, this, "svlResponse(QByteArray)");
  16726. QByteArray umessage;
  16727. umessage.append(station);
  16728. umessage.append((char)0x03);
  16729. quint16 uaddress = (quint16)attributes.value("sVUpperAddr").toInt();
  16730. addressBytes = (char*)&uaddress;
  16731. umessage.append(addressBytes[1]);
  16732. umessage.append(addressBytes[0]);
  16733. umessage.append((char)0x00);
  16734. umessage.append((char)0x01);
  16735. queueMessage(umessage, this, "svuResponse(QByteArray)");
  16736. }
  16737. else
  16738. {
  16739. outputSVLower = attributes.value("sVLower").toDouble();
  16740. outputSVUpper = attributes.value("sVUpper").toDouble();
  16741. }
  16742. outputSVStub.append(station);
  16743. outputSVStub.append((char)0x06);
  16744. quint16 address = (quint16)attributes.value("sVOutputAddr").toInt();
  16745. char *addressBytes = (char*)&address;
  16746. outputSVStub.append(addressBytes[1]);
  16747. outputSVStub.append(addressBytes[0]);
  16748. }
  16749. Channel *pv = new Channel;
  16750. channels.append(pv);
  16751. pvStub.append(station);
  16752. pvStub.append((char)0x04);
  16753. pvaddress = (quint16)attributes.value("pVAddress").toInt();
  16754. char *pvac = (char*)&pvaddress;
  16755. pvStub.append(pvac[1]);
  16756. pvStub.append(pvac[0]);
  16757. pvStub.append((char)0x00);
  16758. pvStub.append((char)0x01);
  16759. svenabled = attributes.value("sVEnabled").toBool();
  16760. if(svenabled)
  16761. {
  16762. Channel *sv = new Channel;
  16763. channels.append(sv);
  16764. svStub.append(station);
  16765. svStub.append((char)0x04);
  16766. svaddress = (quint16)attributes.value("sVReadAddress").toInt();
  16767. char *svac = (char*)&svaddress;
  16768. svStub.append(svac[1]);
  16769. svStub.append(svac[0]);
  16770. svStub.append((char)0x00);
  16771. svStub.append((char)0x01);
  16772. if(svaddress - pvaddress == 1)
  16773. {
  16774. mStub.append(station);
  16775. mStub.append((char)0x04);
  16776. mStub.append(pvac[1]);
  16777. mStub.append(pvac[0]);
  16778. mStub.append((char)0x00);
  16779. mStub.append((char)0x02);
  16780. }
  16781. }
  16782. connect(this, SIGNAL(queueEmpty()), this, SLOT(requestMeasurement()));
  16783. requestMeasurement();
  16784. }
  16785. double ModbusRTUDevice::SVLower()
  16786. {
  16787. return outputSVLower;
  16788. }
  16789. double ModbusRTUDevice::SVUpper()
  16790. {
  16791. return outputSVUpper;
  16792. }
  16793. int ModbusRTUDevice::decimals()
  16794. {
  16795. return decimalPosition;
  16796. }
  16797. void ModbusRTUDevice::decimalResponse(QByteArray response)
  16798. {
  16799. quint16 temp;
  16800. char *tchar = (char*)&temp;
  16801. tchar[1] = response.at(3);
  16802. tchar[0] = response.at(4);
  16803. decimalPosition = temp;
  16804. emit SVDecimalChanged(decimalPosition);
  16805. }
  16806. void ModbusRTUDevice::unitResponse(QByteArray response)
  16807. {
  16808. quint16 temp;
  16809. char *tchar = (char*)&temp;
  16810. tchar[1] = response.at(3);
  16811. tchar[0] = response.at(4);
  16812. int value = temp;
  16813. if(value == valueF)
  16814. {
  16815. unitIsF = @[true@];
  16816. }
  16817. else
  16818. {
  16819. unitIsF = @[false@];
  16820. }
  16821. }
  16822. void ModbusRTUDevice::svlResponse(QByteArray response)
  16823. {
  16824. quint16 temp;
  16825. char *tchar = (char*)&temp;
  16826. tchar[1] = response.at(3);
  16827. tchar[0] = response.at(4);
  16828. outputSVLower = (double)temp;
  16829. for(int i = 0; i < decimalPosition; i++)
  16830. {
  16831. outputSVLower /= 10;
  16832. }
  16833. emit SVLowerChanged(outputSVLower);
  16834. }
  16835. void ModbusRTUDevice::svuResponse(QByteArray response)
  16836. {
  16837. quint16 temp;
  16838. char *tchar = (char*)&temp;
  16839. tchar[1] = response.at(3);
  16840. tchar[0] = response.at(4);
  16841. outputSVUpper = (double)temp;
  16842. for(int i = 0; i < decimalPosition; i++)
  16843. {
  16844. outputSVUpper /= 10;
  16845. }
  16846. emit SVUpperChanged(outputSVUpper);
  16847. }
  16848. void ModbusRTUDevice::requestMeasurement()
  16849. {
  16850. if(mStub.length() > 0)
  16851. {
  16852. queueMessage(mStub, this, "mResponse(QByteArray)");
  16853. }
  16854. else
  16855. {
  16856. queueMessage(pvStub, this, "mResponse(QByteArray)");
  16857. if(svenabled)
  16858. {
  16859. queueMessage(svStub, this, "mResponse(QByteArray)");
  16860. }
  16861. }
  16862. }
  16863. void ModbusRTUDevice::mResponse(QByteArray response)
  16864. {
  16865. QTime time = QTime::currentTime();
  16866. if(response.at(2) == 0x04)
  16867. {
  16868. @<Process PV and SV@>@;
  16869. }
  16870. else
  16871. {
  16872. @<Process PV or SV@>@;
  16873. }
  16874. }
  16875. @ There are two ways that we might request measurement data. All of the
  16876. devices I'@q'@>ve seen documented provide function 0x4 addresses for PV and SV
  16877. such that SV can be obtained from the address immediately after the address
  16878. from which we obtain PV. In this case we request both values at the same time.
  16879. @<Process PV and SV@>=
  16880. quint16 pv;
  16881. quint16 sv;
  16882. char *pvBytes = (char*)&pv;
  16883. char *svBytes = (char*)&sv;
  16884. pvBytes[1] = response.at(3);
  16885. pvBytes[0] = response.at(4);
  16886. svBytes[1] = response.at(5);
  16887. svBytes[0] = response.at(6);
  16888. double pvOut = (double)pv;
  16889. double svOut = (double)sv;
  16890. for(int i = 0; i < decimalPosition; i++)
  16891. {
  16892. pvOut /= 10;
  16893. svOut /= 10;
  16894. }
  16895. if(!unitIsF)
  16896. {
  16897. pvOut = pvOut * 9 / 5 + 32;
  16898. svOut = svOut * 9 / 5 + 32;
  16899. }
  16900. Measurement pvm(pvOut, time, Units::Fahrenheit);
  16901. Measurement svm(svOut, time, Units::Fahrenheit);
  16902. channels.at(0)->input(pvm);
  16903. channels.at(1)->input(svm);
  16904. @ When not measuring PV and SV at the same time, there are two possibilities.
  16905. One possibility is that SV is not enabled and we will only be reading from PV.
  16906. The other possibility is that we are alternating between reading PV and SV.
  16907. @<Process PV or SV@>=
  16908. quint16 value;
  16909. char *valueBytes = (char*)&value;
  16910. valueBytes[1] = response.at(3);
  16911. valueBytes[0] = response.at(4);
  16912. double valueOut = (double)value;
  16913. for(int i = 0; i < decimalPosition; i++)
  16914. {
  16915. valueOut /= 10;
  16916. }
  16917. if(!unitIsF)
  16918. {
  16919. valueOut = valueOut * 9 / 5 + 32;
  16920. }
  16921. if(!svenabled)
  16922. {
  16923. Measurement vm(valueOut, time, Units::Fahrenheit);
  16924. channels.at(0)->input(vm);
  16925. }
  16926. else
  16927. {
  16928. if(readingsv)
  16929. {
  16930. Measurement pvm(savedpv, time, Units::Fahrenheit);
  16931. Measurement svm(valueOut, time, Units::Fahrenheit);
  16932. channels.at(0)->input(pvm);
  16933. channels.at(1)->input(svm);
  16934. readingsv = false;
  16935. }
  16936. else
  16937. {
  16938. savedpv = valueOut;
  16939. readingsv = true;
  16940. }
  16941. }
  16942. @ The destructor should close the port.
  16943. @<ModbusRTUDevice implementation@>=
  16944. ModbusRTUDevice::~ModbusRTUDevice()
  16945. {
  16946. commTimeout->stop();
  16947. messageDelayTimer->stop();
  16948. port->close();
  16949. }
  16950. @ When data is available it should be read into a buffer. The start of the
  16951. buffer should always be the start of a response and there should never be
  16952. more than one response in the buffer at a time. It is, however, likely that
  16953. this buffer will have incomplete data. This means that we must determine when
  16954. the full response is available before passing the complete response along to
  16955. the appropriate method. If the response has not been received in full, nothing
  16956. is done. We'@q'@>ll be notified of more data shortly.
  16957. When the message we see the response for was queued, a callback was also
  16958. registered to handle the response. Once we have the complete message, we pass
  16959. the response along to the callback that was registered for that message,
  16960. remove the message and callback information from the message queue, and start
  16961. a timer which will trigger sending the next message after a safe amount of
  16962. time has passed.
  16963. If a response is received with an invalid CRC, we do not pass that message
  16964. out. Instead, the message handling queues are kept as they are and the previous
  16965. command will be sent again once the message delay timer is finished.
  16966. @<ModbusRTUDevice implementation@>=
  16967. void ModbusRTUDevice::dataAvailable()
  16968. {
  16969. if(messageDelayTimer->isActive())
  16970. {
  16971. messageDelayTimer->stop();
  16972. }
  16973. responseBuffer.append(port->readAll());
  16974. @<Check Modbus RTU message size@>@;
  16975. commTimeout->stop();
  16976. if(calculateCRC(responseBuffer) == 0)
  16977. {
  16978. QObject *object = retObjQueue.at(0);
  16979. char *method = callbackQueue.at(0);
  16980. QMetaMethod metamethod = @| object->metaObject()->
  16981. method(object->metaObject()->
  16982. indexOfMethod(@|QMetaObject::normalizedSignature(method)));
  16983. metamethod.invoke(object, Qt::QueuedConnection,
  16984. Q_ARG(QByteArray, responseBuffer));
  16985. messageQueue.removeAt(0);
  16986. retObjQueue.removeAt(0);
  16987. callbackQueue.removeAt(0);
  16988. }
  16989. else
  16990. {
  16991. qDebug() << "CRC failed";
  16992. }
  16993. messageDelayTimer->start(delayTime);
  16994. waiting = @[false@];
  16995. responseBuffer.clear();
  16996. }
  16997. @ In Modbus RTU, a response message starts with one byte identifying the device
  16998. the message was sent from, one byte indicating the function, a variable number
  16999. of bytes with the response data, and two bytes used to verify that the response
  17000. was correctly received. In the event of a normal response, messages will be at
  17001. least six bytes long, but in the event of an error it is possible for a message
  17002. to be five bytes long.
  17003. Messages with a function number of 0x01 or 0x02 will be 6 bytes in length.
  17004. Messages with a function number of 0x03 or 0x04 will be at least 7 bytes in
  17005. length with the total length determined by the sum of 5 and the value in the
  17006. fifth byte. Messages with a function number of 0x05, 0x06, or 0x10 will be 8
  17007. bytes in length. Messages with a function number greater than 0x80 will be five
  17008. bytes in length.
  17009. @<Check Modbus RTU message size@>=
  17010. if(responseBuffer.size() < 5)
  17011. {
  17012. return;
  17013. }
  17014. switch(responseBuffer.at(1))
  17015. {
  17016. case 0x01:
  17017. case 0x02:
  17018. if(responseBuffer.size() < 6)
  17019. {
  17020. return;
  17021. }
  17022. responseBuffer = responseBuffer.left(6);
  17023. break;
  17024. case 0x03:
  17025. case 0x04:
  17026. if(responseBuffer.size() < 5 + responseBuffer.at(2))
  17027. {
  17028. return;
  17029. }
  17030. responseBuffer = responseBuffer.left(5 + responseBuffer.at(2));
  17031. break;
  17032. case 0x05:
  17033. case 0x06:
  17034. case 0x10:
  17035. if(responseBuffer.size() < 8)
  17036. {
  17037. return;
  17038. }
  17039. responseBuffer = responseBuffer.left(8);
  17040. break;
  17041. }
  17042. @ When sending and receiving messages, it is necessary to calculate a 16 bit
  17043. cyclic redundancy check code. The algorithm used to calculate this is specified
  17044. by the Modbus RTU protocol documentation. When sending a message, |data| should
  17045. be the message to send except for the CRC bytes which will be appended once
  17046. this method calculates them. When receiving a message, passing the complete
  17047. message back through this method should result in a return value of |0|. Any
  17048. other value indicates an error.
  17049. @<ModbusRTUDevice implementation@>=
  17050. quint16 ModbusRTUDevice::calculateCRC(QByteArray data)
  17051. {
  17052. quint16 retval = 0xFFFF;
  17053. int i = 0;
  17054. while(i < data.size())
  17055. {
  17056. retval ^= 0x00FF & (quint16)data.at(i);
  17057. for(int j = 0; j < 8; j++)
  17058. {
  17059. if(retval & 1)
  17060. {
  17061. retval = (retval >> 1) ^ 0xA001;
  17062. }
  17063. else
  17064. {
  17065. retval >>= 1;
  17066. }
  17067. }
  17068. i++;
  17069. }
  17070. return retval;
  17071. }
  17072. @ When preparing an instance of ModbusRTUDevice, several messages may need to
  17073. be sent to the device in order to determine important details such as how
  17074. measurement data should be interpreted. During normal operation, messages
  17075. might be sent interactively between regular messages to acquire data. When
  17076. queueing a message, we also specify an object and method the response should be
  17077. sent to.
  17078. @<ModbusRTUDevice implementation@>=
  17079. void ModbusRTUDevice::queueMessage(QByteArray request, QObject *object,
  17080. const char *callback)
  17081. {
  17082. messageQueue.append(request);
  17083. retObjQueue.append(object);
  17084. callbackQueue.append(const_cast<char*>(callback));
  17085. if(messageQueue.size() == 1 && !(messageDelayTimer->isActive()))
  17086. {
  17087. sendNextMessage();
  17088. }
  17089. }
  17090. void ModbusRTUDevice::sendNextMessage()
  17091. {
  17092. if(messageQueue.size() > 0 && !waiting)
  17093. {
  17094. QByteArray message = messageQueue.at(0);
  17095. quint16 crc = calculateCRC(message);
  17096. char *check = (char*)&crc;
  17097. message.append(check[0]);
  17098. message.append(check[1]);
  17099. port->write(message);
  17100. commTimeout->start(2000);
  17101. messageDelayTimer->start(delayTime);
  17102. waiting = @[true@];
  17103. }
  17104. else
  17105. {
  17106. emit queueEmpty();
  17107. }
  17108. }
  17109. void ModbusRTUDevice::outputSV(double value)
  17110. {
  17111. for(int i = 0; i < decimalPosition; i++)
  17112. {
  17113. value *= 10;
  17114. }
  17115. quint16 outval = (quint16)value;
  17116. QByteArray message(outputSVStub);
  17117. char *valBytes = (char*)&outval;
  17118. message.append(valBytes[1]);
  17119. message.append(valBytes[0]);
  17120. queueMessage(message, this, "ignore(QByteArray)");
  17121. }
  17122. @ We don'@q'@>t care about the response when sending a new SV.
  17123. @<ModbusRTUDevice implementation@>=
  17124. void ModbusRTUDevice::ignore(QByteArray)
  17125. {
  17126. return;
  17127. }
  17128. @ Sometimes a communications failure will occur in which a response to a
  17129. command is never received. To reset communications we set a timer whenever a
  17130. command is sent and stop that once a full response is received. If the timer
  17131. times out, we should clear the response buffer and attempt to re-establish
  17132. communications. Currently this timeout is hard coded at 2 seconds, however
  17133. this should be configurable and smaller values may well be acceptable.
  17134. @<ModbusRTUDevice implementation@>=
  17135. void ModbusRTUDevice::timeout()
  17136. {
  17137. qDebug() << "Communications timeout.";
  17138. responseBuffer.clear();
  17139. waiting = false;
  17140. messageDelayTimer->start();
  17141. }
  17142. @ This class must be exposed to the host environment.
  17143. @<Function prototypes for scripting@>=
  17144. QScriptValue constructModbusRTUDevice(QScriptContext *context, QScriptEngine *engine);
  17145. QScriptValue ModbusRTUDevice_pVChannel(QScriptContext *context, QScriptEngine *engine);
  17146. QScriptValue ModbusRTUDevice_sVChannel(QScriptContext *context, QScriptEngine *engine);
  17147. void setModbusRTUDeviceProperties(QScriptValue value, QScriptEngine *engine);
  17148. @ The host environment is informed of the constructor.
  17149. @<Set up the scripting engine@>=
  17150. constructor = engine->newFunction(constructModbusRTUDevice);
  17151. value = engine->newQMetaObject(&ModbusRTUDevice::staticMetaObject, constructor);
  17152. engine->globalObject().setProperty("ModbusRTUDevice", value);
  17153. @ The constructor takes the configuration model and the index to the device of
  17154. interest as arguments rather than provide a large number of property setters to
  17155. handle initialization.
  17156. @<Functions for scripting@>=
  17157. QScriptValue constructModbusRTUDevice(QScriptContext *context, QScriptEngine *engine)
  17158. {
  17159. QScriptValue object;
  17160. if(context->argumentCount() == 2)
  17161. {
  17162. object = engine->newQObject(new ModbusRTUDevice(argument<DeviceTreeModel *>(0, context),
  17163. argument<QModelIndex>(1, context)),
  17164. QScriptEngine::ScriptOwnership);
  17165. setModbusRTUDeviceProperties(object, engine);
  17166. }
  17167. else
  17168. {
  17169. context->throwError("Incorrect number of arguments passed to "@|
  17170. "ModbusRTUDevice constructor. This takes the configuration model "@|
  17171. "and an index.");
  17172. }
  17173. return object;
  17174. }
  17175. @ The host environment needs a way to gain access to the channel objects.
  17176. @<Functions for scripting@>=
  17177. QScriptValue ModbusRTUDevice_pVChannel(QScriptContext *context, QScriptEngine *engine)
  17178. {
  17179. ModbusRTUDevice *self = getself<ModbusRTUDevice *>(context);
  17180. QScriptValue object;
  17181. if(self)
  17182. {
  17183. if(self->channels.size() > 0)
  17184. {
  17185. object = engine->newQObject(self->channels.at(0));
  17186. setChannelProperties(object, engine);
  17187. }
  17188. }
  17189. return object;
  17190. }
  17191. QScriptValue ModbusRTUDevice_sVChannel(QScriptContext *context, QScriptEngine *engine)
  17192. {
  17193. ModbusRTUDevice *self = getself<ModbusRTUDevice *>(context);
  17194. QScriptValue object;
  17195. if(self)
  17196. {
  17197. if(self->channels.size() > 1)
  17198. {
  17199. object = engine->newQObject(self->channels.at(1));
  17200. setChannelProperties(object, engine);
  17201. }
  17202. }
  17203. return object;
  17204. }
  17205. @ These methods are set as properties when the object is created.
  17206. @<Functions for scripting@>=
  17207. void setModbusRTUDeviceProperties(QScriptValue value, QScriptEngine *engine)
  17208. {
  17209. setQObjectProperties(value, engine);
  17210. value.setProperty("pVChannel", engine->newFunction(ModbusRTUDevice_pVChannel));
  17211. value.setProperty("sVChannel", engine->newFunction(ModbusRTUDevice_sVChannel));
  17212. }
  17213. @* Modbus RTU device configuration widget.
  17214. \noindent This class was minimally adapted from a prototype implementation to
  17215. use the new configuration system introduced in \pn{} 1.4.
  17216. With all of the custom widgets for specifying a device configuration in place,
  17217. we can proceed to combine these in a form. As all of the options might use more
  17218. screen space than is available we make this scrollable. Some reorganization of
  17219. this will be done prior to release to enable the use of multiple devices on the
  17220. port which may obviate the need for this, but as there are those who prefer to
  17221. have a small screen it may be better to leave the scroll area in place even
  17222. after such a change.
  17223. @<Class declarations@>=
  17224. class ModbusConfigurator : public BasicDeviceConfigurationWidget
  17225. {
  17226. @[Q_OBJECT@]@;
  17227. public:@/
  17228. Q_INVOKABLE@,@, ModbusConfigurator(DeviceTreeModel *model, const QModelIndex &index);@/
  17229. @[private slots@]:@/
  17230. void updatePort(const QString &newPort);
  17231. void updateBaudRate(const QString &newRate);
  17232. void updateParity(const QString &newParity);
  17233. void updateFlowControl(const QString &newFlow);
  17234. void updateStopBits(const QString &newStopBits);
  17235. void updateStation(int station);
  17236. void updateFixedDecimal(bool fixed);
  17237. void updateDecimalAddress(int address);
  17238. void updateDecimalPosition(int position);
  17239. void updateFixedUnit(bool fixed);
  17240. void updateUnitAddress(int address);
  17241. void updateValueForF(int value);
  17242. void updateValueForC(int value);
  17243. void updateUnit(const QString &newUnit);
  17244. void updatePVAddress(int address);
  17245. void updateSVEnabled(bool enabled);
  17246. void updateSVReadAddress(int address);
  17247. void updateDeviceLimit(bool query);
  17248. void updateSVLowerAddress(int address);
  17249. void updateSVUpperAddress(int address);
  17250. void updateSVLower(double value);
  17251. void updateSVUpper(double value);
  17252. void updateSVWritable(bool canWriteSV);
  17253. void updateSVWriteAddress(int address);
  17254. void updatePVColumnName(const QString &name);
  17255. void updateSVColumnName(const QString &name);
  17256. void updatePVHidden(bool hidden);
  17257. void updateSVHidden(bool hidden);@/
  17258. private:@/
  17259. PortSelector *port;
  17260. BaudSelector *baud;
  17261. ParitySelector *parity;
  17262. FlowSelector *flow;
  17263. StopSelector *stop;
  17264. QSpinBox *station;
  17265. QCheckBox *decimalQuery;
  17266. ShortHexSpinBox *decimalAddress;
  17267. QSpinBox *decimalPosition;
  17268. QCheckBox *unitQuery;
  17269. ShortHexSpinBox *unitAddress;
  17270. QSpinBox *valueF;
  17271. QSpinBox *valueC;
  17272. QComboBox *fixedUnit;
  17273. ShortHexSpinBox *pVAddress;
  17274. QCheckBox *sVEnabled;
  17275. ShortHexSpinBox *sVReadAddress;
  17276. QCheckBox *deviceLimit;
  17277. ShortHexSpinBox *sVLowerAddr;
  17278. ShortHexSpinBox *sVUpperAddr;
  17279. QDoubleSpinBox *sVLower;
  17280. QDoubleSpinBox *sVUpper;
  17281. QCheckBox *sVWritable;
  17282. ShortHexSpinBox *sVOutputAddr;
  17283. QLineEdit *pVColumnName;
  17284. QLineEdit *sVColumnName;
  17285. };
  17286. @ Implementation.
  17287. @<ModbusConfigurator implementation@>=
  17288. ModbusConfigurator::ModbusConfigurator(DeviceTreeModel *model, const QModelIndex &index)
  17289. : BasicDeviceConfigurationWidget(model, index),
  17290. port(new PortSelector), baud(new BaudSelector), parity(new ParitySelector),
  17291. flow(new FlowSelector), stop(new StopSelector), station(new QSpinBox),
  17292. decimalQuery(new QCheckBox(tr("Enable"))),
  17293. decimalAddress(new ShortHexSpinBox), decimalPosition(new QSpinBox),
  17294. unitQuery(new QCheckBox(tr("Enable"))),
  17295. unitAddress(new ShortHexSpinBox), valueF(new QSpinBox),
  17296. valueC(new QSpinBox), fixedUnit(new QComboBox),
  17297. pVAddress(new ShortHexSpinBox),
  17298. sVEnabled(new QCheckBox(tr("Enable"))),
  17299. sVReadAddress(new ShortHexSpinBox),
  17300. deviceLimit(new QCheckBox(tr("Enable"))),
  17301. sVLowerAddr(new ShortHexSpinBox), sVUpperAddr(new ShortHexSpinBox),
  17302. sVLower(new QDoubleSpinBox), sVUpper(new QDoubleSpinBox),
  17303. sVWritable(new QCheckBox(tr("Enable"))),
  17304. sVOutputAddr(new ShortHexSpinBox),
  17305. pVColumnName(new QLineEdit), sVColumnName(new QLineEdit)
  17306. {
  17307. QHBoxLayout *layout = new QHBoxLayout;
  17308. QWidget *form = new QWidget;
  17309. QHBoxLayout *masterLayout = new QHBoxLayout;
  17310. QVBoxLayout *portAndDeviceLayout = new QVBoxLayout;
  17311. QVBoxLayout *seriesLayout = new QVBoxLayout;
  17312. QFormLayout *serialSection = new QFormLayout;
  17313. serialSection->addRow(QString(tr("Port:")), port);
  17314. serialSection->addRow(QString(tr("Baud rate:")), baud);
  17315. serialSection->addRow(QString(tr("Parity:")), parity);
  17316. serialSection->addRow(QString(tr("Flow control:")), flow);
  17317. serialSection->addRow(QString(tr("Stop bits:")), stop);
  17318. QGroupBox *serialSectionBox = new QGroupBox(tr("Serial Port Configuration"));
  17319. serialSectionBox->setLayout(serialSection);
  17320. portAndDeviceLayout->addWidget(serialSectionBox);
  17321. QFormLayout *deviceSection = new QFormLayout;
  17322. station->setMinimum(1);
  17323. station->setMaximum(255);
  17324. decimalPosition->setMinimum(0);
  17325. decimalPosition->setMaximum(9);
  17326. valueF->setMinimum(0);
  17327. valueF->setMaximum(0xFFFF);
  17328. valueC->setMinimum(0);
  17329. valueC->setMaximum(0xFFFF);
  17330. fixedUnit->addItem(tr("Fahrenheit"), QVariant(QString("F")));
  17331. fixedUnit->addItem(tr("Celsius"), QVariant(QString("C")));
  17332. deviceSection->addRow(tr("Station:"), station);
  17333. deviceSection->addRow(tr("Decimal position from device:"), decimalQuery);
  17334. deviceSection->addRow(tr("Decimal position relative address:"), decimalAddress);
  17335. deviceSection->addRow(tr("Fixed decimal position:"), decimalPosition);
  17336. deviceSection->addRow(tr("Measurement unit from device:"), unitQuery);
  17337. deviceSection->addRow(tr("Current unit relative address:"), unitAddress);
  17338. deviceSection->addRow(tr("Value for Fahrenheit:"), valueF);
  17339. deviceSection->addRow(tr("Value for Celsius:"), valueC);
  17340. deviceSection->addRow(tr("Fixed unit:"), fixedUnit);
  17341. QGroupBox *deviceSectionBox = new QGroupBox(tr("Device Configuration"));
  17342. deviceSectionBox->setLayout(deviceSection);
  17343. portAndDeviceLayout->addWidget(deviceSectionBox);
  17344. QFormLayout *pVSection = new QFormLayout;
  17345. pVSection->addRow(tr("Value relative address:"), pVAddress);
  17346. pVSection->addRow(tr("PV column name:"), pVColumnName);
  17347. QCheckBox *pVHideBox = new QCheckBox(tr("Hide this channel"));
  17348. pVSection->addRow(pVHideBox);
  17349. QGroupBox *processValueBox = new QGroupBox(tr("Process Value"));
  17350. processValueBox->setLayout(pVSection);
  17351. seriesLayout->addWidget(processValueBox);
  17352. QFormLayout *sVSection = new QFormLayout;
  17353. sVLower->setDecimals(1);
  17354. sVLower->setMinimum(0.0);
  17355. sVLower->setMaximum(999.9);
  17356. sVUpper->setDecimals(1);
  17357. sVUpper->setMinimum(0.0);
  17358. sVUpper->setMaximum(999.9);
  17359. sVSection->addRow(tr("Set value:"), sVEnabled);
  17360. sVSection->addRow(tr("Read relative address:"), sVReadAddress);
  17361. sVSection->addRow(tr("SV column name:"), sVColumnName);
  17362. sVSection->addRow(tr("Limits from device:"), deviceLimit);
  17363. sVSection->addRow(tr("Lower limit relative address:"), sVLowerAddr);
  17364. sVSection->addRow(tr("Upper limit relative address:"), sVUpperAddr);
  17365. sVSection->addRow(tr("Lower limit:"), sVLower);
  17366. sVSection->addRow(tr("Upper limit:"), sVUpper);
  17367. sVSection->addRow(tr("Output set value:"), sVWritable);
  17368. sVSection->addRow(tr("Output relative address:"), sVOutputAddr);
  17369. QCheckBox *sVHideBox = new QCheckBox(tr("Hide this channel"));
  17370. sVSection->addRow(sVHideBox);
  17371. QGroupBox *setValueBox = new QGroupBox(tr("Set Value"));
  17372. setValueBox->setLayout(sVSection);
  17373. seriesLayout->addWidget(setValueBox);
  17374. masterLayout->addLayout(portAndDeviceLayout);
  17375. masterLayout->addLayout(seriesLayout);
  17376. form->setLayout(masterLayout);
  17377. @<Get device configuration data for current node@>@;
  17378. for(int i = 0; i < configData.size(); i++)
  17379. {
  17380. node = configData.at(i).toElement();
  17381. if(node.attribute("name") == "port")
  17382. {
  17383. QString portname = node.attribute("value");
  17384. int idx = port->findText(portname);
  17385. if(idx >= 0)
  17386. {
  17387. port->setCurrentIndex(idx);
  17388. }
  17389. else
  17390. {
  17391. port->addItem(portname);
  17392. }
  17393. }
  17394. else if(node.attribute("name") == "baud")
  17395. {
  17396. baud->setCurrentIndex(baud->findText(node.attribute("value")));
  17397. }
  17398. else if(node.attribute("name") == "parity")
  17399. {
  17400. parity->setCurrentIndex(parity->findData(node.attribute("value")));
  17401. }
  17402. else if(node.attribute("name") == "flow")
  17403. {
  17404. flow->setCurrentIndex(flow->findData(node.attribute("value")));
  17405. }
  17406. else if(node.attribute("name") == "stop")
  17407. {
  17408. stop->setCurrentIndex(stop->findData(node.attribute("value")));
  17409. }
  17410. else if(node.attribute("name") == "station")
  17411. {
  17412. station->setValue(node.attribute("value").toInt());
  17413. }
  17414. else if(node.attribute("name") == "decimalQuery")
  17415. {
  17416. if(node.attribute("value") == "true")
  17417. {
  17418. decimalQuery->setChecked(true);
  17419. }
  17420. else
  17421. {
  17422. decimalQuery->setChecked(false);
  17423. }
  17424. }
  17425. else if(node.attribute("name") == "decimalAddress")
  17426. {
  17427. decimalAddress->setValue(node.attribute("value").toInt());
  17428. }
  17429. else if(node.attribute("name") == "decimalPosition")
  17430. {
  17431. decimalPosition->setValue(node.attribute("value").toInt());
  17432. }
  17433. else if(node.attribute("name") == "unitQuery")
  17434. {
  17435. if(node.attribute("value") == "true")
  17436. {
  17437. unitQuery->setChecked(true);
  17438. }
  17439. else
  17440. {
  17441. unitQuery->setChecked(false);
  17442. }
  17443. }
  17444. else if(node.attribute("name") == "unitAddress")
  17445. {
  17446. unitAddress->setValue(node.attribute("value").toInt());
  17447. }
  17448. else if(node.attribute("name") == "valueF")
  17449. {
  17450. valueF->setValue(node.attribute("value").toInt());
  17451. }
  17452. else if(node.attribute("name") == "valueC")
  17453. {
  17454. valueC->setValue(node.attribute("value").toInt());
  17455. }
  17456. else if(node.attribute("name") == "fixedUnit")
  17457. {
  17458. fixedUnit->setCurrentIndex(fixedUnit->findText(node.attribute("value")));
  17459. }
  17460. else if(node.attribute("name") == "pVAddress")
  17461. {
  17462. pVAddress->setValue(node.attribute("value").toInt());
  17463. }
  17464. else if(node.attribute("name") == "sVEnabled")
  17465. {
  17466. if(node.attribute("value") == "true")
  17467. {
  17468. sVEnabled->setChecked(true);
  17469. }
  17470. else
  17471. {
  17472. sVEnabled->setChecked(false);
  17473. }
  17474. }
  17475. else if(node.attribute("name") == "sVReadAddress")
  17476. {
  17477. sVReadAddress->setValue(node.attribute("value").toInt());
  17478. }
  17479. else if(node.attribute("name") == "deviceLimit")
  17480. {
  17481. if(node.attribute("value") == "true")
  17482. {
  17483. deviceLimit->setChecked(true);
  17484. }
  17485. else
  17486. {
  17487. deviceLimit->setChecked(false);
  17488. }
  17489. }
  17490. else if(node.attribute("name") == "sVLowerAddr")
  17491. {
  17492. sVLowerAddr->setValue(node.attribute("value").toInt());
  17493. }
  17494. else if(node.attribute("name") == "sVUpperAddr")
  17495. {
  17496. sVUpperAddr->setValue(node.attribute("value").toInt());
  17497. }
  17498. else if(node.attribute("name") == "sVLower")
  17499. {
  17500. sVLower->setValue(node.attribute("value").toDouble());
  17501. }
  17502. else if(node.attribute("name") == "sVUpper")
  17503. {
  17504. sVUpper->setValue(node.attribute("value").toDouble());
  17505. }
  17506. else if(node.attribute("name") == "sVWritable")
  17507. {
  17508. if(node.attribute("value") == "true")
  17509. {
  17510. sVWritable->setChecked(true);
  17511. }
  17512. else
  17513. {
  17514. sVWritable->setChecked(false);
  17515. }
  17516. }
  17517. else if(node.attribute("name") == "sVOutputAddr")
  17518. {
  17519. sVOutputAddr->setValue(node.attribute("value").toInt());
  17520. }
  17521. else if(node.attribute("name") == "pvcolname")
  17522. {
  17523. pVColumnName->setText(node.attribute("value"));
  17524. }
  17525. else if(node.attribute("name") == "svcolname")
  17526. {
  17527. sVColumnName->setText(node.attribute("value"));
  17528. }
  17529. else if(node.attribute("name") == "pvhidden")
  17530. {
  17531. pVHideBox->setChecked(node.attribute("value") == "true");
  17532. }
  17533. else if(node.attribute("name") == "svhidden")
  17534. {
  17535. sVHideBox->setChecked(node.attribute("value") == "true");
  17536. }
  17537. }
  17538. updatePort(port->currentText());
  17539. updateBaudRate(baud->currentText());
  17540. updateParity(parity->itemData(parity->currentIndex()).toString());
  17541. updateFlowControl(flow->itemData(flow->currentIndex()).toString());
  17542. updateStopBits(stop->itemData(stop->currentIndex()).toString());
  17543. updateStation(station->value());
  17544. updateFixedDecimal(decimalQuery->isChecked());
  17545. updateDecimalAddress(decimalAddress->value());
  17546. updateDecimalPosition(decimalPosition->value());
  17547. updateFixedUnit(unitQuery->isChecked());
  17548. updateUnitAddress(unitAddress->value());
  17549. updateValueForF(valueF->value());
  17550. updateValueForC(valueC->value());
  17551. updateUnit(fixedUnit->currentText());
  17552. updatePVAddress(pVAddress->value());
  17553. updateSVEnabled(sVEnabled->isChecked());
  17554. updateSVReadAddress(sVReadAddress->value());
  17555. updateDeviceLimit(deviceLimit->isChecked());
  17556. updateSVLowerAddress(sVLowerAddr->value());
  17557. updateSVUpperAddress(sVUpperAddr->value());
  17558. updateSVLower(sVLower->value());
  17559. updateSVUpper(sVUpper->value());
  17560. updateSVWritable(sVWritable->isChecked());
  17561. updateSVWriteAddress(sVOutputAddr->value());
  17562. updatePVColumnName(pVColumnName->text());
  17563. updateSVColumnName(sVColumnName->text());
  17564. updatePVHidden(pVHideBox->isChecked());
  17565. updateSVHidden(sVHideBox->isChecked());
  17566. connect(port, SIGNAL(currentIndexChanged(QString)), this, SLOT(updatePort(QString)));
  17567. connect(port, SIGNAL(editTextChanged(QString)), this, SLOT(updatePort(QString)));
  17568. connect(baud, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateBaudRate(QString)));
  17569. connect(parity, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateParity(QString)));
  17570. connect(flow, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateFlowControl(QString)));
  17571. connect(stop, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateStopBits(QString)));
  17572. connect(station, SIGNAL(valueChanged(int)), this, SLOT(updateStation(int)));
  17573. connect(decimalQuery, SIGNAL(toggled(bool)), this, SLOT(updateFixedDecimal(bool)));
  17574. connect(decimalAddress, SIGNAL(valueChanged(int)), this, SLOT(updateDecimalAddress(int)));
  17575. connect(decimalPosition, SIGNAL(valueChanged(int)), this, SLOT(updateDecimalPosition(int)));
  17576. connect(unitQuery, SIGNAL(toggled(bool)), this, SLOT(updateFixedUnit(bool)));
  17577. connect(unitAddress, SIGNAL(valueChanged(int)), this, SLOT(updateUnitAddress(int)));
  17578. connect(valueF, SIGNAL(valueChanged(int)), this, SLOT(updateValueForF(int)));
  17579. connect(valueC, SIGNAL(valueChanged(int)), this, SLOT(updateValueForC(int)));
  17580. connect(fixedUnit, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateUnit(QString)));
  17581. connect(pVAddress, SIGNAL(valueChanged(int)), this, SLOT(updatePVAddress(int)));
  17582. connect(sVEnabled, SIGNAL(toggled(bool)), this, SLOT(updateSVEnabled(bool)));
  17583. connect(sVReadAddress, SIGNAL(valueChanged(int)), this, SLOT(updateSVReadAddress(int)));
  17584. connect(deviceLimit, SIGNAL(toggled(bool)), this, SLOT(updateDeviceLimit(bool)));
  17585. connect(sVLowerAddr, SIGNAL(valueChanged(int)), this, SLOT(updateSVLowerAddress(int)));
  17586. connect(sVUpperAddr, SIGNAL(valueChanged(int)), this, SLOT(updateSVUpperAddress(int)));
  17587. connect(sVLower, SIGNAL(valueChanged(double)), this, SLOT(updateSVLower(double)));
  17588. connect(sVUpper, SIGNAL(valueChanged(double)), this, SLOT(updateSVUpper(double)));
  17589. connect(sVWritable, SIGNAL(toggled(bool)), this, SLOT(updateSVWritable(bool)));
  17590. connect(sVOutputAddr, SIGNAL(valueChanged(int)), this, SLOT(updateSVWriteAddress(int)));
  17591. connect(pVColumnName, SIGNAL(textEdited(QString)), this, SLOT(updatePVColumnName(QString)));
  17592. connect(sVColumnName, SIGNAL(textEdited(QString)), this, SLOT(updateSVColumnName(QString)));
  17593. connect(pVHideBox, SIGNAL(toggled(bool)), this, SLOT(updatePVHidden(bool)));
  17594. connect(sVHideBox, SIGNAL(toggled(bool)), this, SLOT(updateSVHidden(bool)));
  17595. layout->addWidget(form);
  17596. setLayout(layout);
  17597. }
  17598. void ModbusConfigurator::updatePort(const QString &newPort)
  17599. {
  17600. updateAttribute("port", newPort);
  17601. }
  17602. void ModbusConfigurator::updateBaudRate(const QString &newRate)
  17603. {
  17604. updateAttribute("baud", newRate);
  17605. }
  17606. void ModbusConfigurator::updateParity(const QString &)
  17607. {
  17608. updateAttribute("parity", parity->itemData(parity->currentIndex()).toString());
  17609. }
  17610. void ModbusConfigurator::updateFlowControl(const QString &)
  17611. {
  17612. updateAttribute("flow", flow->itemData(flow->currentIndex()).toString());
  17613. }
  17614. void ModbusConfigurator::updateStopBits(const QString &)
  17615. {
  17616. updateAttribute("stop", stop->itemData(stop->currentIndex()).toString());
  17617. }
  17618. void ModbusConfigurator::updateStation(int station)
  17619. {
  17620. updateAttribute("station", QString("%1").arg(station));
  17621. }
  17622. void ModbusConfigurator::updateFixedDecimal(bool fixed)
  17623. {
  17624. updateAttribute("decimalQuery", fixed ? "true" : "false");
  17625. }
  17626. void ModbusConfigurator::updateDecimalAddress(int address)
  17627. {
  17628. updateAttribute("decimalAddress", QString("%1").arg(address));
  17629. }
  17630. void ModbusConfigurator::updateDecimalPosition(int position)
  17631. {
  17632. updateAttribute("decimalPosition", QString("%1").arg(position));
  17633. }
  17634. void ModbusConfigurator::updateFixedUnit(bool fixed)
  17635. {
  17636. updateAttribute("unitQuery", fixed ? "true" : "false");
  17637. }
  17638. void ModbusConfigurator::updateUnitAddress(int address)
  17639. {
  17640. updateAttribute("unitAddress", QString("%1").arg(address));
  17641. }
  17642. void ModbusConfigurator::updateValueForF(int value)
  17643. {
  17644. updateAttribute("valueF", QString("%1").arg(value));
  17645. }
  17646. void ModbusConfigurator::updateValueForC(int value)
  17647. {
  17648. updateAttribute("valueC", QString("%1").arg(value));
  17649. }
  17650. void ModbusConfigurator::updateUnit(const QString &newUnit)
  17651. {
  17652. updateAttribute("fixedUnit", newUnit);
  17653. }
  17654. void ModbusConfigurator::updatePVAddress(int address)
  17655. {
  17656. updateAttribute("pVAddress", QString("%1").arg(address));
  17657. }
  17658. void ModbusConfigurator::updateSVEnabled(bool enabled)
  17659. {
  17660. updateAttribute("sVEnabled", enabled ? "true" : "false");
  17661. }
  17662. void ModbusConfigurator::updateSVReadAddress(int address)
  17663. {
  17664. updateAttribute("sVReadAddress", QString("%1").arg(address));
  17665. }
  17666. void ModbusConfigurator::updateDeviceLimit(bool query)
  17667. {
  17668. updateAttribute("deviceLimit", query ? "true" : "false");
  17669. }
  17670. void ModbusConfigurator::updateSVLowerAddress(int address)
  17671. {
  17672. updateAttribute("sVLowerAddr", QString("%1").arg(address));
  17673. }
  17674. void ModbusConfigurator::updateSVUpperAddress(int address)
  17675. {
  17676. updateAttribute("sVUpperAddr", QString("%1").arg(address));
  17677. }
  17678. void ModbusConfigurator::updateSVLower(double value)
  17679. {
  17680. updateAttribute("sVLower", QString("%1").arg(value));
  17681. }
  17682. void ModbusConfigurator::updateSVUpper(double value)
  17683. {
  17684. updateAttribute("sVUpper", QString("%1").arg(value));
  17685. }
  17686. void ModbusConfigurator::updateSVWritable(bool canWriteSV)
  17687. {
  17688. updateAttribute("sVWritable", canWriteSV ? "true" : "false");
  17689. }
  17690. void ModbusConfigurator::updateSVWriteAddress(int address)
  17691. {
  17692. updateAttribute("sVOutputAddr", QString("%1").arg(address));
  17693. }
  17694. void ModbusConfigurator::updatePVColumnName(const QString &name)
  17695. {
  17696. updateAttribute("pvcolname", name);
  17697. }
  17698. void ModbusConfigurator::updateSVColumnName(const QString &name)
  17699. {
  17700. updateAttribute("svcolname", name);
  17701. }
  17702. void ModbusConfigurator::updatePVHidden(bool hidden)
  17703. {
  17704. updateAttribute("pvhidden", hidden ? "true" : "false");
  17705. }
  17706. void ModbusConfigurator::updateSVHidden(bool hidden)
  17707. {
  17708. updateAttribute("svhidden", hidden ? "true" : "false");
  17709. }
  17710. @ This is registered with the configuration system.
  17711. @<Register device configuration widgets@>=
  17712. app.registerDeviceConfigurationWidget("modbusrtu", ModbusConfigurator::staticMetaObject);
  17713. @ A |NodeInserter| for the driver configuration widget is also needed.
  17714. @<Register top level device configuration nodes@>=
  17715. inserter = new NodeInserter(tr("Modbus RTU Device"), tr("Modbus RTU Device"), "modbusrtu", NULL);
  17716. topLevelNodeInserters.append(inserter);
  17717. @i modbus.w
  17718. @i unsupportedserial.w
  17719. @i phidgets.w
  17720. @i phidget22.w
  17721. @* Configuration widget for a calibrated data series.
  17722. \noindent This control is used for adding a |LinearSplineInterpolator| to the
  17723. logging view.
  17724. @<Class declarations@>=
  17725. class LinearSplineInterpolationConfWidget : public BasicDeviceConfigurationWidget
  17726. {
  17727. @[Q_OBJECT@]@/
  17728. public:@/
  17729. @[Q_INVOKABLE@]@, LinearSplineInterpolationConfWidget(DeviceTreeModel *model,
  17730. const QModelIndex &index);
  17731. @[private slots@]:@/
  17732. void updateSourceColumn(const QString &source);
  17733. void updateDestinationColumn(const QString &dest);
  17734. void updateKnots();
  17735. private:@/
  17736. SaltModel *tablemodel;
  17737. };
  17738. @ This is configured by specifying a source column name, a destination column
  17739. name, and a two column table. Note that while we only have one widget handling
  17740. the mapping data, we store each column of the table in its own attribute.
  17741. @<LinearSplineInterpolationConfWidget implementation@>=
  17742. LinearSplineInterpolationConfWidget::LinearSplineInterpolationConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  17743. : BasicDeviceConfigurationWidget(model, index), tablemodel(new SaltModel(2))
  17744. {
  17745. QFormLayout *layout = new QFormLayout;
  17746. QLineEdit *source = new QLineEdit;
  17747. layout->addRow(tr("Source column name:"), source);
  17748. QLineEdit *destination = new QLineEdit;
  17749. layout->addRow(tr("Destination column name:"), destination);
  17750. tablemodel->setHeaderData(0, Qt::Horizontal, "Input");
  17751. tablemodel->setHeaderData(1, Qt::Horizontal, "Output");
  17752. QTableView *mappingTable = new QTableView;
  17753. mappingTable->setModel(tablemodel);
  17754. NumericDelegate *delegate = new NumericDelegate;
  17755. mappingTable->setItemDelegate(delegate);
  17756. layout->addRow(tr("Mapping data:"), mappingTable);
  17757. @<Get device configuration data for current node@>@;
  17758. for(int i = 0; i < configData.size(); i++)
  17759. {
  17760. node = configData.at(i).toElement();
  17761. if(node.attribute("name") == "source")
  17762. {
  17763. source->setText(node.attribute("value"));
  17764. }
  17765. else if(node.attribute("name") == "destination")
  17766. {
  17767. destination->setText(node.attribute("value"));
  17768. }
  17769. else if(node.attribute("name") == "sourcevalues")
  17770. {
  17771. @<Convert numeric array literal to list@>@;
  17772. int column = 0;
  17773. @<Populate model column from list@>@;
  17774. }
  17775. else if(node.attribute("name") == "destinationvalues")
  17776. {
  17777. @<Convert numeric array literal to list@>@;
  17778. int column = 1;
  17779. @<Populate model column from list@>@;
  17780. }
  17781. }
  17782. updateSourceColumn(source->text());
  17783. updateDestinationColumn(destination->text());
  17784. updateKnots();
  17785. connect(source, SIGNAL(textEdited(QString)), this, SLOT(updateSourceColumn(QString)));
  17786. connect(destination, SIGNAL(textEdited(QString)), this, SLOT(updateDestinationColumn(QString)));
  17787. connect(tablemodel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(updateKnots()));
  17788. setLayout(layout);
  17789. }
  17790. @ The saved data will have come from a previous call to
  17791. |SaltModel::arrayLiteral()| to repopulate the model we need to strip off the
  17792. the start and end of the strings and split them back into separate elements.
  17793. @<Convert numeric array literal to list@>=
  17794. QString data = node.attribute("value");
  17795. if(data.length() > 3)
  17796. {
  17797. data.chop(2);
  17798. data = data.remove(0, 2);
  17799. }
  17800. QStringList itemList = data.split(",");
  17801. @ Once the saved string has been split, the values can be inserted into the
  17802. model.
  17803. @<Populate model column from list@>=
  17804. for(int j = 0; j < itemList.size(); j++)
  17805. {
  17806. tablemodel->setData(tablemodel->index(j, column),
  17807. QVariant(itemList.at(j).toDouble()),
  17808. Qt::DisplayRole);
  17809. }
  17810. @ When data in the table is changed we simply overwrite any previously saved
  17811. data with the current data.
  17812. @<LinearSplineInterpolationConfWidget implementation@>=
  17813. void LinearSplineInterpolationConfWidget::updateKnots()
  17814. {
  17815. updateAttribute("sourcevalues", tablemodel->arrayLiteral(0, Qt::DisplayRole));
  17816. updateAttribute("destinationvalues", tablemodel->arrayLiteral(1, Qt::DisplayRole));
  17817. }
  17818. void LinearSplineInterpolationConfWidget::updateSourceColumn(const QString &source)
  17819. {
  17820. updateAttribute("source", source);
  17821. }
  17822. void LinearSplineInterpolationConfWidget::updateDestinationColumn(const QString &dest)
  17823. {
  17824. updateAttribute("destination", dest);
  17825. }
  17826. @ The widget is registered with the configuration system.
  17827. @<Register device configuration widgets@>=
  17828. app.registerDeviceConfigurationWidget("linearspline", LinearSplineInterpolationConfWidget::staticMetaObject);
  17829. @* Additional Timers.
  17830. \noindent \pn{} 1.6.4 adds support for more timer indicators than just the
  17831. default batch timer. Three new timer types are supported. The first is a
  17832. cooling timer. This is a timer that is initially set to 0, but at the end of a
  17833. batch this is set to a configured time and starts counting down. There are no
  17834. data logging requirements and this is purely a convenience feature, but one
  17835. that supports product and personnel safety best practices as it helps to
  17836. eliminate the practice of a person reaching into the cooling tray as it
  17837. agitates to test if the coffee has cooled.
  17838. @<Class declarations@>=
  17839. class CoolingTimerConfWidget : public BasicDeviceConfigurationWidget
  17840. {
  17841. @[Q_OBJECT@]@/
  17842. public:@/
  17843. @[Q_INVOKABLE@]@, CoolingTimerConfWidget(DeviceTreeModel *model,
  17844. const QModelIndex &index);
  17845. @[private slots@]:@/
  17846. void updateResetTime(QTime time);
  17847. };
  17848. @ The only configurable detail is the vaue the timer should reset to. For this
  17849. a |QTimeEdit| is fine.
  17850. @<CoolingTimerConfWidget implementation@>=
  17851. CoolingTimerConfWidget::CoolingTimerConfWidget(DeviceTreeModel *model,
  17852. const QModelIndex &index)
  17853. : BasicDeviceConfigurationWidget(model, index)
  17854. {
  17855. QHBoxLayout *layout = new QHBoxLayout;
  17856. QLabel *label = new QLabel(tr("Cooling Time: "));
  17857. QTimeEdit *editor = new QTimeEdit;
  17858. editor->setDisplayFormat("mm:ss");
  17859. @<Get device configuration data for current node@>@;
  17860. for(int i = 0; i < configData.size(); i++)
  17861. {
  17862. node = configData.at(i).toElement();
  17863. if(node.attribute("name") == "reset")
  17864. {
  17865. editor->setTime(QTime::fromString(node.attribute("value"), "mm:ss"));
  17866. }
  17867. }
  17868. updateResetTime(editor->time());
  17869. connect(editor, SIGNAL(timeChanged(QTime)),
  17870. this, SLOT(updateResetTime(QTime)));
  17871. layout->addWidget(label);
  17872. layout->addWidget(editor);
  17873. setLayout(layout);
  17874. }
  17875. void CoolingTimerConfWidget::updateResetTime(QTime time)
  17876. {
  17877. updateAttribute("reset", time.toString("mm:ss"));
  17878. }
  17879. @ The widget is registered with the configuration system.
  17880. @<Register device configuration widgets@>=
  17881. app.registerDeviceConfigurationWidget("coolingtimer",
  17882. CoolingTimerConfWidget::staticMetaObject);
  17883. @ The implementation chunk for now is in the main source file.
  17884. @<Class implementations@>=
  17885. @<CoolingTimerConfWidget implementation@>
  17886. @ The other two timer types are intended for measuring ranges of interest
  17887. within a batch. These have more configuration options. First is the range
  17888. timer. Someone setting this up needs to decide how the timer is started.
  17889. Sensible options include starting the timer at the start of the batch,
  17890. starting the timer when a button is pressed, or starting the timer when a set
  17891. point is reached on the ascent of a named data series. Stopping the timer is
  17892. also a configurable concern. This will be stopped at the end of the batch,
  17893. but it might be stopped sooner on reaching some threshold or when a button is
  17894. pressed. There are also questions of how the information is persisted with the
  17895. roasting records.
  17896. @<Class declarations@>=
  17897. class RangeTimerConfWidget : public BasicDeviceConfigurationWidget
  17898. {
  17899. @[Q_OBJECT@]@;
  17900. public:@/
  17901. @[Q_INVOKABLE@]@, RangeTimerConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  17902. @[private slots@]:@/
  17903. void updateStartButtonText(const QString &text);
  17904. void updateStopButtonText(const QString &text);
  17905. void updateStartColumnName(const QString &text);
  17906. void updateStopColumnName(const QString &text);
  17907. void updateStartValue(const QString &text);
  17908. void updateStopValue(const QString &text);
  17909. void updateStartTrigger(int option);
  17910. void updateStopTrigger(int option);
  17911. };
  17912. @ The constructor sets up controls for configuring these details.
  17913. @<RangeTimerConfWidget implementation@>=
  17914. RangeTimerConfWidget::RangeTimerConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  17915. : BasicDeviceConfigurationWidget(model, index)
  17916. {
  17917. QVBoxLayout *layout = new QVBoxLayout;
  17918. QGroupBox *startConfigurationGroup = new QGroupBox(tr("Start trigger"));
  17919. QRadioButton *startBatchOption = new QRadioButton(tr("Start of batch"));
  17920. QRadioButton *buttonOption = new QRadioButton(tr("Manual"));
  17921. QRadioButton *thresholdOption = new QRadioButton(tr("At temperature"));
  17922. QButtonGroup *startOptionGroup = new QButtonGroup;
  17923. startOptionGroup->addButton(startBatchOption, 1);
  17924. startOptionGroup->addButton(buttonOption, 2);
  17925. startOptionGroup->addButton(thresholdOption, 3);
  17926. startBatchOption->setChecked(true);
  17927. QGridLayout *startOptions = new QGridLayout;
  17928. startOptions->addWidget(startBatchOption, 0, 0);
  17929. startOptions->addWidget(buttonOption, 1, 0);
  17930. startOptions->addWidget(thresholdOption, 2, 0);
  17931. QLabel *buttonTextLabel = new QLabel(tr("Button Text: "));
  17932. QLineEdit *buttonTextEdit = new QLineEdit;
  17933. QHBoxLayout *buttonTextOptions = new QHBoxLayout;
  17934. buttonTextOptions->addWidget(buttonTextLabel);
  17935. buttonTextOptions->addWidget(buttonTextEdit);
  17936. startOptions->addLayout(buttonTextOptions, 1, 1);
  17937. QFormLayout *thresholdOptions = new QFormLayout;
  17938. QLineEdit *startColumnName = new QLineEdit;
  17939. QLineEdit *startValue = new QLineEdit;
  17940. thresholdOptions->addRow(tr("Column Name: "), startColumnName);
  17941. thresholdOptions->addRow(tr("Value: "), startValue);
  17942. startOptions->addLayout(thresholdOptions, 2, 1);
  17943. startConfigurationGroup->setLayout(startOptions);
  17944. layout->addWidget(startConfigurationGroup);
  17945. QGroupBox *stopConfigurationGroup = new QGroupBox(tr("Stop trigger"));
  17946. QRadioButton *stopBatchOption = new QRadioButton(tr("End of batch"));
  17947. QRadioButton *stopButtonOption = new QRadioButton(tr("Manual"));
  17948. QRadioButton *stopThresholdOption = new QRadioButton(tr("At temperature"));
  17949. QButtonGroup *stopOptionGroup = new QButtonGroup;
  17950. stopOptionGroup->addButton(stopBatchOption, 1);
  17951. stopOptionGroup->addButton(stopButtonOption, 2);
  17952. stopOptionGroup->addButton(stopThresholdOption, 3);
  17953. stopBatchOption->setChecked(true);
  17954. QGridLayout *stopOptions = new QGridLayout;
  17955. stopOptions->addWidget(stopBatchOption, 0, 0);
  17956. stopOptions->addWidget(stopButtonOption, 1, 0);
  17957. stopOptions->addWidget(stopThresholdOption, 2, 0);
  17958. QLabel *stopButtonLabel = new QLabel(tr("Button Text: "));
  17959. QLineEdit *stopButtonEdit = new QLineEdit;
  17960. QHBoxLayout *stopButtonTextOptions = new QHBoxLayout;
  17961. stopButtonTextOptions->addWidget(stopButtonLabel);
  17962. stopButtonTextOptions->addWidget(stopButtonEdit);
  17963. stopOptions->addLayout(stopButtonTextOptions, 1, 1);
  17964. QLineEdit *stopColumnName = new QLineEdit;
  17965. QLineEdit *stopValue = new QLineEdit;
  17966. QFormLayout *stopThresholdOptions = new QFormLayout;
  17967. stopThresholdOptions->addRow(tr("Column Name: "), stopColumnName);
  17968. stopThresholdOptions->addRow(tr("Value: "), stopValue);
  17969. stopOptions->addLayout(stopThresholdOptions, 2, 1);
  17970. stopConfigurationGroup->setLayout(stopOptions);
  17971. layout->addWidget(stopConfigurationGroup);
  17972. @<Get device configuration data for current node@>@;
  17973. for(int i = 0; i < configData.size(); i++)
  17974. {
  17975. node = configData.at(i).toElement();
  17976. if(node.attribute("name") == "startbuttontext")
  17977. {
  17978. buttonTextEdit->setText(node.attribute("value"));
  17979. }
  17980. else if(node.attribute("name") == "stopbuttontext")
  17981. {
  17982. stopButtonEdit->setText(node.attribute("value"));
  17983. }
  17984. else if(node.attribute("name") == "startcolumnname")
  17985. {
  17986. startColumnName->setText(node.attribute("value"));
  17987. }
  17988. else if(node.attribute("name") == "stopcolumnname")
  17989. {
  17990. stopColumnName->setText(node.attribute("value"));
  17991. }
  17992. else if(node.attribute("name") == "startvalue")
  17993. {
  17994. startValue->setText(node.attribute("value"));
  17995. }
  17996. else if(node.attribute("name") == "stopvalue")
  17997. {
  17998. stopValue->setText(node.attribute("value"));
  17999. }
  18000. else if(node.attribute("name") == "starttrigger")
  18001. {
  18002. if(node.attribute("value") == "batch")
  18003. {
  18004. startBatchOption->setChecked(true);
  18005. }
  18006. else if(node.attribute("value") == "manual")
  18007. {
  18008. buttonOption->setChecked(true);
  18009. }
  18010. else if(node.attribute("value") == "value")
  18011. {
  18012. thresholdOption->setChecked(true);
  18013. }
  18014. }
  18015. else if(node.attribute("name") == "stoptrigger")
  18016. {
  18017. if(node.attribute("value") == "batch")
  18018. {
  18019. stopBatchOption->setChecked(true);
  18020. }
  18021. else if(node.attribute("value") == "manual")
  18022. {
  18023. stopButtonOption->setChecked(true);
  18024. }
  18025. else if(node.attribute("value") == "value")
  18026. {
  18027. stopThresholdOption->setChecked(true);
  18028. }
  18029. }
  18030. }
  18031. updateStartButtonText(buttonTextEdit->text());
  18032. updateStopButtonText(stopButtonEdit->text());
  18033. updateStartColumnName(startColumnName->text());
  18034. updateStopColumnName(stopColumnName->text());
  18035. updateStartValue(startValue->text());
  18036. updateStopValue(stopValue->text());
  18037. updateStartTrigger(startOptionGroup->checkedId());
  18038. updateStopTrigger(stopOptionGroup->checkedId());
  18039. setLayout(layout);
  18040. connect(buttonTextEdit, SIGNAL(textChanged(QString)),
  18041. this, SLOT(updateStartButtonText(QString)));
  18042. connect(stopButtonEdit, SIGNAL(textChanged(QString)),
  18043. this, SLOT(updateStopButtonText(QString)));
  18044. connect(startColumnName, SIGNAL(textChanged(QString)),
  18045. this, SLOT(updateStartColumnName(QString)));
  18046. connect(stopColumnName, SIGNAL(textChanged(QString)),
  18047. this, SLOT(updateStopColumnName(QString)));
  18048. connect(startValue, SIGNAL(textChanged(QString)),
  18049. this, SLOT(updateStartValue(QString)));
  18050. connect(stopValue, SIGNAL(textChanged(QString)),
  18051. this, SLOT(updateStopValue(QString)));
  18052. connect(startOptionGroup, SIGNAL(buttonClicked(int)),
  18053. this, SLOT(updateStartTrigger(int)));
  18054. connect(stopOptionGroup, SIGNAL(buttonClicked(int)),
  18055. this, SLOT(updateStopTrigger(int)));
  18056. }
  18057. @ Small methods update the configuration as usual.
  18058. @<RangeTimerConfWidget implementation@>=
  18059. void RangeTimerConfWidget::updateStartButtonText(const QString &text)
  18060. {
  18061. updateAttribute("startbuttontext", text);
  18062. }
  18063. void RangeTimerConfWidget::updateStopButtonText(const QString &text)
  18064. {
  18065. updateAttribute("stopbuttontext", text);
  18066. }
  18067. void RangeTimerConfWidget::updateStartColumnName(const QString &text)
  18068. {
  18069. updateAttribute("startcolumnname", text);
  18070. }
  18071. void RangeTimerConfWidget::updateStopColumnName(const QString &text)
  18072. {
  18073. updateAttribute("stopcolumnname", text);
  18074. }
  18075. void RangeTimerConfWidget::updateStartValue(const QString &text)
  18076. {
  18077. updateAttribute("startvalue", text);
  18078. }
  18079. void RangeTimerConfWidget::updateStopValue(const QString &text)
  18080. {
  18081. updateAttribute("stopvalue", text);
  18082. }
  18083. void RangeTimerConfWidget::updateStartTrigger(int option)
  18084. {
  18085. switch(option)
  18086. {
  18087. case 1:
  18088. updateAttribute("starttrigger", "batch");
  18089. break;
  18090. case 2:
  18091. updateAttribute("starttrigger", "manual");
  18092. break;
  18093. case 3:
  18094. updateAttribute("starttrigger", "value");
  18095. break;
  18096. default:
  18097. break;
  18098. }
  18099. }
  18100. void RangeTimerConfWidget::updateStopTrigger(int option)
  18101. {
  18102. switch(option)
  18103. {
  18104. case 1:
  18105. updateAttribute("stoptrigger", "batch");
  18106. break;
  18107. case 2:
  18108. updateAttribute("stoptrigger", "manual");
  18109. break;
  18110. case 3:
  18111. updateAttribute("stoptrigger", "value");
  18112. break;
  18113. default:
  18114. break;
  18115. }
  18116. }
  18117. @ The widget is registered with the configuration system.
  18118. @<Register device configuration widgets@>=
  18119. app.registerDeviceConfigurationWidget("rangetimer",
  18120. RangeTimerConfWidget::staticMetaObject);
  18121. @ The implementation chunk for now is in the main source file.
  18122. @<Class implementations@>=
  18123. @<RangeTimerConfWidget implementation@>
  18124. @ The multirange timer is a little different. To keep configuration tractible,
  18125. this is slightly less general purpose than the range timer and only supports
  18126. automatic triggering on a single data series.
  18127. @<Class declarations@>=
  18128. class MultiRangeTimerConfWidget : public BasicDeviceConfigurationWidget
  18129. {
  18130. @[Q_OBJECT@]@/
  18131. public:@/
  18132. @[Q_INVOKABLE@]@, MultiRangeTimerConfWidget(DeviceTreeModel *model,
  18133. const QModelIndex &index);
  18134. @[private slots@]:@/
  18135. void updateColumnName(const QString &text);
  18136. void updateRangeData();
  18137. private:@/
  18138. SaltModel *tablemodel;
  18139. };
  18140. @ These limitations allow a rather small set of controls for configuration. A
  18141. line edit to specify the data series used for automatic triggering and a table
  18142. which specifies the name of the timed range and an ending temperature.
  18143. @<MultiRangeTimerConfWidget implementation@>=
  18144. MultiRangeTimerConfWidget::MultiRangeTimerConfWidget(DeviceTreeModel *model,
  18145. const QModelIndex &index)
  18146. : BasicDeviceConfigurationWidget(model, index), tablemodel(new SaltModel(2))
  18147. {
  18148. QFormLayout *layout = new QFormLayout;
  18149. QLineEdit *trigger = new QLineEdit;
  18150. layout->addRow(tr("Trigger column name:"), trigger);
  18151. tablemodel->setHeaderData(0, Qt::Horizontal, "Range Name");
  18152. tablemodel->setHeaderData(1, Qt::Horizontal, "End Temperature");
  18153. QTableView *rangeTable = new QTableView;
  18154. rangeTable->setModel(tablemodel);
  18155. layout->addRow(tr("Range data:"), rangeTable);
  18156. @<Get device configuration data for current node@>@;
  18157. for(int i = 0; i < configData.size(); i++)
  18158. {
  18159. node = configData.at(i).toElement();
  18160. if(node.attribute("name") == "trigger")
  18161. {
  18162. trigger->setText(node.attribute("value"));
  18163. }
  18164. else if(node.attribute("name") == "rangenames")
  18165. {
  18166. QString data = node.attribute("value");
  18167. if(data.length() > 3)
  18168. {
  18169. data.chop(2);
  18170. data = data.remove(0, 2);
  18171. }
  18172. QStringList itemList = data.split(", ");
  18173. for(int j = 0; j < itemList.size(); j++)
  18174. {
  18175. QString item = itemList.at(j);
  18176. item.chop(1);
  18177. item = item.remove(0, 1);
  18178. tablemodel->setData(tablemodel->index(j, 0),
  18179. QVariant(item), Qt::DisplayRole);
  18180. }
  18181. }
  18182. else if(node.attribute("name") == "endtemps")
  18183. {
  18184. @<Convert numeric array literal to list@>@;
  18185. int column = 1;
  18186. @<Populate model column from list@>@;
  18187. }
  18188. }
  18189. updateColumnName(trigger->text());
  18190. updateRangeData();
  18191. connect(trigger, SIGNAL(textEdited(QString)), this, SLOT(updateColumnName(QString)));
  18192. connect(tablemodel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(updateRangeData()));
  18193. setLayout(layout);
  18194. }
  18195. @ The update mechanisms are reasonably straightforward. Table updates just
  18196. refresh the entire table instead of attempting to be clever about updating only
  18197. the element that changed.
  18198. @<MultiRangeTimerConfWidget implementation@>=
  18199. void MultiRangeTimerConfWidget::updateRangeData()
  18200. {
  18201. updateAttribute("rangenames", tablemodel->quotedArrayLiteral(0, Qt::DisplayRole));
  18202. updateAttribute("endtemps", tablemodel->arrayLiteral(1, Qt::DisplayRole));
  18203. }
  18204. void MultiRangeTimerConfWidget::updateColumnName(const QString &text)
  18205. {
  18206. updateAttribute("trigger", text);
  18207. }
  18208. @ The widget is registered with the configuration system.
  18209. @<Register device configuration widgets@>=
  18210. app.registerDeviceConfigurationWidget("multirangetimer",
  18211. MultiRangeTimerConfWidget::staticMetaObject);
  18212. @ The implementation is in the main source file.
  18213. @<Class implementations@>=
  18214. @<MultiRangeTimerConfWidget implementation@>
  18215. @* Profile Translation Configuration Widget.
  18216. \noindent Configuring profile translation requires knowing which column to use
  18217. for matching purposes and the value to match.
  18218. @<Class declarations@>=
  18219. class TranslationConfWidget : public BasicDeviceConfigurationWidget
  18220. {
  18221. @[Q_OBJECT@]@;
  18222. public:@/
  18223. @[Q_INVOKABLE@]@, TranslationConfWidget(DeviceTreeModel *model, const QModelIndex &index);
  18224. @[private slots@]:@/
  18225. void updateMatchingColumn(const QString &column);
  18226. void updateTemperature();
  18227. void updateDelay();
  18228. private:@/
  18229. QDoubleSpinBox *temperatureValue;
  18230. QComboBox *unitSelector;
  18231. QSpinBox *delaySelector;
  18232. };
  18233. @ The constructor sets up our user interface.
  18234. @<TranslationConfWidget implementation@>=
  18235. TranslationConfWidget::TranslationConfWidget(DeviceTreeModel *model, const QModelIndex &index)
  18236. : BasicDeviceConfigurationWidget(model, index),
  18237. temperatureValue(new QDoubleSpinBox), unitSelector(new QComboBox),
  18238. delaySelector(new QSpinBox)
  18239. {
  18240. unitSelector->addItem("Fahrenheit");
  18241. unitSelector->addItem("Celsius");
  18242. temperatureValue->setMinimum(0);
  18243. temperatureValue->setMaximum(1000);
  18244. QFormLayout *layout = new QFormLayout;
  18245. QLineEdit *column = new QLineEdit;
  18246. layout->addRow(tr("Column to match:"), column);
  18247. layout->addRow(tr("Unit:"), unitSelector);
  18248. layout->addRow(tr("Value:"), temperatureValue);
  18249. layout->addRow(tr("Start of batch safety delay:"), delaySelector);
  18250. @<Get device configuration data for current node@>@;
  18251. for(int i = 0; i < configData.size(); i++)
  18252. {
  18253. node = configData.at(i).toElement();
  18254. if(node.attribute("name") == "column")
  18255. {
  18256. column->setText(node.attribute("value"));
  18257. }
  18258. else if(node.attribute("name") == "unit")
  18259. {
  18260. unitSelector->setCurrentIndex(unitSelector->findText(node.attribute("value")));
  18261. }
  18262. else if(node.attribute("name") == "value")
  18263. {
  18264. temperatureValue->setValue(node.attribute("value").toDouble());
  18265. }
  18266. else if(node.attribute("name") == "delay")
  18267. {
  18268. delaySelector->setValue(node.attribute("value").toInt());
  18269. }
  18270. }
  18271. updateMatchingColumn(column->text());
  18272. updateTemperature();
  18273. updateDelay();
  18274. connect(column, SIGNAL(textEdited(QString)), this, SLOT(updateMatchingColumn(QString)));
  18275. connect(unitSelector, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateTemperature()));
  18276. connect(temperatureValue, SIGNAL(valueChanged(double)), this, SLOT(updateTemperature()));
  18277. connect(delaySelector, SIGNAL(valueChanged(int)), this, SLOT(updateDelay()));
  18278. setLayout(layout);
  18279. }
  18280. @ To update the temperature at which to match we save both the values of the
  18281. two widgets which control this and the value in Fahrenheit so we don'@q'@>t need to
  18282. run unit conversions during view initialization.
  18283. @<TranslationConfWidget implementation@>=
  18284. void TranslationConfWidget::updateTemperature()
  18285. {
  18286. updateAttribute("unit", unitSelector->currentText());
  18287. updateAttribute("value", QString("%1").arg(temperatureValue->value()));
  18288. if(unitSelector->currentText() == "Fahrenheit")
  18289. {
  18290. updateAttribute("FValue", QString("%1").arg(temperatureValue->value()));
  18291. }
  18292. else
  18293. {
  18294. updateAttribute("FValue", QString("%1").arg(temperatureValue->value() * 9 / 5 + 32));
  18295. }
  18296. }
  18297. void TranslationConfWidget::updateMatchingColumn(const QString &column)
  18298. {
  18299. updateAttribute("column", column);
  18300. }
  18301. void TranslationConfWidget::updateDelay()
  18302. {
  18303. updateAttribute("delay", QString("%1").arg(delaySelector->value()));
  18304. }
  18305. @ This is registered with the configuration system.
  18306. @<Register device configuration widgets@>=
  18307. app.registerDeviceConfigurationWidget("translation", TranslationConfWidget::staticMetaObject);
  18308. @i rate.w
  18309. @i mergeseries.w
  18310. @i dataqsdk.w
  18311. @i scales.w
  18312. @i valueannotation.w
  18313. @i thresholdannotation.w
  18314. @i user.w
  18315. @i roastcoloredit.w
  18316. @** Local changes.
  18317. \noindent This is the end of \pn{} as distributed by its author. It is expected
  18318. that some might have need of a program like \pn, but require some modification.
  18319. The patching capabilities of \cweb{} can be used to produce these local
  18320. modifications. This section is provided for those whose change requirements are
  18321. sufficiently extensive to require the introduction of new sections to the
  18322. program.
  18323. @** Index.
  18324. \noindent Following is a list of identifiers used in \pn, with underlined
  18325. entries pointing to where \cweb{} has guessed that the identifier was defined.
  18326. All references are to section numbers instead of page numbers.
  18327. \def\nullsec{--}