ImageLabeler.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493
  1. /*
  2. * ImageLabeler.cpp
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "ImageLabeler.h"
  8. #include "functions.h"
  9. #include <QApplication>
  10. #include <QFrame>
  11. #include <QMenuBar>
  12. #include <QMenu>
  13. #include <QAction>
  14. #include <QBoxLayout>
  15. #include <QGridLayout>
  16. #include <QPixmap>
  17. #include <QLabel>
  18. #include <QScrollArea>
  19. #include <QPushButton>
  20. #include <QButtonGroup>
  21. #include <QListWidget>
  22. #include <QListWidgetItem>
  23. #include <QDesktopWidget>
  24. #include <QFileDialog>
  25. #include <QColorDialog>
  26. #include <QDir>
  27. #include <QMessageBox>
  28. #include <QListIterator>
  29. #include <QDomDocument>
  30. #include <QFile>
  31. #include <QKeyEvent>
  32. #include <QDebug>
  33. ImageLabeler::ImageLabeler(QWidget *aParent) :
  34. QMainWindow(aParent)
  35. {
  36. /*
  37. * Variables
  38. */
  39. list_images_ = new QList< Image >;
  40. main_label_ = -1;
  41. pure_data_ = 0;
  42. //label_ID_ = -1;
  43. /* options */
  44. auto_color_generation_ = 0;
  45. /* flags */
  46. interrupt_search_ = 0;
  47. unsaved_data_ = 0;
  48. /*
  49. * menu bar part begins
  50. */
  51. menu_bar_ = new QMenuBar(this);
  52. setMenuBar(menu_bar_);
  53. menu_file_ = new QMenu(menu_bar_);
  54. menu_file_->setTitle(tr("&File"));
  55. menu_pascal_ = new QMenu(menu_bar_);
  56. menu_pascal_->setTitle(tr("&Pascal"));
  57. menu_view_ = new QMenu(menu_bar_);
  58. menu_view_->setTitle(tr("&View"));
  59. menu_edit_ = new QMenu(menu_bar_);
  60. menu_edit_->setTitle(tr("&Edit"));
  61. menu_help_ = new QMenu(menu_bar_);
  62. menu_help_->setTitle(tr("&Help"));
  63. /* menu file */
  64. action_open_image_ = new QAction(this);
  65. action_open_image_->setText(tr("&Load image"));
  66. action_open_images_ = new QAction(this);
  67. action_open_images_->setText(tr("&Load images(recursively)"));
  68. action_open_labeled_image_ = new QAction(this);
  69. action_open_labeled_image_->setText(tr("&Load labeled image"));
  70. action_load_legend_ = new QAction(this);
  71. action_load_legend_->setText(tr("&Load legend"));
  72. action_save_labels_ = new QAction(this);
  73. action_save_labels_->setText(tr("&Save all info"));
  74. action_save_labels_->setEnabled(false);
  75. action_save_segmented_ = new QAction(this);
  76. action_save_segmented_->setText(tr("Save segmented &picture"));
  77. action_save_segmented_->setEnabled(false);
  78. action_save_legend_ = new QAction(this);
  79. action_save_legend_->setText(tr("Save &legend"));
  80. action_save_legend_->setEnabled(false);
  81. action_quit_ = new QAction(this);
  82. action_quit_->setText(tr("&Quit"));
  83. /* menu pascal */
  84. action_load_pascal_file_ = new QAction(this);
  85. action_load_pascal_file_->setText(tr("&Load pascal file"));
  86. action_load_pascal_poly_ = new QAction(this);
  87. action_load_pascal_poly_->setText(tr("&Load poly info"));
  88. /* menu view */
  89. action_view_normal_ = new QAction(this);
  90. action_view_normal_->setText(tr("&Normal"));
  91. action_view_normal_->setEnabled(false);
  92. action_view_segmented_ = new QAction(this);
  93. action_view_segmented_->setText(tr("&Segmented"));
  94. action_view_segmented_->setEnabled(false);
  95. /* menu edit */
  96. action_undo_ = new QAction(this);
  97. action_undo_->setText(tr("&Undo"));
  98. action_undo_->setEnabled(false);
  99. action_redo_ = new QAction(this);
  100. action_redo_->setText(tr("&Redo"));
  101. action_redo_->setEnabled(false);
  102. action_bound_box_tool_ = new QAction(this);
  103. action_bound_box_tool_->setText(tr("Bounding box tool"));
  104. action_bound_box_tool_->setEnabled(false);
  105. action_polygon_tool_ = new QAction(this);
  106. action_polygon_tool_->setText(tr("&Polygon tool"));
  107. action_polygon_tool_->setEnabled(false);
  108. action_tagging_tool_ = new QAction(this);
  109. action_tagging_tool_->setText(tr("&Tagging tool"));
  110. action_tagging_tool_->setEnabled(false);
  111. action_add_description_ = new QAction(this);
  112. action_add_description_->setText(tr("&Add image description"));
  113. action_add_description_->setEnabled(false);
  114. action_options_ = new QAction(this);
  115. action_options_->setText(tr("&Options"));
  116. /* menu help */
  117. action_help_content_ = new QAction(this);
  118. action_help_content_->setText(tr("&Help content"));
  119. action_help_content_->setEnabled(false);
  120. action_about_ = new QAction(this);
  121. action_about_->setText(tr("&About"));
  122. action_about_->setEnabled(false);
  123. /* ------------------ */
  124. menu_file_->addAction(action_open_image_);
  125. menu_file_->addAction(action_open_images_);
  126. menu_file_->addAction(action_open_labeled_image_);
  127. menu_file_->addAction(action_load_legend_);
  128. menu_file_->addAction(menu_pascal_->menuAction());
  129. menu_file_->addSeparator();
  130. menu_file_->addAction(action_save_segmented_);
  131. menu_file_->addAction(action_save_legend_);
  132. menu_file_->addAction(action_save_labels_);
  133. menu_file_->addSeparator();
  134. menu_file_->addAction(action_quit_);
  135. menu_pascal_->addAction(action_load_pascal_file_);
  136. menu_pascal_->addAction(action_load_pascal_poly_);
  137. menu_view_->addAction(action_view_normal_);
  138. menu_view_->addAction(action_view_segmented_);
  139. menu_edit_->addAction(action_undo_);
  140. menu_edit_->addAction(action_redo_);
  141. menu_edit_->addSeparator();
  142. menu_edit_->addAction(action_bound_box_tool_);
  143. menu_edit_->addAction(action_polygon_tool_);
  144. menu_edit_->addAction(action_tagging_tool_);
  145. menu_edit_->addSeparator();
  146. menu_edit_->addAction(action_add_description_);
  147. menu_edit_->addAction(action_options_);
  148. menu_help_->addAction(action_help_content_);
  149. menu_help_->addAction(action_about_);
  150. menu_bar_->addAction(menu_file_->menuAction());
  151. menu_bar_->addAction(menu_view_->menuAction());
  152. menu_bar_->addAction(menu_edit_->menuAction());
  153. menu_bar_->addAction(menu_help_->menuAction());
  154. /*
  155. * popup menu part begins
  156. */
  157. popup_area_list_ = new QMenu;
  158. action_delete_area_ = new QAction(this);
  159. action_delete_area_->setText(tr("&Delete area"));
  160. action_edit_area_ = new QAction(this);
  161. action_edit_area_->setText(tr("&Change area"));
  162. popup_area_list_->addAction(action_delete_area_);
  163. popup_area_list_->addAction(action_edit_area_);
  164. popup_label_list_ = new QMenu;
  165. action_set_color_ = new QAction(this);
  166. action_set_color_->setText(tr("Set &color"));
  167. action_toggle_priority_ = new QAction(this);
  168. action_toggle_priority_->setText(tr("Toggle &priority"));
  169. action_delete_label_ = new QAction(this);
  170. action_delete_label_->setText(tr("&Delete"));
  171. popup_label_list_->addAction(action_set_color_);
  172. popup_label_list_->addAction(action_toggle_priority_);
  173. popup_label_list_->addAction(action_delete_label_);
  174. popup_images_list_ = new QMenu;
  175. action_remove_image_ = new QAction(this);
  176. action_remove_image_->setText(tr("&remove image"));
  177. popup_images_list_->addAction(action_remove_image_);
  178. /*
  179. * widgets part begins
  180. */
  181. central_widget_ = new QWidget(this);
  182. setCentralWidget(central_widget_);
  183. frame_toolbox_ = new QFrame(central_widget_);
  184. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  185. frame_toolbox_->setLineWidth(0);
  186. frame_toolbox_->setMidLineWidth(0);
  187. frame_center_ = new QFrame(central_widget_);
  188. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  189. //frame_image_->setLineWidth(1);
  190. frame_image_ = new QScrollArea(frame_center_);
  191. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  192. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  193. frame_image_->setLineWidth(0);
  194. frame_image_->setMidLineWidth(0);
  195. frame_image_->setWidgetResizable(false);
  196. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  197. frame_image_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  198. frame_labelbox_ = new QFrame(central_widget_);
  199. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  200. frame_labelbox_->setLineWidth(0);
  201. frame_labelbox_->setMidLineWidth(0);
  202. image_ = new QPixmap(500, 500);
  203. image_->fill(QColor(Qt::white));
  204. //image_holder_ = new ImageHolder(frame_image_);
  205. image_holder_ = new ImageHolder;//(frame_image_);
  206. image_holder_->setPixmap(*image_);
  207. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  208. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  209. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  210. image_holder_->setScaledContents(true);
  211. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  212. image_holder_->setLineWidth(0);
  213. frame_image_->setWidget(image_holder_);
  214. list_label_ = new QListWidget(central_widget_);
  215. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  216. list_areas_ = new QListWidget(central_widget_);
  217. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  218. list_images_widget_ = new QListWidget(central_widget_);
  219. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  220. // addLabel(0, false, "BACKGROUND");
  221. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  222. //QListWidgetItem *background = list_label_->item(0);
  223. //qDebug() << background;
  224. //background->setFlags(Qt::ItemIsSelectable);
  225. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  226. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  227. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  228. /* buttons */
  229. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  230. button_bound_box_tool_->setText(tr("bbox"));
  231. button_bound_box_tool_->setEnabled(false);
  232. button_bound_box_tool_->setCheckable(true);
  233. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  234. button_polygon_tool_->setText(tr("poly tool"));
  235. button_polygon_tool_->setEnabled(false);
  236. button_polygon_tool_->setCheckable(true);
  237. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  238. button_tagging_tool_->setText(tr("tagging"));
  239. button_tagging_tool_->setEnabled(false);
  240. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  241. button_clear_selection_tool_->setText(tr("clear selection"));
  242. button_clear_selection_tool_->setEnabled(false);
  243. button_generate_colors_ = new QPushButton(frame_toolbox_);
  244. button_generate_colors_->setText(tr("generate label colors"));
  245. button_generate_colors_->setEnabled(false);
  246. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  247. button_delete_all_labels_->setText(tr("delete all labels"));
  248. button_delete_all_labels_->setEnabled(false);
  249. group_tools_ = new QButtonGroup;
  250. group_tools_->addButton(button_bound_box_tool_);
  251. group_tools_->addButton(button_polygon_tool_);
  252. button_confirm_selection_ = new QPushButton(central_widget_);
  253. button_confirm_selection_->setText(tr("Confirm selection"));
  254. button_confirm_selection_->setEnabled(false);
  255. button_add_label_ = new QPushButton(frame_labelbox_);
  256. button_add_label_->setText(tr("Add label"));
  257. button_add_label_->setEnabled(false);
  258. button_remove_label_ = new QPushButton(frame_labelbox_);
  259. button_remove_label_->setText(tr("Remove label"));
  260. button_remove_label_->setEnabled(false);
  261. button_prev_image_ = new QPushButton(central_widget_);
  262. button_prev_image_->setText("←");
  263. button_next_image_ = new QPushButton(central_widget_);
  264. button_next_image_->setText("→");
  265. /*
  266. * layouts part begins
  267. */
  268. layout_main_ = new QHBoxLayout(central_widget_);
  269. layout_left_ = new QVBoxLayout();
  270. layout_toolbox_ = new QVBoxLayout();
  271. layout_center_ = new QVBoxLayout();
  272. layout_frame_image_ = new QVBoxLayout();
  273. layout_image_widget_ = new QGridLayout();
  274. layout_center_buttons_ = new QHBoxLayout();
  275. layout_right_ = new QVBoxLayout();
  276. layout_labelbox_ = new QVBoxLayout();
  277. layout_labelbox_buttons_ = new QHBoxLayout();
  278. layout_main_->addLayout(layout_left_);
  279. layout_main_->addLayout(layout_center_);
  280. layout_main_->addLayout(layout_right_);
  281. /* making the center part stretchable */
  282. layout_main_->setStretch(1, 1);
  283. /* left part */
  284. layout_left_->addWidget(frame_toolbox_);
  285. frame_toolbox_->setLayout(layout_toolbox_);
  286. layout_toolbox_->addWidget(label_toolbox_);
  287. layout_toolbox_->addWidget(button_bound_box_tool_);
  288. layout_toolbox_->addWidget(button_polygon_tool_);
  289. layout_toolbox_->addSpacing(10);
  290. layout_toolbox_->addWidget(button_tagging_tool_);
  291. layout_toolbox_->addSpacing(10);
  292. layout_toolbox_->addWidget(button_clear_selection_tool_);
  293. layout_toolbox_->addWidget(button_delete_all_labels_);
  294. layout_toolbox_->addSpacing(10);
  295. layout_toolbox_->addWidget(button_generate_colors_);
  296. /* stretch is for making toolbox as small as it can be */
  297. layout_toolbox_->addSpacing(10);
  298. layout_left_->addWidget(label_list_images_);
  299. layout_left_->addWidget(list_images_widget_);
  300. list_images_widget_->setFixedWidth(200);
  301. layout_left_->addStretch(1);
  302. layout_left_->addWidget(button_confirm_selection_);
  303. /* central part */
  304. layout_center_->addWidget(frame_center_);
  305. frame_center_->setLayout(layout_frame_image_);
  306. //layout_frame_image_->addWidget(image_holder_);
  307. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  308. layout_frame_image_->addWidget(frame_image_);
  309. //frame_image_->setLayout(layout_image_widget_);
  310. // layout_image_widget_->setRowStretch(0, 1);
  311. // layout_image_widget_->setColumnStretch(0, 1);
  312. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  313. // layout_image_widget_->setRowStretch(2, 1);
  314. // layout_image_widget_->setColumnStretch(2, 1);
  315. //layout_frame_image_->addStretch(1);
  316. layout_frame_image_->addLayout(layout_center_buttons_);
  317. layout_center_buttons_->addWidget(button_prev_image_);
  318. layout_center_buttons_->addWidget(button_next_image_);
  319. /* right part */
  320. layout_right_->addWidget(frame_labelbox_);
  321. frame_labelbox_->setFixedWidth(200);
  322. frame_labelbox_->setLayout(layout_labelbox_);
  323. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  324. layout_labelbox_buttons_->addWidget(button_add_label_);
  325. layout_labelbox_buttons_->addWidget(button_remove_label_);
  326. layout_labelbox_->addWidget(list_label_);
  327. layout_labelbox_->addWidget(label_list_areas_);
  328. layout_labelbox_->addWidget(list_areas_);
  329. connect(
  330. action_quit_,
  331. SIGNAL(triggered()),
  332. this,
  333. SLOT(close())
  334. );
  335. connect(
  336. action_open_images_,
  337. SIGNAL(triggered()),
  338. this,
  339. SLOT(loadImages())
  340. );
  341. connect(
  342. action_open_image_,
  343. SIGNAL(triggered()),
  344. this,
  345. SLOT(loadImage())
  346. );
  347. connect(
  348. action_open_labeled_image_,
  349. SIGNAL(triggered()),
  350. this,
  351. SLOT(loadInfo())
  352. );
  353. connect(
  354. action_load_legend_,
  355. SIGNAL(triggered()),
  356. this,
  357. SLOT(loadLegendFromFile())
  358. );
  359. connect(
  360. action_load_pascal_file_,
  361. SIGNAL(triggered()),
  362. this,
  363. SLOT(loadPascalFile())
  364. );
  365. connect(
  366. action_load_pascal_poly_,
  367. SIGNAL(triggered()),
  368. this,
  369. SLOT(loadPascalPolys())
  370. );
  371. connect(
  372. action_save_legend_,
  373. SIGNAL(triggered()),
  374. this,
  375. SLOT(saveLegend())
  376. );
  377. connect(
  378. action_save_segmented_,
  379. SIGNAL(triggered()),
  380. this,
  381. SLOT(saveSegmentedPicture())
  382. );
  383. connect(
  384. action_save_labels_,
  385. SIGNAL(triggered()),
  386. this,
  387. SLOT(saveAllInfo())
  388. );
  389. connect(
  390. action_view_normal_,
  391. SIGNAL(triggered()),
  392. this,
  393. SLOT(viewNormal())
  394. );
  395. connect(
  396. action_view_segmented_,
  397. SIGNAL(triggered()),
  398. this,
  399. SLOT(viewSegmented())
  400. );
  401. connect(
  402. action_undo_,
  403. SIGNAL(triggered()),
  404. image_holder_,
  405. SLOT(undo())
  406. );
  407. connect(
  408. action_redo_,
  409. SIGNAL(triggered()),
  410. image_holder_,
  411. SLOT(redo())
  412. );
  413. connect(
  414. action_add_description_,
  415. SIGNAL(triggered()),
  416. &line_edit_form_,
  417. SLOT(setDescription())
  418. );
  419. connect(
  420. action_options_,
  421. SIGNAL(triggered()),
  422. &options_form_,
  423. SLOT(showOptions())
  424. );
  425. connect(
  426. button_add_label_,
  427. SIGNAL(clicked()),
  428. this,
  429. SLOT(addLabel())
  430. );
  431. connect(
  432. button_remove_label_,
  433. SIGNAL(clicked()),
  434. this, SLOT(removeLabel())
  435. );
  436. connect(
  437. button_next_image_,
  438. SIGNAL(clicked()),
  439. this,
  440. SLOT(nextImage())
  441. );
  442. connect(
  443. button_prev_image_,
  444. SIGNAL(clicked()),
  445. this,
  446. SLOT(prevImage())
  447. );
  448. connect(
  449. button_bound_box_tool_,
  450. SIGNAL(toggled(bool)),
  451. this,
  452. SLOT(setBoundingBoxTool(bool))
  453. );
  454. connect(
  455. button_polygon_tool_,
  456. SIGNAL(toggled(bool)),
  457. this,
  458. SLOT(setPolygonTool(bool))
  459. );
  460. connect(
  461. button_tagging_tool_,
  462. SIGNAL(clicked()),
  463. &line_edit_form_,
  464. SLOT(setTags())
  465. );
  466. connect(
  467. button_clear_selection_tool_,
  468. SIGNAL(clicked()),
  469. this,
  470. SLOT(clearAllTool())
  471. );
  472. connect(
  473. button_generate_colors_,
  474. SIGNAL(clicked()),
  475. this,
  476. SLOT(generateColors())
  477. );
  478. connect(
  479. button_delete_all_labels_,
  480. SIGNAL(clicked()),
  481. this,
  482. SLOT(clearLabelList())
  483. );
  484. connect(
  485. button_confirm_selection_,
  486. SIGNAL(clicked()),
  487. this,
  488. SLOT(confirmSelection())
  489. );
  490. connect(
  491. list_label_,
  492. SIGNAL(itemChanged(QListWidgetItem *)),
  493. this,
  494. SLOT(editLabel(QListWidgetItem *))
  495. );
  496. connect(
  497. list_label_,
  498. SIGNAL(itemClicked(QListWidgetItem *)),
  499. this,
  500. SLOT(setLabelID(QListWidgetItem *))
  501. );
  502. connect(
  503. list_areas_,
  504. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  505. image_holder_,
  506. SLOT(focusOnArea(QListWidgetItem *))
  507. );
  508. connect(
  509. list_areas_,
  510. SIGNAL(customContextMenuRequested(const QPoint &)),
  511. this,
  512. SLOT(areaListPopupMenu(const QPoint &))
  513. );
  514. connect(
  515. list_areas_,
  516. SIGNAL(itemChanged(QListWidgetItem *)),
  517. this,
  518. SLOT(onAreaItemChange(QListWidgetItem *))
  519. );
  520. connect(
  521. list_label_,
  522. SIGNAL(customContextMenuRequested(const QPoint &)),
  523. this,
  524. SLOT(labelListPopupMenu(const QPoint &))
  525. );
  526. connect(
  527. list_images_widget_,
  528. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  529. this,
  530. SLOT(selectImage(QListWidgetItem *))
  531. );
  532. connect(
  533. list_images_widget_,
  534. SIGNAL(customContextMenuRequested(const QPoint &)),
  535. this,
  536. SLOT(imageListPopupMenu(const QPoint &))
  537. );
  538. connect(
  539. action_delete_area_,
  540. SIGNAL(triggered()),
  541. this,
  542. SLOT(deleteArea())
  543. );
  544. connect(
  545. action_edit_area_,
  546. SIGNAL(triggered()),
  547. this,
  548. SLOT(editArea())
  549. );
  550. connect(
  551. action_set_color_,
  552. SIGNAL(triggered()),
  553. this,
  554. SLOT(setLabelColor())
  555. );
  556. connect(
  557. action_toggle_priority_,
  558. SIGNAL(triggered()),
  559. this,
  560. SLOT(toggleLabelPriority())
  561. );
  562. connect(
  563. action_delete_label_,
  564. SIGNAL(triggered()),
  565. this,
  566. SLOT(removeLabel())
  567. );
  568. connect(
  569. action_remove_image_,
  570. SIGNAL(triggered()),
  571. this,
  572. SLOT(removeImage())
  573. );
  574. connect(
  575. image_holder_,
  576. SIGNAL(selectionStarted()),
  577. this,
  578. SLOT(onSelectionStarted())
  579. );
  580. connect(
  581. &line_edit_form_,
  582. SIGNAL(dataSet(QString)),
  583. this,
  584. SLOT(setDataFromForm(QString))
  585. );
  586. connect(
  587. image_holder_,
  588. SIGNAL(imageScaled()),
  589. this,
  590. SLOT(onImageScaled())
  591. );
  592. connect(
  593. image_holder_,
  594. SIGNAL(areaEdited()),
  595. this,
  596. SLOT(onAreaEdit())
  597. );
  598. connect(
  599. &options_form_,
  600. SIGNAL(optionsSet()),
  601. this,
  602. SLOT(onOptionsSet())
  603. );
  604. image_holder_->setBoundingBoxList(&list_bounding_box_);
  605. image_holder_->setPolygonList(&list_polygon_);
  606. image_holder_->setLabelColorList(&list_label_colors_);
  607. image_holder_->setMainLabelNum(&main_label_);
  608. image_holder_->setImage(image_);
  609. options_form_.setPASCALpath(&PASCALpath_);
  610. }
  611. ImageLabeler::~ImageLabeler()
  612. {
  613. delete action_quit_;
  614. delete action_open_labeled_image_;
  615. delete action_open_image_;
  616. delete action_open_images_;
  617. delete action_load_legend_;
  618. delete action_load_pascal_file_;
  619. delete action_load_pascal_poly_;
  620. delete action_save_legend_;
  621. delete action_save_segmented_;
  622. delete action_save_labels_;
  623. delete action_view_normal_;
  624. delete action_view_segmented_;
  625. delete action_undo_;
  626. delete action_redo_;
  627. delete action_bound_box_tool_;
  628. delete action_polygon_tool_;
  629. delete action_tagging_tool_;
  630. delete action_add_description_;
  631. delete action_options_;
  632. delete action_about_;
  633. delete action_help_content_;
  634. delete menu_pascal_;
  635. delete menu_file_;
  636. delete menu_view_;
  637. delete menu_edit_;
  638. delete menu_help_;
  639. delete menu_bar_;
  640. delete action_delete_area_;
  641. delete action_edit_area_;
  642. delete popup_area_list_;
  643. delete action_toggle_priority_;
  644. delete action_set_color_;
  645. delete action_delete_label_;
  646. delete popup_label_list_;
  647. delete action_remove_image_;
  648. delete popup_images_list_;
  649. delete image_;
  650. delete image_holder_;
  651. delete button_add_label_;
  652. delete button_remove_label_;
  653. delete button_bound_box_tool_;
  654. delete button_polygon_tool_;
  655. delete button_tagging_tool_;
  656. delete button_clear_selection_tool_;
  657. delete button_generate_colors_;
  658. delete button_delete_all_labels_;
  659. delete button_next_image_;
  660. delete button_prev_image_;
  661. delete button_confirm_selection_;
  662. delete label_list_areas_;
  663. delete label_toolbox_;
  664. delete label_list_images_;
  665. delete list_areas_;
  666. delete list_label_;
  667. delete list_images_widget_;
  668. delete layout_toolbox_;
  669. delete layout_right_;
  670. delete layout_center_buttons_;
  671. delete layout_frame_image_;
  672. delete layout_center_;
  673. delete layout_labelbox_buttons_;
  674. delete layout_labelbox_;
  675. delete layout_left_;
  676. delete layout_main_;
  677. delete frame_labelbox_;
  678. delete frame_toolbox_;
  679. delete frame_image_;
  680. delete frame_center_;
  681. delete central_widget_;
  682. //delete current_image_;
  683. if (pure_data_) {
  684. delete[] *pure_data_;
  685. delete pure_data_;
  686. }
  687. delete list_images_;
  688. }
  689. void
  690. ImageLabeler::addImage(Image *anImage)
  691. {
  692. QListWidgetItem *newItem = new QListWidgetItem;
  693. QString itemText = QString("%1: %2").
  694. arg(list_images_widget_->count()).
  695. arg(getFilenameFromPath(&(anImage->image_)));
  696. if (anImage->labeled_)
  697. itemText.append(" #labeled");
  698. if (anImage->pas_)
  699. itemText.append(" #pas");
  700. newItem->setText(itemText);
  701. list_images_widget_->addItem(newItem);
  702. list_images_->append(*anImage);
  703. }
  704. void
  705. ImageLabeler::addLabel()
  706. {
  707. QListWidgetItem *newItem = new QListWidgetItem;
  708. QString label;
  709. int itemNum = list_label_->count();
  710. label.append(QString("%1: ").arg(itemNum));
  711. if (itemNum) {
  712. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  713. label.append("New label");
  714. }
  715. else {
  716. newItem->setFlags(
  717. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  718. Qt::ItemIsEnabled
  719. );
  720. label.append("BACKGROUND");
  721. }
  722. newItem->setText(label);
  723. QPixmap iconPix = QPixmap(20, 20);
  724. QColor color;
  725. if (0 != itemNum)
  726. color = Qt::white;
  727. else
  728. color = Qt::black;
  729. iconPix.fill(color);
  730. QIcon icon(iconPix);
  731. list_label_colors_.append(color.rgb());
  732. newItem->setIcon(icon);
  733. list_label_->addItem(newItem);
  734. list_label_->setItemSelected(newItem, true);
  735. label_ID_ = list_label_->count() - 1;
  736. unsaved_data_ = 1;
  737. }
  738. void
  739. ImageLabeler::addLabel(
  740. int aLabelID,
  741. bool isMain,
  742. QString aLabel
  743. )
  744. {
  745. QListWidgetItem *newItem = new QListWidgetItem;
  746. if (0 == aLabelID) {
  747. aLabel = QString("BACKGROUND");
  748. newItem->setFlags(
  749. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  750. Qt::ItemIsEnabled
  751. );
  752. }
  753. QString label;
  754. label.append(QString("%1: %2").
  755. arg(aLabelID).
  756. arg(aLabel)
  757. );
  758. /* TODO: check if there is another main label and make it common */
  759. if (isMain) {
  760. main_label_ = aLabelID;
  761. }
  762. QPixmap iconPix = QPixmap(20, 20);
  763. QColor color;
  764. if (0 != aLabelID)
  765. color = Qt::white;
  766. else
  767. color = Qt::black;
  768. iconPix.fill(color);
  769. QIcon icon(iconPix);
  770. list_label_colors_.append(color.rgb());
  771. newItem->setIcon(icon);
  772. newItem->setText(label);
  773. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  774. list_label_->addItem(newItem);
  775. list_label_->setItemSelected(newItem, true);
  776. unsaved_data_ = 1;
  777. }
  778. void
  779. ImageLabeler::editLabel(QListWidgetItem *anItem)
  780. {
  781. QString label = anItem->text();
  782. int itemRow = list_label_->row(anItem);
  783. QString prefix = QString("%1: ").arg(itemRow);
  784. if (-1 != label.indexOf(prefix)) {
  785. return;
  786. /* NOTREACHED */
  787. }
  788. label.prepend(QString("%1: ").arg(itemRow));
  789. if (main_label_ == itemRow) {
  790. label.append(" #main");
  791. }
  792. list_label_->blockSignals(true);
  793. anItem->setText(label);
  794. list_label_->blockSignals(false);
  795. unsaved_data_ = 1;
  796. }
  797. void
  798. ImageLabeler::removeLabel()
  799. {
  800. if (0 == list_label_->count()) {
  801. return;
  802. /* NOTREACHED */
  803. }
  804. //QListWidgetItem *current = list_label_->currentItem();
  805. /* we need to keep BACKGROUND category */
  806. if (label_ID_ < 1) {
  807. return;
  808. /* NOTREACHED */
  809. }
  810. if (list_label_->count() <= label_ID_ ||
  811. list_label_colors_.count() <= label_ID_) {
  812. return;
  813. /* NOTREACHED */
  814. }
  815. list_label_->takeItem(label_ID_);
  816. list_label_colors_.takeAt(label_ID_);
  817. unsaved_data_ = 1;
  818. }
  819. void
  820. ImageLabeler::setLabelID(
  821. QListWidgetItem *anItem
  822. )
  823. {
  824. Q_UNUSED(anItem)
  825. if (!list_label_->count()) {
  826. return;
  827. /* NOTREACHED */
  828. }
  829. label_ID_ = list_label_->row(anItem);
  830. }
  831. void
  832. ImageLabeler::addBBoxArea(
  833. int anID,
  834. BoundingBox aBBox,
  835. int itemID
  836. )
  837. {
  838. QListWidgetItem *newItem = new QListWidgetItem;
  839. if (-1 == itemID)
  840. itemID = list_areas_->count();
  841. QString label;
  842. label.append(QString("%1: ").arg(itemID));
  843. label.append(QString("BBox #%1; ").arg(anID));
  844. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  845. label.append(
  846. QString("data:%1;%2;%3;%4; ").
  847. arg(aBBox.rect.topLeft().x()).
  848. arg(aBBox.rect.topLeft().y()).
  849. arg(aBBox.rect.width()).
  850. arg(aBBox.rect.height())
  851. );
  852. newItem->setText(label);
  853. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  854. list_areas_->insertItem(itemID, newItem);
  855. list_areas_->setItemSelected(newItem, true);
  856. }
  857. void
  858. ImageLabeler::addPolyArea(
  859. int aPolyID,
  860. Polygon aPoly,
  861. int itemID
  862. )
  863. {
  864. QListWidgetItem *newItem = new QListWidgetItem;
  865. if (-1 == itemID)
  866. itemID = list_areas_->count();
  867. QString label;
  868. label.append(QString("%1: ").arg(itemID));
  869. label.append(QString("Poly #%1; ").arg(aPolyID));
  870. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  871. label.append("points:");
  872. for (int i = 0; i < aPoly.poly.count(); i++) {
  873. label.append(
  874. QString("%1;%2;").
  875. arg(aPoly.poly.point(i).x()).
  876. arg(aPoly.poly.point(i).y())
  877. );
  878. }
  879. newItem->setText(label);
  880. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  881. list_areas_->insertItem(itemID, newItem);
  882. list_areas_->setItemSelected(newItem, true);
  883. }
  884. void
  885. ImageLabeler::editArea()
  886. {
  887. bool oldState = list_areas_->blockSignals(true);
  888. if (!list_areas_->currentItem() || !list_areas_->count()) {
  889. return;
  890. /* NOTREACHED */
  891. }
  892. QListWidgetItem *current = list_areas_->currentItem();
  893. old_area_string_ = current->text();
  894. current->setFlags(current->flags() | Qt::ItemIsEditable);
  895. list_areas_->editItem(current);
  896. list_areas_->blockSignals(oldState);
  897. }
  898. void
  899. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  900. {
  901. list_areas_->blockSignals(true);
  902. QString areaString = anItem->text();
  903. int oldID = -1;
  904. if (-1 != areaString.indexOf("Poly")) {
  905. Polygon *poly = new Polygon;
  906. *poly = polyFromString(&areaString, &oldID);
  907. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  908. -1 < oldID) {
  909. list_polygon_.takeAt(oldID);
  910. list_polygon_.insert(oldID, poly);
  911. }
  912. else
  913. anItem->setText(old_area_string_);
  914. }
  915. else if (-1 != areaString.indexOf("BBox")) {
  916. BoundingBox *bbox = new BoundingBox;
  917. *bbox = BBoxFromString(&areaString, &oldID);
  918. if (-1 < bbox->label_ID_ && -1 < oldID) {
  919. list_bounding_box_.takeAt(oldID);
  920. list_bounding_box_.insert(oldID, bbox);
  921. }
  922. else
  923. anItem->setText(old_area_string_);
  924. }
  925. else {
  926. showWarning(tr("record format is corrupted, try again"));
  927. anItem->setText(old_area_string_);
  928. }
  929. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  930. list_areas_->blockSignals(false);
  931. image_holder_->update();
  932. unsaved_data_ = 1;
  933. }
  934. void
  935. ImageLabeler::onAreaEdit()
  936. {
  937. if (!list_areas_->count() ||
  938. -1 == image_holder_->focusedSelection()) {
  939. //showWarning(tr("You haven't added any label"));
  940. return;
  941. /* NOTREACHED */
  942. }
  943. int figureID = image_holder_->focusedSelection();
  944. Figure figure = image_holder_->focusedSelectionType();
  945. for (int i = 0; i < list_areas_->count(); i++) {
  946. QListWidgetItem *item = list_areas_->item(i);
  947. QString text = item->text();
  948. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  949. bool ok = 0;
  950. int num = getNumFromString(&text, "BBox #", ";", &ok);
  951. if (ok && num == figureID) {
  952. list_areas_->takeItem(i);
  953. addBBoxArea(num, *list_bounding_box_.at(num), i);
  954. }
  955. }
  956. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  957. bool ok = 0;
  958. int num = getNumFromString(&text, "Poly #", ";", &ok);
  959. if (ok && num == figureID) {
  960. list_areas_->takeItem(i);
  961. addPolyArea(num, *list_polygon_.at(num), i);
  962. }
  963. }
  964. }
  965. unsaved_data_ = 1;
  966. }
  967. void
  968. ImageLabeler::deleteArea()
  969. {
  970. if (!list_areas_->currentItem() || !list_areas_->count()) {
  971. return;
  972. /* NOTREACHED */
  973. }
  974. QString text = list_areas_->currentItem()->text();
  975. bool ok = 0;
  976. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  977. // if (!ok) {
  978. // return;
  979. // /* NOTREACHED */
  980. // }
  981. /*
  982. * 0 - bbox
  983. * 1 - poly
  984. */
  985. QString shape;
  986. if (-1 != text.indexOf("BBox"))
  987. shape = QString("BBox");
  988. else if (-1 != text.indexOf("Poly"))
  989. shape = QString("Poly");
  990. else {
  991. return;
  992. /* NOTREACHED */
  993. }
  994. int areaNum = getNumFromString(&text, "#", ";", &ok);
  995. if (!ok) {
  996. return;
  997. /* NOTREACHED */
  998. }
  999. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  1000. /* changing all shapes(depends on current) which are next in the list */
  1001. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  1002. QListWidgetItem item = *(list_areas_->item(i));
  1003. QString newText = item.text();
  1004. if (-1 == newText.indexOf(shape))
  1005. continue;
  1006. int num = getNumFromString(&newText, "#", ";", &ok);
  1007. num--;
  1008. QString numString = QString("%1").arg(num);
  1009. int numPos = newText.indexOf("#") + 1;
  1010. newText.replace(numPos, numString.size(), numString);
  1011. list_areas_->takeItem(i);
  1012. list_areas_->insertItem(i, newText);
  1013. }
  1014. list_areas_->takeItem(currentItemRow);
  1015. if (shape == "BBox")
  1016. list_bounding_box_.removeAt(areaNum);
  1017. else
  1018. list_polygon_.removeAt(areaNum);
  1019. image_holder_->update();
  1020. unsaved_data_ = 1;
  1021. }
  1022. void
  1023. ImageLabeler::toggleLabelPriority()
  1024. {
  1025. if (!list_label_->count()) {
  1026. return;
  1027. /* NOTREACHED */
  1028. }
  1029. QListWidgetItem *item = list_label_->currentItem();
  1030. /* because we need to keep BACKGROUND category */
  1031. if (0 == list_label_->row(item)) {
  1032. return;
  1033. /* NOTREACHED */
  1034. }
  1035. int itemRow = list_label_->row(item);
  1036. QString text = item->text();
  1037. /* cleaning previous " #main" mark */
  1038. if (-1 != main_label_) {
  1039. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1040. QString lastMainText = lastMain->text();
  1041. int mainPos = lastMainText.indexOf(" #main");
  1042. lastMainText = lastMainText.mid(0, mainPos);
  1043. list_label_->blockSignals(true);
  1044. lastMain->setText(lastMainText);
  1045. list_label_->blockSignals(false);
  1046. }
  1047. if (main_label_ == itemRow) {
  1048. int mainPos = text.indexOf(" #main");
  1049. text = text.mid(0, mainPos);
  1050. main_label_ = -1;
  1051. }
  1052. else {
  1053. text.append(" #main");
  1054. main_label_ = list_label_->row(item);
  1055. }
  1056. list_label_->blockSignals(true);
  1057. item->setText(text);
  1058. list_label_->blockSignals(false);
  1059. image_holder_->update();
  1060. unsaved_data_ = 1;
  1061. }
  1062. void
  1063. ImageLabeler::getImagesFromDir(const QDir &dir)
  1064. {
  1065. /* avoiding freezing during recursive search for files */
  1066. QApplication::processEvents();
  1067. if (interrupt_search_) {
  1068. clearAll();
  1069. return;
  1070. }
  1071. QStringList filenameFilter;
  1072. filenameFilter <<
  1073. "*.jpeg" <<
  1074. "*.jpg" <<
  1075. "*.gif" <<
  1076. "*.png" <<
  1077. "*.bmp" <<
  1078. "*.tiff" <<
  1079. "*.dat"
  1080. ;
  1081. QStringList listImages =
  1082. dir.entryList(filenameFilter, QDir::Files);
  1083. QStringList ignoredFiles;
  1084. foreach (QString file, listImages) {
  1085. if (ignoredFiles.contains(file) ||
  1086. file.contains("_segmented", Qt::CaseInsensitive) ||
  1087. file.contains(".dat", Qt::CaseInsensitive)) {
  1088. continue;
  1089. }
  1090. Image newImage;
  1091. newImage.image_ = dir.absoluteFilePath(file);
  1092. /* TODO: think about loading pascal files */
  1093. newImage.pas_ = 0;
  1094. QString labeled = alterFileName(file, "_labeled");
  1095. labeled = removePath(labeled);
  1096. labeled.append(".dat");
  1097. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1098. newImage.labeled_ = 1;
  1099. else
  1100. {
  1101. newImage.labeled_ = 0;
  1102. }
  1103. addImage(&newImage);
  1104. }
  1105. QStringList listDir = dir.entryList(QDir::Dirs);
  1106. foreach (QString subdir, listDir) {
  1107. if ("." == subdir || ".." == subdir)
  1108. continue;
  1109. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1110. }
  1111. }
  1112. void
  1113. ImageLabeler::nextImage()
  1114. {
  1115. if (list_images_->isEmpty()) {
  1116. return;
  1117. /* NOTREACHED */
  1118. }
  1119. if (askForUnsavedData()) {
  1120. return;
  1121. /* NOTREACHED */
  1122. }
  1123. if (list_images_widget_->count() - 1 == image_ID_) {
  1124. //current_image_ = list_images_->at(0).image_;
  1125. image_ID_ = 0;
  1126. }
  1127. else {
  1128. image_ID_ ++;
  1129. //current_image_ = list_images_->at(image_ID_).image_;
  1130. }
  1131. if (!selectImage(image_ID_)) {
  1132. showWarning(tr("Next image is not available"));
  1133. return;
  1134. /* NOTREACHED */
  1135. }
  1136. list_images_widget_->setCurrentRow(image_ID_);
  1137. if (current_image_.isEmpty()) {
  1138. return;
  1139. /* NOTREACHED */
  1140. }
  1141. QString winTitle;
  1142. winTitle.append("ImageLabeler - ");
  1143. winTitle.append(current_image_);
  1144. setWindowTitle(winTitle);
  1145. image_->load(current_image_);
  1146. image_holder_->resize(image_->size());
  1147. image_holder_->setPixmap(*image_);
  1148. list_bounding_box_.clear();
  1149. list_polygon_.clear();
  1150. list_areas_->clear();
  1151. image_holder_->clearAll();
  1152. segmented_image_.clear();
  1153. clearLabelColorList();
  1154. //setWindowTitle(tr("ImageLabeler"));
  1155. }
  1156. void
  1157. ImageLabeler::prevImage()
  1158. {
  1159. if (!list_images_widget_->count()) {
  1160. return;
  1161. /* NOTREACHED */
  1162. }
  1163. if (askForUnsavedData()) {
  1164. return;
  1165. /* NOTREACHED */
  1166. }
  1167. if (!image_ID_) {
  1168. image_ID_ = list_images_widget_->count() - 1;
  1169. //current_image_ = list_images_->at(image_ID_).image_;
  1170. }
  1171. else {
  1172. image_ID_--;
  1173. //current_image_ = list_images_->at(image_ID_).image_;
  1174. }
  1175. list_images_widget_->setCurrentRow(image_ID_);
  1176. if (!selectImage(image_ID_)) {
  1177. showWarning(tr("Next image is not available"));
  1178. return;
  1179. /* NOTREACHED */
  1180. }
  1181. QString winTitle;
  1182. winTitle.append("ImageLabeler - ");
  1183. winTitle.append(current_image_);
  1184. setWindowTitle(winTitle);
  1185. image_->load(current_image_);
  1186. image_holder_->resize(image_->size());
  1187. image_holder_->setPixmap(*image_);
  1188. list_bounding_box_.clear();
  1189. list_polygon_.clear();
  1190. list_areas_->clear();
  1191. image_holder_->clearAll();
  1192. segmented_image_.clear();
  1193. clearLabelColorList();
  1194. //setWindowTitle(tr("ImageLabeler"));
  1195. }
  1196. void
  1197. ImageLabeler::saveAllInfo()
  1198. {
  1199. if (!list_images_widget_->count()) {
  1200. showWarning("You have not opened any image yet");
  1201. return;
  1202. /* NOTREACHED */
  1203. }
  1204. /* ------------------------------------------------------------------------
  1205. * XML part
  1206. */
  1207. QDomDocument doc(tr("ImageLabeler"));
  1208. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1209. doc.appendChild(root);
  1210. QDomElement image = doc.createElement(tr("image"));
  1211. root.appendChild(image);
  1212. QDomText pathToImage = doc.createTextNode(current_image_);
  1213. image.appendChild(pathToImage);
  1214. if (!segmented_image_.isEmpty()) {
  1215. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1216. root.appendChild(segmentedImage);
  1217. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1218. segmentedImage.appendChild(pathToSegmented);
  1219. }
  1220. QDomElement description = doc.createElement(tr("description"));
  1221. root.appendChild(description);
  1222. QDomText descriptionText = doc.createTextNode(image_description_);
  1223. description.appendChild(descriptionText);
  1224. QDomElement tags = doc.createElement(tr("tags"));
  1225. root.appendChild(tags);
  1226. QDomText tagsText = doc.createTextNode(tags_);
  1227. tags.appendChild(tagsText);
  1228. legendToXml(&doc, &root);
  1229. objectsToXml(&doc, &root);
  1230. setPureData();
  1231. QDomElement pureData = doc.createElement(tr("pure_data"));
  1232. QSize imageSize = image_->size();
  1233. QString pixelValues;
  1234. for (int i = 0; i < imageSize.height(); i++) {
  1235. for (int j = 0; j < imageSize.width(); j++) {
  1236. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1237. //pixelValues.append(QString(""));
  1238. }
  1239. pixelValues.append("\n");
  1240. }
  1241. QDomText pureDataText = doc.createTextNode(pixelValues);
  1242. pureData.appendChild(pureDataText);
  1243. root.appendChild(pureData);
  1244. QString xml = doc.toString();
  1245. /* ------------------------------------------------------------------------
  1246. * XML part ends
  1247. */
  1248. QFileDialog fileDialog(0, tr("Save all info"));
  1249. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1250. fileDialog.setDefaultSuffix("dat");
  1251. fileDialog.setFileMode(QFileDialog::AnyFile);
  1252. QString dir = getDirFromPath(&current_image_);
  1253. /* altering the name of a new file */
  1254. QString newFileName = alterFileName(current_image_, "_labeled");
  1255. newFileName = removePath(newFileName);
  1256. fileDialog.selectFile(newFileName);
  1257. fileDialog.setDirectory(dir);
  1258. QString filename;
  1259. if (fileDialog.exec()) {
  1260. filename = fileDialog.selectedFiles().last();
  1261. }
  1262. else {
  1263. //showWarning(tr("Can not open file dialog"));
  1264. return;
  1265. /* NOTREACHED */
  1266. }
  1267. if (filename.isEmpty()) {
  1268. return;
  1269. /* NOTREACHED */
  1270. }
  1271. QFile file(filename);
  1272. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1273. showWarning(tr("Can not open file for writing"));
  1274. return;
  1275. /* NOTREACHED */
  1276. }
  1277. file.write(xml.toLocal8Bit());
  1278. file.close();
  1279. unsaved_data_ = 0;
  1280. }
  1281. void
  1282. ImageLabeler::saveSegmentedPicture()
  1283. {
  1284. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1285. return;
  1286. /* NOTREACHED */
  1287. }
  1288. setPureData();
  1289. // QString suffix = *current_image_;
  1290. // int dotPos = suffix.lastIndexOf(".");
  1291. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1292. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1293. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1294. fileDialog.setDefaultSuffix("png");
  1295. fileDialog.setFileMode(QFileDialog::AnyFile);
  1296. QString dir = getDirFromPath(&current_image_);
  1297. /* altering the name of a new file */
  1298. QString newFileName = alterFileName(current_image_, "_segmented");
  1299. newFileName = removePath(newFileName);
  1300. fileDialog.selectFile(newFileName);
  1301. fileDialog.setDirectory(dir);
  1302. QString filename;
  1303. if (fileDialog.exec()) {
  1304. filename = fileDialog.selectedFiles().last();
  1305. }
  1306. else {
  1307. return;
  1308. /* NOTREACHED */
  1309. }
  1310. QSize imageSize = image_holder_->pixmap()->size();
  1311. QImage newImage(imageSize, QImage::Format_RGB32);
  1312. bool generateColorsFlag = auto_color_generation_;
  1313. bool flag = 0;
  1314. /* checking if all the colors are are different from white(default) */
  1315. if (!generateColorsFlag) {
  1316. for (int i = 1; i < list_label_->count(); i++) {
  1317. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1318. flag = 1;
  1319. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1320. QMessageBox msgBox;
  1321. msgBox.setText(tr("There are few labels with default white color."));
  1322. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1323. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1324. msgBox.setDefaultButton(QMessageBox::Yes);
  1325. msgBox.setIcon(QMessageBox::Question);
  1326. int ret = msgBox.exec();
  1327. if (QMessageBox::Yes == ret) {
  1328. generateColorsFlag = 1;
  1329. break;
  1330. }
  1331. else {
  1332. return;
  1333. /* NOTREACHED */
  1334. }
  1335. }
  1336. }
  1337. }
  1338. /* generating colors for labels */
  1339. if (list_label_colors_.count() < list_label_->count() ||
  1340. generateColorsFlag) {
  1341. generateColors();
  1342. }
  1343. for (int i = 0; i < imageSize.height(); i++)
  1344. for (int j = 0; j < imageSize.width(); j++) {
  1345. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1346. }
  1347. if (!newImage.save(filename, "png", 100)) {
  1348. showWarning(tr("An error occurred while saving the segmented image"));
  1349. return;
  1350. /* NOTREACHED */
  1351. }
  1352. segmented_image_ = filename;
  1353. action_view_segmented_->setEnabled(true);
  1354. }
  1355. void
  1356. ImageLabeler::saveLegend()
  1357. {
  1358. if (!list_label_->count()) {
  1359. showWarning("You have not added any label yet");
  1360. return;
  1361. /* NOTREACHED */
  1362. }
  1363. /* ------------------------------------------------------------------------
  1364. * XML part
  1365. */
  1366. QDomDocument doc(tr("ImageLabeler"));
  1367. QDomElement root = doc.createElement(tr("root"));
  1368. doc.appendChild(root);
  1369. legendToXml(&doc, &root);
  1370. QString xml = doc.toString();
  1371. /* ------------------------------------------------------------------------
  1372. * XML part ends
  1373. */
  1374. QFileDialog fileDialog(0, tr("Save legend"));
  1375. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1376. fileDialog.setDefaultSuffix("dat");
  1377. fileDialog.setFileMode(QFileDialog::AnyFile);
  1378. QString dir = getDirFromPath(&current_image_);
  1379. /* altering the name of a new file */
  1380. QString newFileName = alterFileName(current_image_, "_legend");
  1381. newFileName = removePath(newFileName);
  1382. fileDialog.selectFile(newFileName);
  1383. fileDialog.setDirectory(dir);
  1384. QString filename;
  1385. if (fileDialog.exec()) {
  1386. filename = fileDialog.selectedFiles().last();
  1387. }
  1388. else {
  1389. //showWarning(tr("Can not open file dialog"));
  1390. return;
  1391. /* NOTREACHED */
  1392. }
  1393. if (filename.isEmpty()) {
  1394. return;
  1395. /* NOTREACHED */
  1396. }
  1397. QFile file(filename);
  1398. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1399. showWarning(tr("Can not open file for writing"));
  1400. return;
  1401. /* NOTREACHED */
  1402. }
  1403. file.write(xml.toLocal8Bit());
  1404. file.close();
  1405. }
  1406. void
  1407. ImageLabeler::loadInfo()
  1408. {
  1409. if (askForUnsavedData()) {
  1410. return;
  1411. /* NOTREACHED */
  1412. }
  1413. QFileDialog fileDialog(0, tr("Load file with info"));
  1414. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1415. fileDialog.setDefaultSuffix("dat");
  1416. fileDialog.setFileMode(QFileDialog::AnyFile);
  1417. QString filename;
  1418. if (fileDialog.exec()) {
  1419. filename = fileDialog.selectedFiles().last();
  1420. }
  1421. else {
  1422. //showWarning(tr("Can not open file dialog"));
  1423. return;
  1424. /* NOTREACHED */
  1425. }
  1426. clearAllTool();
  1427. clearLabelList();
  1428. if (loadInfo(filename)) {
  1429. enableTools();
  1430. Image newImage;
  1431. newImage.image_ = current_image_;
  1432. newImage.labeled_ = 1;
  1433. newImage.pas_ = 0;
  1434. addImage(&newImage);
  1435. image_ID_ = list_images_widget_->count() - 1;
  1436. list_images_widget_->setCurrentRow(image_ID_);
  1437. }
  1438. unsaved_data_ = 0;
  1439. }
  1440. bool
  1441. ImageLabeler::loadInfo(QString filename)
  1442. {
  1443. QDomDocument doc("Image Labeler");
  1444. QFile file(filename);
  1445. if (!file.open(QIODevice::ReadOnly)) {
  1446. showWarning(tr("Can not open such file"));
  1447. return false;
  1448. /* NOTREACHED */
  1449. }
  1450. QString errMsg;
  1451. if (!doc.setContent(&file, &errMsg)) {
  1452. showWarning(errMsg);
  1453. file.close();
  1454. return false;
  1455. /* NOTREACHED */
  1456. }
  1457. file.close();
  1458. //clearAll();
  1459. //enableTools();
  1460. /* getting all info */
  1461. QDomElement elements = doc.documentElement();
  1462. QDomNode rootNode = elements.firstChild();
  1463. QString string;
  1464. while(!rootNode.isNull()) {
  1465. QDomElement element = rootNode.toElement();
  1466. if(!element.isNull()) {
  1467. /* path to the image */
  1468. if (element.tagName() == "image") {
  1469. string = element.text();
  1470. if (string.isEmpty()) {
  1471. showWarning(
  1472. tr(
  1473. "The file with data doesn't contain path to the image"
  1474. )
  1475. );
  1476. return false;
  1477. /* NOTREACHED */
  1478. }
  1479. if (!image_->load(string)) {
  1480. return false;
  1481. /* NOTREACHED */
  1482. }
  1483. // Image newImage;
  1484. // newImage.image_ = string;
  1485. // newImage.labeled_ = 1;
  1486. // addImage(&newImage);
  1487. current_image_ = string;
  1488. // image_ID_ = list_images_widget_->count() - 1;
  1489. // list_images_widget_->setCurrentRow(image_ID_);
  1490. QString winTitle;
  1491. winTitle.append("ImageLabeler - ");
  1492. winTitle.append(current_image_);
  1493. setWindowTitle(winTitle);
  1494. image_holder_->resize(image_->size());
  1495. image_holder_->setPixmap(*image_);
  1496. }
  1497. /* path to the segmented image */
  1498. if (element.tagName() == "segmented") {
  1499. string = element.text();
  1500. if (string.isEmpty()) {
  1501. continue;
  1502. }
  1503. segmented_image_ = string;
  1504. action_view_segmented_->setEnabled(true);
  1505. }
  1506. /* image description */
  1507. else if (element.tagName() == "description" &&
  1508. !element.text().isEmpty()) {
  1509. image_description_ = element.text();
  1510. }
  1511. /* legend */
  1512. else if (element.tagName() == "legend") {
  1513. loadLegendFromNode(&element);
  1514. }
  1515. /* objects */
  1516. else if (element.tagName() == "objects") {
  1517. QDomNode subNode = element.firstChild();
  1518. QDomElement subElement;
  1519. while(!subNode.isNull()) {
  1520. subElement = subNode.toElement();
  1521. if (subElement.isNull() || subElement.text().isEmpty()) {
  1522. subNode = subNode.nextSibling();
  1523. continue;
  1524. }
  1525. string = subElement.attribute("id");
  1526. bool ok = 1;
  1527. int id = string.toInt(&ok, 10);
  1528. if (!ok) {
  1529. qDebug() <<
  1530. "loadInfo: "
  1531. "poly id format is corrupted";
  1532. subNode = subNode.nextSibling();
  1533. continue;
  1534. }
  1535. string = subElement.text();
  1536. if (subElement.tagName() == "bbox") {
  1537. addBBoxFromData(&string, &id);
  1538. }
  1539. if (subElement.tagName() == "poly") {
  1540. addPolyFromData(&string, &id);
  1541. }
  1542. subNode = subNode.nextSibling();
  1543. }
  1544. }
  1545. }
  1546. rootNode = rootNode.nextSibling();
  1547. }
  1548. unsaved_data_ = 0;
  1549. return true;
  1550. }
  1551. void
  1552. ImageLabeler::loadPascalFile()
  1553. {
  1554. if (askForUnsavedData()) {
  1555. return;
  1556. /* NOTREACHED */
  1557. }
  1558. if (PASCALpath_.isEmpty()) {
  1559. showWarning(tr("before opening first PASCAL file please choose \"root\" directory"
  1560. " where a folder with segmentations,"
  1561. " a folder with polygons,"
  1562. " a folder with image descriptions and"
  1563. " a folder with the pure images are."));
  1564. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  1565. fileDialog.setFileMode(QFileDialog::Directory);
  1566. if (fileDialog.exec())
  1567. PASCALpath_ = fileDialog.selectedFiles().last();
  1568. else {
  1569. return;
  1570. /* NOTREACHED */
  1571. }
  1572. }
  1573. QFileDialog fileDialog(0, tr("Load pascal file"));
  1574. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1575. fileDialog.setDefaultSuffix("xml");
  1576. fileDialog.setFileMode(QFileDialog::AnyFile);
  1577. QStringList filters;
  1578. filters << "PASCAL xml data (*.xml)"
  1579. << "Any files (*)";
  1580. fileDialog.setNameFilters(filters);
  1581. QString filename;
  1582. if (fileDialog.exec()) {
  1583. filename = fileDialog.selectedFiles().last();
  1584. }
  1585. else {
  1586. //showWarning(tr("Can not open file dialog"));
  1587. return;
  1588. /* NOTREACHED */
  1589. }
  1590. clearAllTool();
  1591. clearLabelList();
  1592. if (loadPascalFile(filename, PASCALpath_)) {
  1593. enableTools();
  1594. Image newImage;
  1595. newImage.image_ = current_image_;
  1596. newImage.labeled_ = 1;
  1597. newImage.pas_ = 1;
  1598. addImage(&newImage);
  1599. image_ID_ = list_images_widget_->count() - 1;
  1600. list_images_widget_->setCurrentRow(image_ID_);
  1601. }
  1602. unsaved_data_ = 0;
  1603. }
  1604. bool
  1605. ImageLabeler::loadPascalFile(QString aFilename, QString aPath)
  1606. {
  1607. QDomDocument doc;
  1608. QFile file(aFilename);
  1609. if (!file.open(QIODevice::ReadOnly)) {
  1610. showWarning(tr("Can not open such file"));
  1611. return false;
  1612. /* NOTREACHED */
  1613. }
  1614. QString errMsg;
  1615. if (!doc.setContent(&file, &errMsg)) {
  1616. showWarning(errMsg);
  1617. file.close();
  1618. return false;
  1619. /* NOTREACHED */
  1620. }
  1621. file.close();
  1622. //clearAll();
  1623. //enableTools();
  1624. /* getting all info */
  1625. QDomElement elements = doc.documentElement();
  1626. QDomNode rootNode = elements.firstChild();
  1627. QString string;
  1628. QString path;
  1629. if (aPath.isEmpty())
  1630. path = getPathFromFilename(aFilename);
  1631. else
  1632. path = aPath + "/";
  1633. QString filename;
  1634. QStringList labels;
  1635. labels << "BACKGROUND";
  1636. int labelID;
  1637. while(!rootNode.isNull()) {
  1638. QDomElement element = rootNode.toElement();
  1639. if(!element.isNull()) {
  1640. /* folder */
  1641. if (element.tagName() == "folder") {
  1642. string = element.text();
  1643. if (!string.isEmpty()) {
  1644. path.append(string);
  1645. }
  1646. }
  1647. /* filename */
  1648. else if (element.tagName() == "filename") {
  1649. string = element.text();
  1650. if (!string.isEmpty()) {
  1651. filename = string;
  1652. }
  1653. }
  1654. /* object */
  1655. else if (element.tagName() == "object") {
  1656. QDomNode subNode = element.firstChild();
  1657. QDomElement subElement;
  1658. while(!subNode.isNull()) {
  1659. subElement = subNode.toElement();
  1660. if (subElement.isNull() || subElement.text().isEmpty()) {
  1661. subNode = subNode.nextSibling();
  1662. continue;
  1663. }
  1664. /* label */
  1665. if (subElement.tagName() == "name") {
  1666. string = subElement.text();
  1667. if (!string.isEmpty() &&
  1668. !labels.contains(string, Qt::CaseInsensitive))
  1669. {
  1670. addLabel(list_label_->count(), 0, string);
  1671. labelID = labels.count();
  1672. labels << string;
  1673. }
  1674. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1675. for (int i = 0; i < labels.count(); i++) {
  1676. if (labels.at(i) == string)
  1677. labelID = i;
  1678. }
  1679. }
  1680. }
  1681. if (subElement.tagName() == "bndbox") {
  1682. /* 2 points */
  1683. QPoint topLeft;
  1684. QPoint bottomRight;
  1685. QDomNode bboxNode = subElement.firstChild();
  1686. QDomElement bboxElement;
  1687. while(!bboxNode.isNull()) {
  1688. bboxElement = bboxNode.toElement();
  1689. string.clear();
  1690. bool ok = 1;
  1691. if (bboxElement.tagName() == "xmin") {
  1692. string = bboxElement.text();
  1693. if (string.isEmpty())
  1694. ok = 0;
  1695. double xmin = string.toDouble(&ok);
  1696. if (ok)
  1697. topLeft.setX(qRound(xmin));
  1698. }
  1699. else if (bboxElement.tagName() == "ymin") {
  1700. string = bboxElement.text();
  1701. if (string.isEmpty())
  1702. ok = 0;
  1703. double ymin = string.toDouble(&ok);
  1704. if (ok)
  1705. topLeft.setY(qRound(ymin));
  1706. }
  1707. else if (bboxElement.tagName() == "xmax") {
  1708. string = bboxElement.text();
  1709. if (string.isEmpty())
  1710. ok = 0;
  1711. double xmax = string.toDouble(&ok);
  1712. if (ok)
  1713. bottomRight.setX(qRound(xmax));
  1714. }
  1715. else if (bboxElement.tagName() == "ymax") {
  1716. string = bboxElement.text();
  1717. if (string.isEmpty())
  1718. ok = 0;
  1719. double ymax = string.toDouble(&ok);
  1720. if (ok)
  1721. bottomRight.setY(qRound(ymax));
  1722. }
  1723. if (string.isEmpty() || !ok)
  1724. break;
  1725. bboxNode = bboxNode.nextSibling();
  1726. if (bboxNode.isNull()) {
  1727. BoundingBox *bbox = new BoundingBox;
  1728. bbox->rect.setTopLeft(topLeft);
  1729. bbox->rect.setBottomRight(bottomRight);
  1730. bbox->label_ID_ = labelID;
  1731. addBBox(bbox);
  1732. }
  1733. }
  1734. }
  1735. subNode = subNode.nextSibling();
  1736. }
  1737. }
  1738. }
  1739. rootNode = rootNode.nextSibling();
  1740. }
  1741. if (!image_->load(path + "/" + filename)) {
  1742. return false;
  1743. /* NOTREACHED */
  1744. }
  1745. current_image_ = path + filename;
  1746. QString winTitle;
  1747. winTitle.append("ImageLabeler - ");
  1748. winTitle.append(current_image_);
  1749. setWindowTitle(winTitle);
  1750. image_holder_->resize(image_->size());
  1751. image_holder_->setPixmap(*image_);
  1752. unsaved_data_ = 0;
  1753. return true;
  1754. }
  1755. void
  1756. ImageLabeler::loadPascalPolys()
  1757. {
  1758. if (current_image_.isEmpty()) {
  1759. return;
  1760. /* NOTREACHED */
  1761. }
  1762. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1763. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1764. fileDialog.setDefaultSuffix("polygon");
  1765. fileDialog.setFileMode(QFileDialog::AnyFile);
  1766. QStringList filters;
  1767. filters << "PASCAL polygons (*.polygon)"
  1768. << "Any files (*)";
  1769. fileDialog.setNameFilters(filters);
  1770. QString filename;
  1771. if (fileDialog.exec()) {
  1772. filename = fileDialog.selectedFiles().last();
  1773. }
  1774. else {
  1775. //showWarning(tr("Can not open file dialog"));
  1776. return;
  1777. /* NOTREACHED */
  1778. }
  1779. //clearAllTool();
  1780. //clearLabelList();
  1781. if (!loadPascalPolys(filename)) {
  1782. showWarning(tr("File format is corrupted."));
  1783. }
  1784. unsaved_data_ = 0;
  1785. image_holder_->update();
  1786. }
  1787. bool
  1788. ImageLabeler::loadPascalPolys(QString aFilename)
  1789. {
  1790. QFile file(aFilename);
  1791. if (!file.open(QIODevice::ReadOnly)) {
  1792. showWarning(tr("Can not open such file"));
  1793. return false;
  1794. /* NOTREACHED */
  1795. }
  1796. QByteArray data = file.readAll();
  1797. file.close();
  1798. QString label;
  1799. int lastSpace = 0;
  1800. int pointCount = 0;
  1801. Polygon *poly = 0;
  1802. QPoint point;
  1803. bool evenFlag = 0;
  1804. //for (int i = 0; i < data.length(); i++) {
  1805. int i = 0;
  1806. while (i < data.length()) {
  1807. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1808. label = QString(data.mid(lastSpace, i - lastSpace));
  1809. lastSpace = i + 1;
  1810. }
  1811. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1812. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1813. bool ok = 1;
  1814. pointCount = num.toInt(&ok, 10);
  1815. if (!ok) {
  1816. return false;
  1817. /* NOTREACHED */
  1818. }
  1819. poly = new Polygon;
  1820. poly->label_ID_ = -1;
  1821. lastSpace = i + 1;
  1822. }
  1823. else if (data.at(i) == ' ' &&
  1824. !label.isEmpty() &&
  1825. pointCount &&
  1826. !evenFlag)
  1827. {
  1828. evenFlag = 1;
  1829. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1830. bool ok = 1;
  1831. int coor = qRound(num.toDouble(&ok));
  1832. if (!ok) {
  1833. return false;
  1834. /* NOTREACHED */
  1835. }
  1836. point.setX(coor);
  1837. lastSpace = i + 1;
  1838. }
  1839. else if (data.at(i) == ' ' &&
  1840. !label.isEmpty() &&
  1841. pointCount &&
  1842. evenFlag)
  1843. {
  1844. evenFlag = 0;
  1845. pointCount--;
  1846. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1847. bool ok = 1;
  1848. int coor = qRound(num.toDouble(&ok));
  1849. if (!ok) {
  1850. return false;
  1851. /* NOTREACHED */
  1852. }
  1853. point.setY(coor);
  1854. lastSpace = i + 1;
  1855. poly->poly << point;
  1856. }
  1857. else if (data.at(i) == '\n') {
  1858. if (pointCount || !poly) {
  1859. return false;
  1860. /* NOTREACHED */
  1861. }
  1862. lastSpace = i + 1;
  1863. for (int j = 0; j < list_label_->count(); j++) {
  1864. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1865. poly->label_ID_ = j;
  1866. }
  1867. }
  1868. if (-1 == poly->label_ID_) {
  1869. int labelID = list_label_->count();
  1870. addLabel(labelID, 0, label);
  1871. poly->label_ID_ = labelID;
  1872. }
  1873. addPoly(poly);
  1874. label.clear();
  1875. }
  1876. i++;
  1877. }
  1878. //clearAll();
  1879. //enableTools();
  1880. return true;
  1881. }
  1882. void
  1883. ImageLabeler::loadImage()
  1884. {
  1885. if (askForUnsavedData()) {
  1886. return;
  1887. /* NOTREACHED */
  1888. }
  1889. QFileDialog fileDialog(0, tr("Load image"));
  1890. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1891. QStringList filters;
  1892. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1893. << "Any files (*)";
  1894. fileDialog.setNameFilters(filters);
  1895. QString filename;
  1896. if (fileDialog.exec()) {
  1897. filename = fileDialog.selectedFiles().last();
  1898. }
  1899. else {
  1900. //showWarning(tr("Could not open file dialog"));
  1901. return;
  1902. /* NOTREACHED */
  1903. }
  1904. if (filename.isEmpty()) {
  1905. return;
  1906. /* NOTREACHED */
  1907. }
  1908. QString dirPath = getPathFromFilename(filename);
  1909. QDir dir(dirPath);
  1910. QStringList filter;
  1911. filter << "*.dat";
  1912. QStringList fileList = dir.entryList(filter, QDir::Files);
  1913. QString labeled = alterFileName(filename, "_labeled");
  1914. labeled = removePath(labeled);
  1915. labeled.append(".dat");
  1916. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1917. labeled = dir.absoluteFilePath(labeled);
  1918. loadInfo(labeled);
  1919. Image *newImage = new Image;
  1920. newImage->image_ = filename;
  1921. newImage->labeled_ = 1;
  1922. newImage->pas_ = 0;
  1923. addImage(newImage);
  1924. enableTools();
  1925. return;
  1926. /* NOTREACHED */
  1927. }
  1928. clearAllTool();
  1929. QString winTitle;
  1930. winTitle.append("ImageLabeler - ");
  1931. winTitle.append(current_image_);
  1932. setWindowTitle(winTitle);
  1933. if (!image_->load(filename)) {
  1934. return;
  1935. /* NOTREACHED */
  1936. }
  1937. image_holder_->resize(image_->size());
  1938. image_holder_->setPixmap(*image_);
  1939. current_image_ = filename;
  1940. image_ID_ = list_images_widget_->count() - 1;
  1941. list_images_widget_->setCurrentRow(image_ID_);
  1942. Image *newImage = new Image;
  1943. newImage->image_ = filename;
  1944. newImage->labeled_ = 0;
  1945. newImage->pas_ = 0;
  1946. addImage(newImage);
  1947. if (!list_images_widget_->count()) {
  1948. return;
  1949. /* NOTREACHED */
  1950. }
  1951. enableTools();
  1952. }
  1953. void
  1954. ImageLabeler::loadImages()
  1955. {
  1956. if (askForUnsavedData()) {
  1957. return;
  1958. /* NOTREACHED */
  1959. }
  1960. QFileDialog fileDialog(0, tr("Load images"));
  1961. fileDialog.setFileMode(QFileDialog::Directory);
  1962. QString dirName("");
  1963. if (fileDialog.exec()) {
  1964. /* TODO: make it possible to select multiple folders */
  1965. dirName = fileDialog.selectedFiles().last();
  1966. }
  1967. else {
  1968. //showWarning(tr("Could not open file dialog"));
  1969. return;
  1970. /* NOTREACHED */
  1971. }
  1972. clearAllTool();
  1973. QWidget *searchInProgress = new QWidget(0);
  1974. searchInProgress->setWindowTitle(tr("Loading images"));
  1975. QLabel *info = new QLabel(searchInProgress);
  1976. info->setText(
  1977. tr("Program is looking for all image files in your directory recursively."));
  1978. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1979. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1980. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1981. searchInProgress->setLayout(layout);
  1982. layout->addWidget(info);
  1983. layout->addWidget(cancel);
  1984. searchInProgress->adjustSize();
  1985. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1986. searchInProgress->show();
  1987. getImagesFromDir(QDir(dirName));
  1988. cancel->disconnect();
  1989. searchInProgress->hide();
  1990. delete info;
  1991. delete cancel;
  1992. delete layout;
  1993. delete searchInProgress;
  1994. if (interrupt_search_) {
  1995. interrupt_search_ = 0;
  1996. return;
  1997. /* NOTREACHED */
  1998. }
  1999. if (!list_images_widget_->count()) {
  2000. showWarning(tr("The folder you selected contains no images"));
  2001. return;
  2002. /* NOTREACHED */
  2003. }
  2004. bool ret = 0;
  2005. if (list_images_->at(0).labeled_) {
  2006. QString labeled =
  2007. alterFileName(list_images_->at(0).image_, "_labeled");
  2008. labeled.append(".dat");
  2009. ret = loadInfo(labeled);
  2010. }
  2011. else
  2012. ret = image_->load(list_images_->at(0).image_);
  2013. if (!ret) {
  2014. return;
  2015. /* NOTREACHED */
  2016. }
  2017. current_image_ = list_images_->at(0).image_;
  2018. image_ID_ = 0;
  2019. list_images_widget_->setCurrentRow(image_ID_);
  2020. QString winTitle;
  2021. winTitle.append("ImageLabeler - ");
  2022. winTitle.append(current_image_);
  2023. setWindowTitle(winTitle);
  2024. image_holder_->resize(image_->size());
  2025. image_holder_->setPixmap(*image_);
  2026. enableTools();
  2027. }
  2028. void
  2029. ImageLabeler::loadLegendFromFile()
  2030. {
  2031. QFileDialog fileDialog(0, tr("Load legend"));
  2032. fileDialog.setFileMode(QFileDialog::AnyFile);
  2033. QString filename;
  2034. if (fileDialog.exec()) {
  2035. filename = fileDialog.selectedFiles().last();
  2036. }
  2037. else {
  2038. //showWarning(tr("Could not open file dialog"));
  2039. return;
  2040. /* NOTREACHED */
  2041. }
  2042. QDomDocument doc("Image Labeler");
  2043. QFile file(filename);
  2044. if (!file.open(QIODevice::ReadOnly)) {
  2045. showWarning(tr("Can not open such file"));
  2046. return;
  2047. /* NOTREACHED */
  2048. }
  2049. QString errMsg;
  2050. if (!doc.setContent(&file, &errMsg)) {
  2051. showWarning(errMsg);
  2052. file.close();
  2053. return;
  2054. /* NOTREACHED */
  2055. }
  2056. file.close();
  2057. list_label_->clear();
  2058. /* getting legend */
  2059. QDomElement elements = doc.documentElement();
  2060. QDomNode rootNode = elements.firstChild();
  2061. QString string;
  2062. while(!rootNode.isNull()) {
  2063. QDomElement element = rootNode.toElement();
  2064. if(!element.isNull()) {
  2065. if (element.tagName() == "legend") {
  2066. loadLegendFromNode(&element);
  2067. }
  2068. }
  2069. rootNode = rootNode.nextSibling();
  2070. }
  2071. }
  2072. void
  2073. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2074. {
  2075. if (!anElement) {
  2076. return;
  2077. /* NOTREACHED */
  2078. }
  2079. QDomNode subNode = anElement->firstChild();
  2080. QDomElement subElement;
  2081. QString string;
  2082. int id = -1;
  2083. bool isMain;
  2084. uint color = 0xff000000;
  2085. while(!subNode.isNull()) {
  2086. subElement = subNode.toElement();
  2087. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2088. string = subElement.attribute("id");
  2089. bool ok = 0;
  2090. id = string.toInt(&ok, 10);
  2091. if (!ok) {
  2092. qDebug() <<
  2093. "loadLegendFromNode: "
  2094. "label id format is corrupted";
  2095. subNode = subNode.nextSibling();
  2096. continue;
  2097. }
  2098. string = subElement.attribute("isMain");
  2099. isMain = string.toInt(&ok, 2);
  2100. if (!ok) {
  2101. qDebug() <<
  2102. "loadLegendFromNode: "
  2103. "label isMain flag format is corrupted";
  2104. subNode = subNode.nextSibling();
  2105. continue;
  2106. }
  2107. string = subElement.attribute("color");
  2108. color = string.toUInt(&ok, 16);
  2109. if (!ok) {
  2110. qDebug() <<
  2111. "loadLegendFromNode: "
  2112. "label color format is corrupted";
  2113. subNode = subNode.nextSibling();
  2114. continue;
  2115. }
  2116. string = subElement.text();
  2117. addLabel(id, isMain, string);
  2118. setLabelColor(id, color);
  2119. }
  2120. subNode = subNode.nextSibling();
  2121. }
  2122. }
  2123. void
  2124. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2125. {
  2126. QDomElement legend = aDoc->createElement(tr("legend"));
  2127. aRoot->appendChild(legend);
  2128. /* storing all labels made by user */
  2129. int labelCount = list_label_->count();
  2130. for (int i = 0; i < labelCount; i++) {
  2131. QDomElement label = aDoc->createElement(tr("label"));
  2132. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2133. label.setAttribute("id", i);
  2134. QString priority;
  2135. if (main_label_ == i)
  2136. priority.append("1");
  2137. else
  2138. priority.append("0");
  2139. label.setAttribute("isMain", priority);
  2140. QString labelText = list_label_->item(i)->text();
  2141. /* removing the number prefix of label */
  2142. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2143. labelText = labelText.mid(3, labelText.size() - 3);
  2144. }
  2145. QDomText labelName = aDoc->createTextNode(labelText);
  2146. label.appendChild(labelName);
  2147. legend.appendChild(label);
  2148. }
  2149. /* in case we have no labels */
  2150. if (0 == labelCount) {
  2151. QDomElement label = aDoc->createElement(tr("label"));
  2152. label.setAttribute(tr("id"), -1);
  2153. legend.appendChild(label);
  2154. }
  2155. }
  2156. void
  2157. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2158. {
  2159. QDomElement objects = aDoc->createElement(tr("objects"));
  2160. aRoot->appendChild(objects);
  2161. /* rects first */
  2162. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2163. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2164. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2165. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2166. QString rectDataString =
  2167. QString("%1;%2;%3;%4;").
  2168. arg(rect.x()).
  2169. arg(rect.y()).
  2170. arg(rect.width()).
  2171. arg(rect.height());
  2172. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2173. rectData.appendChild(rectDataText);
  2174. objects.appendChild(rectData);
  2175. }
  2176. /* polys next */
  2177. for (int i = 0; i < list_polygon_.size(); i++) {
  2178. QDomElement polyData = aDoc->createElement(tr("poly"));
  2179. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2180. QPolygon poly = list_polygon_.at(i)->poly;
  2181. QString polyDataString;
  2182. for (int j = 0; j < poly.count(); j++)
  2183. polyDataString.append(
  2184. QString("%1;%2;").
  2185. arg(poly.point(j).x()).
  2186. arg(poly.point(j).y())
  2187. );
  2188. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2189. polyData.appendChild(polyDataText);
  2190. objects.appendChild(polyData);
  2191. }
  2192. }
  2193. void
  2194. ImageLabeler::generateColors()
  2195. {
  2196. int labelCount = list_label_->count();
  2197. if (!labelCount) {
  2198. showWarning(tr("you have not added any labels yet"));
  2199. return;
  2200. /* NOTREACHED */
  2201. }
  2202. int coeff = (0xff / labelCount) * 3;
  2203. //QList< uint > colors;
  2204. list_label_colors_.clear();
  2205. list_label_colors_.append(0);
  2206. uchar red = 0xff;
  2207. uchar green = 0xff;
  2208. uchar blue = 0xff;
  2209. uchar iterationColor = coeff;
  2210. uint color = 0xffffffff;
  2211. int j = 1;
  2212. for (int i = 1; i < labelCount; i++) {
  2213. if (6 == j) {
  2214. iterationColor += coeff;
  2215. j = 1;
  2216. }
  2217. if (5 == j) {
  2218. red = 0xff - iterationColor;
  2219. green = 0xff - iterationColor;
  2220. blue = 0xff;
  2221. }
  2222. else if (4 == j) {
  2223. red = 0xff - iterationColor;
  2224. green = 0xff;
  2225. blue = 0xff - iterationColor;
  2226. }
  2227. else if (3 == j) {
  2228. red = 0xff - iterationColor;
  2229. green = 0xff;
  2230. blue = 0xff;
  2231. }
  2232. else if (2 == j) {
  2233. red = 0xff;
  2234. green = 0xff - iterationColor;
  2235. blue = 0xff;
  2236. }
  2237. else if (1 == j){
  2238. red = 0xff;
  2239. green = 0xff;
  2240. blue = 0xff - iterationColor;
  2241. }
  2242. j++;
  2243. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2244. int itemNo = list_label_colors_.count();
  2245. QPixmap iconPix = QPixmap(20, 20);
  2246. iconPix.fill(color);
  2247. QIcon icon(iconPix);
  2248. list_label_->item(itemNo)->setIcon(icon);
  2249. list_label_colors_.append(color);
  2250. }
  2251. image_holder_->update();
  2252. }
  2253. void
  2254. ImageLabeler::addBBoxFromData(
  2255. QString *aBBoxData,
  2256. int *ID
  2257. )
  2258. {
  2259. BoundingBox *bbox = new BoundingBox;
  2260. *bbox = BBoxFromData(aBBoxData);
  2261. if (!bbox->rect.isValid() || !ID) {
  2262. return;
  2263. /* NOTREACHED */
  2264. }
  2265. bbox->label_ID_ = *ID;
  2266. list_bounding_box_.append(bbox);
  2267. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2268. }
  2269. BoundingBox
  2270. ImageLabeler::BBoxFromData(
  2271. QString *aBBoxData
  2272. )
  2273. {
  2274. BoundingBox bbox;
  2275. QString buffer;
  2276. bbox.rect.setRect(-1, -1, -1, -1);
  2277. int startPos = 0;
  2278. bool ok = 1;
  2279. for (int i = 0; i < aBBoxData->size(); i++) {
  2280. if (';' != aBBoxData->at(i))
  2281. continue;
  2282. buffer = aBBoxData->mid(startPos, i - startPos);
  2283. int bboxData = buffer.toInt(&ok, 10);
  2284. if (!ok) {
  2285. qDebug() <<
  2286. "BBoxFromData: "
  2287. "bbox format is corrupted";
  2288. break;
  2289. }
  2290. if (-1 == bbox.rect.x()) {
  2291. bbox.rect.setX(bboxData);
  2292. bbox.rect.setWidth(-1);
  2293. }
  2294. else if (-1 == bbox.rect.y()) {
  2295. bbox.rect.setY(bboxData);
  2296. bbox.rect.setHeight(-1);
  2297. }
  2298. else if (-1 == bbox.rect.width()) {
  2299. bbox.rect.setWidth(bboxData);
  2300. }
  2301. else if (-1 == bbox.rect.height()) {
  2302. bbox.rect.setHeight(bboxData);
  2303. }
  2304. startPos = i + 1;
  2305. }
  2306. if (!bbox.rect.isValid()) {
  2307. qDebug() <<
  2308. "BBoxFromData: "
  2309. "bbox format is corrupted";
  2310. bbox.rect.setRect(-1, -1, -1, -1);
  2311. }
  2312. else if (!ok) {
  2313. bbox.rect.setRect(-1, -1, -1, -1);
  2314. }
  2315. return bbox;
  2316. }
  2317. BoundingBox
  2318. ImageLabeler::BBoxFromString(
  2319. QString *aString,
  2320. int *oldID
  2321. )
  2322. {
  2323. BoundingBox bbox;
  2324. bbox.label_ID_ = -1;
  2325. *oldID = -1;
  2326. if (!aString) {
  2327. return bbox;
  2328. /* NOTREACHED */
  2329. }
  2330. if (-1 == aString->indexOf("BBox")) {
  2331. return bbox;
  2332. /* NOTREACHED */
  2333. }
  2334. /* getting bbox id in the list(it cannot be changed) */
  2335. bool ok = 0;
  2336. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2337. if (!ok || bboxID <= -1) {
  2338. qDebug() <<
  2339. "BBoxFromString: bboxID is corrupted";
  2340. return bbox;
  2341. /* NOTREACHED */
  2342. }
  2343. /* getting new label id */
  2344. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2345. if (!ok || labelID <= -1) {
  2346. showWarning(
  2347. tr("new LabelID is wrong, area can not be changed")
  2348. );
  2349. return bbox;
  2350. /* NOTREACHED */
  2351. }
  2352. /* getting new points */
  2353. int pointsPos = aString->indexOf("data:") + 5;
  2354. int pointsLen = aString->size() - pointsPos;
  2355. if (pointsLen <= 0) {
  2356. showWarning(
  2357. tr("new data is wrong, area can not be changed")
  2358. );
  2359. return bbox;
  2360. /* NOTREACHED */
  2361. }
  2362. QString pointsData = aString->mid(pointsPos, pointsLen);
  2363. bbox = BBoxFromData(&pointsData);
  2364. bbox.label_ID_ = labelID;
  2365. *oldID = bboxID;
  2366. return bbox;
  2367. }
  2368. void
  2369. ImageLabeler::addPoly(Polygon *poly)
  2370. {
  2371. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2372. return;
  2373. /* NOTREACHED */
  2374. }
  2375. list_polygon_.append(poly);
  2376. addPolyArea(list_polygon_.count() - 1, *poly);
  2377. }
  2378. void
  2379. ImageLabeler::addBBox(BoundingBox *bbox)
  2380. {
  2381. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2382. return;
  2383. /* NOTREACHED */
  2384. }
  2385. list_bounding_box_.append(bbox);
  2386. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2387. }
  2388. void
  2389. ImageLabeler::addPolyFromData(
  2390. QString *aPolyData,
  2391. int *labelID
  2392. )
  2393. {
  2394. Polygon *poly = new Polygon;
  2395. *poly = polyFromData(aPolyData);
  2396. if (poly->poly.isEmpty() || !labelID) {
  2397. return;
  2398. /* NOTREACHED */
  2399. }
  2400. poly->label_ID_ = *labelID;
  2401. list_polygon_.append(poly);
  2402. addPolyArea(list_polygon_.count() - 1, *poly);
  2403. }
  2404. Polygon
  2405. ImageLabeler::polyFromData(
  2406. QString *aPolyData
  2407. )
  2408. {
  2409. Polygon poly;
  2410. poly.label_ID_ = -1;
  2411. QPoint point;
  2412. QString buffer;
  2413. int startPos = 0;
  2414. bool ok = 1;
  2415. bool evenFlag = 0;
  2416. for (int i = 0; i < aPolyData->size(); i++) {
  2417. if (';' != aPolyData->at(i))
  2418. continue;
  2419. buffer = aPolyData->mid(startPos, i - startPos);
  2420. int polyCoor = buffer.toInt(&ok, 10);
  2421. if (!ok) {
  2422. qDebug() <<
  2423. "polyFromData: "
  2424. "poly format is corrupted";
  2425. break;
  2426. }
  2427. if (!evenFlag) {
  2428. point.setX(polyCoor);
  2429. evenFlag = 1;
  2430. }
  2431. else {
  2432. point.setY(polyCoor);
  2433. poly.poly.append(point);
  2434. evenFlag = 0;
  2435. }
  2436. startPos = i + 1;
  2437. }
  2438. if (evenFlag) {
  2439. qDebug() <<
  2440. "polyFromData: "
  2441. "poly format is corrupted";
  2442. poly.poly.clear();
  2443. }
  2444. else if (!ok) {
  2445. poly.poly.clear();
  2446. }
  2447. return poly;
  2448. }
  2449. Polygon
  2450. ImageLabeler::polyFromString(
  2451. QString *aString,
  2452. int *oldID
  2453. )
  2454. {
  2455. Polygon poly;
  2456. poly.label_ID_ = -1;
  2457. *oldID = -1;
  2458. if (!aString) {
  2459. return poly;
  2460. /* NOTREACHED */
  2461. }
  2462. if (-1 == aString->indexOf("Poly")) {
  2463. return poly;
  2464. /* NOTREACHED */
  2465. }
  2466. /* getting poly id in the list(it cannot be changed) */
  2467. bool ok = 0;
  2468. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2469. if (!ok || polyID <= -1) {
  2470. qDebug() <<
  2471. "polyFromString: poly ID is corrupted";
  2472. return poly;
  2473. /* NOTREACHED */
  2474. }
  2475. /* getting new label id */
  2476. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2477. if (!ok || labelID <= -1) {
  2478. showWarning(
  2479. tr("new LabelID is wrong, area can not be changed")
  2480. );
  2481. return poly;
  2482. /* NOTREACHED */
  2483. }
  2484. /* getting new points */
  2485. int pointsPos = aString->indexOf("points:") + 7;
  2486. int pointsLen = aString->size() - pointsPos;
  2487. if (pointsLen <= 0) {
  2488. showWarning(
  2489. tr("new points data is wrong, area can not be changed")
  2490. );
  2491. return poly;
  2492. /* NOTREACHED */
  2493. }
  2494. QString pointsData = aString->mid(pointsPos, pointsLen);
  2495. poly = polyFromData(&pointsData);
  2496. poly.label_ID_ = labelID;
  2497. *oldID = polyID;
  2498. return poly;
  2499. }
  2500. void
  2501. ImageLabeler::showWarning(
  2502. const QString text
  2503. )
  2504. {
  2505. if (text.isEmpty()) {
  2506. return;
  2507. /* NOTREACHED */
  2508. }
  2509. QMessageBox msgBox;
  2510. msgBox.setText(text);
  2511. msgBox.setIcon(QMessageBox::Warning);
  2512. msgBox.exec();
  2513. }
  2514. bool
  2515. ImageLabeler::askForUnsavedData()
  2516. {
  2517. if ((!list_bounding_box_.isEmpty() ||
  2518. !list_polygon_.isEmpty()) &&
  2519. unsaved_data_)
  2520. {
  2521. QMessageBox msgBox;
  2522. msgBox.setText(tr("There is some unsaved data"));
  2523. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2524. msgBox.setStandardButtons(
  2525. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2526. msgBox.setDefaultButton(QMessageBox::Save);
  2527. msgBox.setIcon(QMessageBox::Question);
  2528. int ret = msgBox.exec();
  2529. if (QMessageBox::Save == ret)
  2530. saveAllInfo();
  2531. else if (QMessageBox::Cancel == ret)
  2532. return true;
  2533. }
  2534. return false;
  2535. }
  2536. void
  2537. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2538. {
  2539. if (aButtonPressed)
  2540. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2541. else {
  2542. image_holder_->setTool(ImageHolder::NoTool);
  2543. image_holder_->clearLast();
  2544. }
  2545. }
  2546. void
  2547. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2548. {
  2549. if (aButtonPressed) {
  2550. image_holder_->setTool(ImageHolder::PolygonTool);
  2551. image_holder_->setFocus();
  2552. }
  2553. else {
  2554. image_holder_->setTool(ImageHolder::NoTool);
  2555. image_holder_->clearLast();
  2556. }
  2557. }
  2558. void
  2559. ImageLabeler::onSelectionStarted()
  2560. {
  2561. button_confirm_selection_->setEnabled(true);
  2562. }
  2563. void
  2564. ImageLabeler::onImageScaled()
  2565. {
  2566. }
  2567. void
  2568. ImageLabeler::confirmSelection()
  2569. {
  2570. if (!list_label_->count()) {
  2571. showWarning(tr("You haven't added any label"));
  2572. return;
  2573. /* NOTREACHED */
  2574. }
  2575. image_holder_->confirmSelection();
  2576. if (label_ID_ < 0)
  2577. label_ID_ = 0;
  2578. ImageHolder::Tool tool = image_holder_->tool();
  2579. switch (tool) {
  2580. case ImageHolder::BoundingBoxTool:
  2581. list_bounding_box_.last()->label_ID_ = label_ID_;
  2582. addBBoxArea(
  2583. list_bounding_box_.count() - 1,
  2584. *(list_bounding_box_.last())
  2585. );
  2586. break;
  2587. case ImageHolder::PolygonTool:
  2588. list_polygon_.last()->label_ID_ = label_ID_;
  2589. addPolyArea(
  2590. list_polygon_.count() - 1,
  2591. *(list_polygon_.last())
  2592. );
  2593. break;
  2594. default:
  2595. break;
  2596. }
  2597. button_confirm_selection_->setEnabled(false);
  2598. unsaved_data_ = 1;
  2599. }
  2600. void
  2601. ImageLabeler::clearAll()
  2602. {
  2603. clearLabelList();
  2604. list_areas_->clear();
  2605. list_bounding_box_.clear();
  2606. list_polygon_.clear();
  2607. list_images_->clear();
  2608. list_images_widget_->clear();
  2609. main_label_ = -1;
  2610. image_holder_->clearAll();
  2611. segmented_image_.clear();
  2612. action_view_normal_->setEnabled(false);
  2613. action_view_segmented_->setEnabled(false);
  2614. }
  2615. void
  2616. ImageLabeler::clearAllTool()
  2617. {
  2618. list_areas_->clear();
  2619. list_bounding_box_.clear();
  2620. list_polygon_.clear();
  2621. main_label_ = -1;
  2622. image_holder_->clearAll();
  2623. }
  2624. void ImageLabeler::clearLabelList()
  2625. {
  2626. list_label_->clear();
  2627. addLabel(0, false, "BACKGROUND");
  2628. list_label_->item(0)->setFlags(
  2629. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2630. Qt::ItemIsEnabled
  2631. );
  2632. }
  2633. void ImageLabeler::clearLabelColorList()
  2634. {
  2635. list_label_colors_.clear();
  2636. list_label_colors_.append(0x0);
  2637. }
  2638. void
  2639. ImageLabeler::enableTools()
  2640. {
  2641. action_save_labels_->setEnabled(true);
  2642. action_save_segmented_->setEnabled(true);
  2643. action_save_legend_->setEnabled(true);
  2644. action_bound_box_tool_->setEnabled(true);
  2645. action_polygon_tool_->setEnabled(true);
  2646. action_add_description_->setEnabled(true);
  2647. action_undo_->setEnabled(true);
  2648. action_redo_->setEnabled(true);
  2649. button_bound_box_tool_->setEnabled(true);
  2650. button_polygon_tool_->setEnabled(true);
  2651. button_tagging_tool_->setEnabled(true);
  2652. button_add_label_->setEnabled(true);
  2653. button_remove_label_->setEnabled(true);
  2654. button_prev_image_->setEnabled(true);
  2655. button_next_image_->setEnabled(true);
  2656. button_clear_selection_tool_->setEnabled(true);
  2657. button_generate_colors_->setEnabled(true);
  2658. button_delete_all_labels_->setEnabled(true);
  2659. }
  2660. void
  2661. ImageLabeler::disableTools()
  2662. {
  2663. action_save_labels_->setEnabled(false);
  2664. action_save_segmented_->setEnabled(false);
  2665. action_save_legend_->setEnabled(false);
  2666. action_bound_box_tool_->setEnabled(false);
  2667. action_polygon_tool_->setEnabled(false);
  2668. button_tagging_tool_->setEnabled(false);
  2669. action_add_description_->setEnabled(false);
  2670. action_undo_->setEnabled(false);
  2671. action_redo_->setEnabled(false);
  2672. button_bound_box_tool_->setEnabled(false);
  2673. button_polygon_tool_->setEnabled(false);
  2674. button_add_label_->setEnabled(false);
  2675. button_remove_label_->setEnabled(false);
  2676. button_prev_image_->setEnabled(false);
  2677. button_next_image_->setEnabled(false);
  2678. button_clear_selection_tool_->setEnabled(false);
  2679. button_generate_colors_->setEnabled(false);
  2680. button_delete_all_labels_->setEnabled(false);
  2681. }
  2682. void
  2683. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2684. {
  2685. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2686. QModelIndex index = list_areas_->indexAt(aPos);
  2687. if (-1 == index.row()) {
  2688. return;
  2689. /* NOTREACHED */
  2690. }
  2691. list_areas_->item(index.row())->setSelected(true);
  2692. popup_area_list_->exec(globalPos);
  2693. }
  2694. void
  2695. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2696. {
  2697. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2698. QModelIndex index = list_label_->indexAt(aPos);
  2699. if (-1 == index.row() || !index.row()) {
  2700. return;
  2701. /* NOTREACHED */
  2702. }
  2703. list_label_->item(index.row())->setSelected(true);
  2704. popup_label_list_->exec(globalPos);
  2705. }
  2706. void
  2707. ImageLabeler::setDataFromForm(QString aData)
  2708. {
  2709. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  2710. image_description_ = aData;
  2711. setWindowTitle(image_description_);
  2712. }
  2713. else if (TaggingPurpose == line_edit_form_.purpose()) {
  2714. tags_ = aData;
  2715. }
  2716. }
  2717. void
  2718. ImageLabeler::setPureData()
  2719. {
  2720. /* initializing array */
  2721. if (pure_data_) {
  2722. delete[] *pure_data_;
  2723. delete pure_data_;
  2724. }
  2725. QSize imageSize = image_->size();
  2726. pure_data_ = new int *[imageSize.height()];
  2727. if (!pure_data_) {
  2728. return;
  2729. /* NOTREACHED */
  2730. }
  2731. for (int i = 0; i < imageSize.height(); i++) {
  2732. pure_data_[i] = new int[imageSize.width()];
  2733. if (!pure_data_[i]) {
  2734. return;
  2735. /* NOTREACHED */
  2736. }
  2737. }
  2738. int bboxCnt = list_bounding_box_.count();
  2739. int polyCnt = list_polygon_.count();
  2740. for (int i = 0; i < imageSize.height(); i++)
  2741. for (int j = 0; j < imageSize.width(); j++) {
  2742. pure_data_[i][j] = 0;
  2743. /* bboxes first */
  2744. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2745. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2746. if (bbox->rect.contains(j, i)) {
  2747. pure_data_[i][j] = bbox->label_ID_;
  2748. }
  2749. }
  2750. /* polys next */
  2751. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2752. Polygon *poly = list_polygon_.at(cnt);
  2753. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2754. pure_data_[i][j] = poly->label_ID_;
  2755. }
  2756. }
  2757. }
  2758. }
  2759. void
  2760. ImageLabeler::setLabelColor()
  2761. {
  2762. if (list_label_colors_.count() < list_label_->count()) {
  2763. generateColors();
  2764. }
  2765. QListWidgetItem *current = list_label_->currentItem();
  2766. int labelID = list_label_->row(current);
  2767. QColor defaultColor;
  2768. defaultColor.setRgb(list_label_colors_.at(labelID));
  2769. QColor newColor = QColorDialog::getColor(
  2770. defaultColor,
  2771. list_label_
  2772. );
  2773. if (!newColor.isValid()) {
  2774. return;
  2775. /* NOTREACHED */
  2776. }
  2777. list_label_colors_.takeAt(labelID);
  2778. list_label_colors_.insert(labelID, newColor.rgb());
  2779. QPixmap iconPix = QPixmap(20, 20);
  2780. iconPix.fill(newColor);
  2781. QIcon icon(iconPix);
  2782. current->setIcon(icon);
  2783. image_holder_->update();
  2784. }
  2785. void
  2786. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2787. {
  2788. if (anID < 0) {
  2789. return;
  2790. /* NOTREACHED */
  2791. }
  2792. if (list_label_colors_.count() < list_label_->count()) {
  2793. generateColors();
  2794. }
  2795. QListWidgetItem *item = list_label_->item(anID);
  2796. list_label_colors_.takeAt(anID);
  2797. list_label_colors_.insert(anID, aColor.rgb());
  2798. QPixmap iconPix = QPixmap(20, 20);
  2799. iconPix.fill(aColor);
  2800. QIcon icon(iconPix);
  2801. item->setIcon(icon);
  2802. image_holder_->update();
  2803. }
  2804. void
  2805. ImageLabeler::onOptionsSet()
  2806. {
  2807. auto_color_generation_ = options_form_.autoColorGeneration();
  2808. }
  2809. void
  2810. ImageLabeler::viewNormal()
  2811. {
  2812. if (current_image_.isEmpty()) {
  2813. return;
  2814. /* NOTREACHED */
  2815. }
  2816. image_->load(current_image_);
  2817. image_holder_->setPixmap(*image_);
  2818. action_view_segmented_->setEnabled(true);
  2819. action_view_normal_->setEnabled(false);
  2820. }
  2821. void
  2822. ImageLabeler::viewSegmented()
  2823. {
  2824. if (segmented_image_.isEmpty()) {
  2825. return;
  2826. /* NOTREACHED */
  2827. }
  2828. image_->load(segmented_image_);
  2829. image_holder_->setPixmap(*image_);
  2830. action_view_segmented_->setEnabled(false);
  2831. action_view_normal_->setEnabled(true);
  2832. }
  2833. void
  2834. ImageLabeler::interruptSearch()
  2835. {
  2836. interrupt_search_ = 1;
  2837. }
  2838. void
  2839. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2840. {
  2841. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2842. list_images_->isEmpty())
  2843. {
  2844. return;
  2845. /* NOTREACHED */
  2846. }
  2847. clearAllTool();
  2848. clearLabelList();
  2849. clearLabelColorList();
  2850. image_ID_ = list_images_widget_->row(anItem);
  2851. selectImage(image_ID_);
  2852. // if (list_images_->at(image_ID_).labeled_ &&
  2853. // !list_images_->at(image_ID_).pas_)
  2854. // {
  2855. // list_label_->clear();
  2856. // QString labeled =
  2857. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2858. // labeled.append(".dat");
  2859. // loadInfo(labeled);
  2860. // }
  2861. // else if (list_images_->at(image_ID_).labeled_ &&
  2862. // list_images_->at(image_ID_).pas_)
  2863. // {
  2864. // /* TODO: do the pascal file selecting */
  2865. // showWarning("this function doesn't work at the moment, sorry.");
  2866. // }
  2867. // else {
  2868. // current_image_ = list_images_->at(image_ID_).image_;
  2869. // image_->load(current_image_);
  2870. // image_holder_->setPixmap(*image_);
  2871. // image_holder_->resize(image_->size());
  2872. // }
  2873. }
  2874. bool
  2875. ImageLabeler::selectImage(int anImageID)
  2876. {
  2877. if (anImageID < 0 || list_images_->isEmpty() ||
  2878. list_images_->count() <= anImageID)
  2879. {
  2880. return false;
  2881. /* NOTREACHED */
  2882. }
  2883. if (list_images_->at(anImageID).labeled_ &&
  2884. !list_images_->at(anImageID).pas_)
  2885. {
  2886. list_label_->clear();
  2887. QString labeled =
  2888. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2889. labeled.append(".dat");
  2890. loadInfo(labeled);
  2891. }
  2892. else if (list_images_->at(anImageID).labeled_ &&
  2893. list_images_->at(anImageID).pas_)
  2894. {
  2895. /* TODO: do the pascal file selecting */
  2896. showWarning("this function doesn't work at the moment, sorry.");
  2897. }
  2898. else {
  2899. current_image_ = list_images_->at(anImageID).image_;
  2900. image_->load(current_image_);
  2901. image_holder_->setPixmap(*image_);
  2902. image_holder_->resize(image_->size());
  2903. }
  2904. return true;
  2905. }
  2906. void
  2907. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2908. {
  2909. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2910. QModelIndex index = list_images_widget_->indexAt(aPos);
  2911. if (-1 == index.row()) {
  2912. return;
  2913. /* NOTREACHED */
  2914. }
  2915. list_images_widget_->item(index.row())->setSelected(true);
  2916. popup_images_list_->exec(globalPos);
  2917. }
  2918. void
  2919. ImageLabeler::removeImage()
  2920. {
  2921. int num = list_images_widget_->currentRow();
  2922. list_images_widget_->takeItem(num);
  2923. list_images_->takeAt(num);
  2924. for (int i = num ; i < list_images_widget_->count(); i++) {
  2925. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2926. newStr.prepend(QString("%1: ").arg(i));
  2927. list_images_widget_->item(i)->setText(newStr);
  2928. }
  2929. }
  2930. void
  2931. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2932. {
  2933. QWidget::resizeEvent(anEvent);
  2934. }
  2935. void
  2936. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2937. {
  2938. QWidget::mousePressEvent(anEvent);
  2939. }
  2940. void
  2941. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2942. {
  2943. keyboard_modifier_ = anEvent->modifiers();
  2944. if (Qt::Key_Left == anEvent->key() &&
  2945. Qt::ControlModifier == keyboard_modifier_) {
  2946. prevImage();
  2947. }
  2948. if (Qt::Key_Right == anEvent->key() &&
  2949. Qt::ControlModifier == keyboard_modifier_) {
  2950. nextImage();
  2951. }
  2952. if ((Qt::Key_Enter == anEvent->key() ||
  2953. Qt::Key_Return == anEvent->key()) &&
  2954. // Qt::NoModifier == anEvent->modifiers() &&
  2955. ImageHolder::NewSelection == image_holder_->state()) {
  2956. confirmSelection();
  2957. }
  2958. if ((Qt::Key_Enter == anEvent->key() ||
  2959. Qt::Key_Return == anEvent->key()) &&
  2960. // Qt::NoModifier == anEvent->modifiers() &&
  2961. -1 != image_holder_->focusedSelection()) {
  2962. image_holder_->clearFocusOnArea();
  2963. image_holder_->clearEdition();
  2964. image_holder_->update();
  2965. }
  2966. if (Qt::Key_Escape == anEvent->key()) {
  2967. image_holder_->clearLast();
  2968. image_holder_->clearFocusOnArea();
  2969. image_holder_->clearEdition();
  2970. image_holder_->update();
  2971. }
  2972. if (Qt::Key_Z == anEvent->key() &&
  2973. Qt::ControlModifier == anEvent->modifiers()) {
  2974. image_holder_->undo();
  2975. }
  2976. if (Qt::Key_Y == anEvent->key() &&
  2977. Qt::ControlModifier == anEvent->modifiers()) {
  2978. image_holder_->redo();
  2979. }
  2980. QWidget::keyPressEvent(anEvent);
  2981. }
  2982. void
  2983. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  2984. {
  2985. keyboard_modifier_ = anEvent->modifiers();
  2986. }
  2987. void
  2988. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2989. {
  2990. /* zoomin */
  2991. if (0 < anEvent->delta() &&
  2992. Qt::ControlModifier == keyboard_modifier_) {
  2993. image_holder_->scaleImage(ZoomIn, 1.1);
  2994. }
  2995. /* zoomout */
  2996. else if (anEvent->delta() < 0 &&
  2997. Qt::ControlModifier == keyboard_modifier_) {
  2998. image_holder_->scaleImage(ZoomOut, 1.1);
  2999. }
  3000. }
  3001. /*
  3002. *
  3003. */