ImageLabeler.cpp 79 KB

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