ImageLabeler.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541
  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. aSettings->endGroup();
  706. qDebug() << PASCALpath_;
  707. return true;
  708. }
  709. bool
  710. ImageLabeler::writeSettings(QSettings *aSettings)
  711. {
  712. aSettings->beginGroup("/global");
  713. aSettings->setValue("/auto_label_color_generation", auto_color_generation_);
  714. aSettings->setValue("/PASCAL_root_path", PASCALpath_);
  715. aSettings->endGroup();
  716. return true;
  717. }
  718. void
  719. ImageLabeler::writeSettings()
  720. {
  721. writeSettings(settings_);
  722. }
  723. void
  724. ImageLabeler::addImage(Image *anImage)
  725. {
  726. QListWidgetItem *newItem = new QListWidgetItem;
  727. QString itemText = QString("%1: %2").
  728. arg(list_images_widget_->count()).
  729. arg(getFilenameFromPath(&(anImage->image_)));
  730. if (anImage->labeled_)
  731. itemText.append(" #labeled");
  732. if (anImage->pas_)
  733. itemText.append(" #pas");
  734. newItem->setText(itemText);
  735. list_images_widget_->addItem(newItem);
  736. list_images_->append(*anImage);
  737. }
  738. void
  739. ImageLabeler::addLabel()
  740. {
  741. QListWidgetItem *newItem = new QListWidgetItem;
  742. QString label;
  743. int itemNum = list_label_->count();
  744. label.append(QString("%1: ").arg(itemNum));
  745. if (itemNum) {
  746. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  747. label.append("New label");
  748. }
  749. else {
  750. newItem->setFlags(
  751. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  752. Qt::ItemIsEnabled
  753. );
  754. label.append("BACKGROUND");
  755. }
  756. newItem->setText(label);
  757. QPixmap iconPix = QPixmap(20, 20);
  758. QColor color;
  759. if (0 != itemNum)
  760. color = Qt::white;
  761. else
  762. color = Qt::black;
  763. iconPix.fill(color);
  764. QIcon icon(iconPix);
  765. list_label_colors_.append(color.rgb());
  766. newItem->setIcon(icon);
  767. list_label_->addItem(newItem);
  768. list_label_->setItemSelected(newItem, true);
  769. label_ID_ = list_label_->count() - 1;
  770. unsaved_data_ = 1;
  771. }
  772. void
  773. ImageLabeler::addLabel(
  774. int aLabelID,
  775. bool isMain,
  776. QString aLabel
  777. )
  778. {
  779. QListWidgetItem *newItem = new QListWidgetItem;
  780. if (0 == aLabelID) {
  781. aLabel = QString("BACKGROUND");
  782. newItem->setFlags(
  783. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  784. Qt::ItemIsEnabled
  785. );
  786. }
  787. QString label;
  788. label.append(QString("%1: %2").
  789. arg(aLabelID).
  790. arg(aLabel)
  791. );
  792. /* TODO: check if there is another main label and make it common */
  793. if (isMain) {
  794. main_label_ = aLabelID;
  795. }
  796. QPixmap iconPix = QPixmap(20, 20);
  797. QColor color;
  798. if (0 != aLabelID)
  799. color = Qt::white;
  800. else
  801. color = Qt::black;
  802. iconPix.fill(color);
  803. QIcon icon(iconPix);
  804. list_label_colors_.append(color.rgb());
  805. newItem->setIcon(icon);
  806. newItem->setText(label);
  807. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  808. list_label_->addItem(newItem);
  809. list_label_->setItemSelected(newItem, true);
  810. unsaved_data_ = 1;
  811. }
  812. void
  813. ImageLabeler::editLabel(QListWidgetItem *anItem)
  814. {
  815. QString label = anItem->text();
  816. int itemRow = list_label_->row(anItem);
  817. QString prefix = QString("%1: ").arg(itemRow);
  818. if (-1 != label.indexOf(prefix)) {
  819. return;
  820. /* NOTREACHED */
  821. }
  822. label.prepend(QString("%1: ").arg(itemRow));
  823. if (main_label_ == itemRow) {
  824. label.append(" #main");
  825. }
  826. list_label_->blockSignals(true);
  827. anItem->setText(label);
  828. list_label_->blockSignals(false);
  829. unsaved_data_ = 1;
  830. }
  831. void
  832. ImageLabeler::removeLabel()
  833. {
  834. if (0 == list_label_->count()) {
  835. return;
  836. /* NOTREACHED */
  837. }
  838. //QListWidgetItem *current = list_label_->currentItem();
  839. /* we need to keep BACKGROUND category */
  840. if (label_ID_ < 1) {
  841. return;
  842. /* NOTREACHED */
  843. }
  844. if (list_label_->count() <= label_ID_ ||
  845. list_label_colors_.count() <= label_ID_) {
  846. return;
  847. /* NOTREACHED */
  848. }
  849. list_label_->takeItem(label_ID_);
  850. list_label_colors_.takeAt(label_ID_);
  851. unsaved_data_ = 1;
  852. }
  853. void
  854. ImageLabeler::setLabelID(
  855. QListWidgetItem *anItem
  856. )
  857. {
  858. Q_UNUSED(anItem)
  859. if (!list_label_->count()) {
  860. return;
  861. /* NOTREACHED */
  862. }
  863. label_ID_ = list_label_->row(anItem);
  864. }
  865. void
  866. ImageLabeler::addBBoxArea(
  867. int anID,
  868. BoundingBox aBBox,
  869. int itemID
  870. )
  871. {
  872. QListWidgetItem *newItem = new QListWidgetItem;
  873. if (-1 == itemID)
  874. itemID = list_areas_->count();
  875. QString label;
  876. label.append(QString("%1: ").arg(itemID));
  877. label.append(QString("BBox #%1; ").arg(anID));
  878. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  879. label.append(
  880. QString("data:%1;%2;%3;%4; ").
  881. arg(aBBox.rect.topLeft().x()).
  882. arg(aBBox.rect.topLeft().y()).
  883. arg(aBBox.rect.width()).
  884. arg(aBBox.rect.height())
  885. );
  886. newItem->setText(label);
  887. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  888. list_areas_->insertItem(itemID, newItem);
  889. list_areas_->setItemSelected(newItem, true);
  890. }
  891. void
  892. ImageLabeler::addPolyArea(
  893. int aPolyID,
  894. Polygon aPoly,
  895. int itemID
  896. )
  897. {
  898. QListWidgetItem *newItem = new QListWidgetItem;
  899. if (-1 == itemID)
  900. itemID = list_areas_->count();
  901. QString label;
  902. label.append(QString("%1: ").arg(itemID));
  903. label.append(QString("Poly #%1; ").arg(aPolyID));
  904. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  905. label.append("points:");
  906. for (int i = 0; i < aPoly.poly.count(); i++) {
  907. label.append(
  908. QString("%1;%2;").
  909. arg(aPoly.poly.point(i).x()).
  910. arg(aPoly.poly.point(i).y())
  911. );
  912. }
  913. newItem->setText(label);
  914. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  915. list_areas_->insertItem(itemID, newItem);
  916. list_areas_->setItemSelected(newItem, true);
  917. }
  918. void
  919. ImageLabeler::editArea()
  920. {
  921. bool oldState = list_areas_->blockSignals(true);
  922. if (!list_areas_->currentItem() || !list_areas_->count()) {
  923. return;
  924. /* NOTREACHED */
  925. }
  926. QListWidgetItem *current = list_areas_->currentItem();
  927. old_area_string_ = current->text();
  928. current->setFlags(current->flags() | Qt::ItemIsEditable);
  929. list_areas_->editItem(current);
  930. list_areas_->blockSignals(oldState);
  931. }
  932. void
  933. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  934. {
  935. list_areas_->blockSignals(true);
  936. QString areaString = anItem->text();
  937. int oldID = -1;
  938. if (-1 != areaString.indexOf("Poly")) {
  939. Polygon *poly = new Polygon;
  940. *poly = polyFromString(&areaString, &oldID);
  941. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  942. -1 < oldID) {
  943. list_polygon_.takeAt(oldID);
  944. list_polygon_.insert(oldID, poly);
  945. }
  946. else
  947. anItem->setText(old_area_string_);
  948. }
  949. else if (-1 != areaString.indexOf("BBox")) {
  950. BoundingBox *bbox = new BoundingBox;
  951. *bbox = BBoxFromString(&areaString, &oldID);
  952. if (-1 < bbox->label_ID_ && -1 < oldID) {
  953. list_bounding_box_.takeAt(oldID);
  954. list_bounding_box_.insert(oldID, bbox);
  955. }
  956. else
  957. anItem->setText(old_area_string_);
  958. }
  959. else {
  960. showWarning(tr("record format is corrupted, try again"));
  961. anItem->setText(old_area_string_);
  962. }
  963. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  964. list_areas_->blockSignals(false);
  965. image_holder_->update();
  966. unsaved_data_ = 1;
  967. }
  968. void
  969. ImageLabeler::onAreaEdit()
  970. {
  971. if (!list_areas_->count() ||
  972. -1 == image_holder_->focusedSelection()) {
  973. //showWarning(tr("You haven't added any label"));
  974. return;
  975. /* NOTREACHED */
  976. }
  977. int figureID = image_holder_->focusedSelection();
  978. Figure figure = image_holder_->focusedSelectionType();
  979. for (int i = 0; i < list_areas_->count(); i++) {
  980. QListWidgetItem *item = list_areas_->item(i);
  981. QString text = item->text();
  982. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  983. bool ok = 0;
  984. int num = getNumFromString(&text, "BBox #", ";", &ok);
  985. if (ok && num == figureID) {
  986. list_areas_->takeItem(i);
  987. addBBoxArea(num, *list_bounding_box_.at(num), i);
  988. }
  989. }
  990. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  991. bool ok = 0;
  992. int num = getNumFromString(&text, "Poly #", ";", &ok);
  993. if (ok && num == figureID) {
  994. list_areas_->takeItem(i);
  995. addPolyArea(num, *list_polygon_.at(num), i);
  996. }
  997. }
  998. }
  999. unsaved_data_ = 1;
  1000. }
  1001. void
  1002. ImageLabeler::deleteArea()
  1003. {
  1004. if (!list_areas_->currentItem() || !list_areas_->count()) {
  1005. return;
  1006. /* NOTREACHED */
  1007. }
  1008. QString text = list_areas_->currentItem()->text();
  1009. bool ok = 0;
  1010. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  1011. // if (!ok) {
  1012. // return;
  1013. // /* NOTREACHED */
  1014. // }
  1015. /*
  1016. * 0 - bbox
  1017. * 1 - poly
  1018. */
  1019. QString shape;
  1020. if (-1 != text.indexOf("BBox"))
  1021. shape = QString("BBox");
  1022. else if (-1 != text.indexOf("Poly"))
  1023. shape = QString("Poly");
  1024. else {
  1025. return;
  1026. /* NOTREACHED */
  1027. }
  1028. int areaNum = getNumFromString(&text, "#", ";", &ok);
  1029. if (!ok) {
  1030. return;
  1031. /* NOTREACHED */
  1032. }
  1033. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  1034. /* changing all shapes(depends on current) which are next in the list */
  1035. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  1036. QListWidgetItem item = *(list_areas_->item(i));
  1037. QString newText = item.text();
  1038. if (-1 == newText.indexOf(shape))
  1039. continue;
  1040. int num = getNumFromString(&newText, "#", ";", &ok);
  1041. num--;
  1042. QString numString = QString("%1").arg(num);
  1043. int numPos = newText.indexOf("#") + 1;
  1044. newText.replace(numPos, numString.size(), numString);
  1045. list_areas_->takeItem(i);
  1046. list_areas_->insertItem(i, newText);
  1047. }
  1048. list_areas_->takeItem(currentItemRow);
  1049. if (shape == "BBox")
  1050. list_bounding_box_.removeAt(areaNum);
  1051. else
  1052. list_polygon_.removeAt(areaNum);
  1053. image_holder_->update();
  1054. unsaved_data_ = 1;
  1055. }
  1056. void
  1057. ImageLabeler::toggleLabelPriority()
  1058. {
  1059. if (!list_label_->count()) {
  1060. return;
  1061. /* NOTREACHED */
  1062. }
  1063. QListWidgetItem *item = list_label_->currentItem();
  1064. /* because we need to keep BACKGROUND category */
  1065. if (0 == list_label_->row(item)) {
  1066. return;
  1067. /* NOTREACHED */
  1068. }
  1069. int itemRow = list_label_->row(item);
  1070. QString text = item->text();
  1071. /* cleaning previous " #main" mark */
  1072. if (-1 != main_label_) {
  1073. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1074. QString lastMainText = lastMain->text();
  1075. int mainPos = lastMainText.indexOf(" #main");
  1076. lastMainText = lastMainText.mid(0, mainPos);
  1077. list_label_->blockSignals(true);
  1078. lastMain->setText(lastMainText);
  1079. list_label_->blockSignals(false);
  1080. }
  1081. if (main_label_ == itemRow) {
  1082. int mainPos = text.indexOf(" #main");
  1083. text = text.mid(0, mainPos);
  1084. main_label_ = -1;
  1085. }
  1086. else {
  1087. text.append(" #main");
  1088. main_label_ = list_label_->row(item);
  1089. }
  1090. list_label_->blockSignals(true);
  1091. item->setText(text);
  1092. list_label_->blockSignals(false);
  1093. image_holder_->update();
  1094. unsaved_data_ = 1;
  1095. }
  1096. void
  1097. ImageLabeler::getImagesFromDir(const QDir &dir)
  1098. {
  1099. /* avoiding freezing during recursive search for files */
  1100. QApplication::processEvents();
  1101. if (interrupt_search_) {
  1102. clearAll();
  1103. return;
  1104. }
  1105. QStringList filenameFilter;
  1106. filenameFilter <<
  1107. "*.jpeg" <<
  1108. "*.jpg" <<
  1109. "*.gif" <<
  1110. "*.png" <<
  1111. "*.bmp" <<
  1112. "*.tiff" <<
  1113. "*.dat"
  1114. ;
  1115. QStringList listImages =
  1116. dir.entryList(filenameFilter, QDir::Files);
  1117. QStringList ignoredFiles;
  1118. foreach (QString file, listImages) {
  1119. if (ignoredFiles.contains(file) ||
  1120. file.contains("_segmented", Qt::CaseInsensitive) ||
  1121. file.contains(".dat", Qt::CaseInsensitive)) {
  1122. continue;
  1123. }
  1124. Image newImage;
  1125. newImage.image_ = dir.absoluteFilePath(file);
  1126. /* TODO: think about loading pascal files */
  1127. newImage.pas_ = 0;
  1128. QString labeled = alterFileName(file, "_labeled");
  1129. labeled = removePath(labeled);
  1130. labeled.append(".dat");
  1131. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1132. newImage.labeled_ = 1;
  1133. else
  1134. {
  1135. newImage.labeled_ = 0;
  1136. }
  1137. addImage(&newImage);
  1138. }
  1139. QStringList listDir = dir.entryList(QDir::Dirs);
  1140. foreach (QString subdir, listDir) {
  1141. if ("." == subdir || ".." == subdir)
  1142. continue;
  1143. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1144. }
  1145. }
  1146. void
  1147. ImageLabeler::nextImage()
  1148. {
  1149. if (list_images_->isEmpty()) {
  1150. return;
  1151. /* NOTREACHED */
  1152. }
  1153. if (askForUnsavedData()) {
  1154. return;
  1155. /* NOTREACHED */
  1156. }
  1157. if (list_images_widget_->count() - 1 == image_ID_) {
  1158. //current_image_ = list_images_->at(0).image_;
  1159. image_ID_ = 0;
  1160. }
  1161. else {
  1162. image_ID_ ++;
  1163. //current_image_ = list_images_->at(image_ID_).image_;
  1164. }
  1165. if (!selectImage(image_ID_)) {
  1166. showWarning(tr("Next image is not available"));
  1167. return;
  1168. /* NOTREACHED */
  1169. }
  1170. list_images_widget_->setCurrentRow(image_ID_);
  1171. if (current_image_.isEmpty()) {
  1172. return;
  1173. /* NOTREACHED */
  1174. }
  1175. QString winTitle;
  1176. winTitle.append("ImageLabeler - ");
  1177. winTitle.append(current_image_);
  1178. setWindowTitle(winTitle);
  1179. image_->load(current_image_);
  1180. image_holder_->resize(image_->size());
  1181. image_holder_->setPixmap(*image_);
  1182. list_bounding_box_.clear();
  1183. list_polygon_.clear();
  1184. list_areas_->clear();
  1185. image_holder_->clearAll();
  1186. segmented_image_.clear();
  1187. clearLabelColorList();
  1188. //setWindowTitle(tr("ImageLabeler"));
  1189. }
  1190. void
  1191. ImageLabeler::prevImage()
  1192. {
  1193. if (!list_images_widget_->count()) {
  1194. return;
  1195. /* NOTREACHED */
  1196. }
  1197. if (askForUnsavedData()) {
  1198. return;
  1199. /* NOTREACHED */
  1200. }
  1201. if (!image_ID_) {
  1202. image_ID_ = list_images_widget_->count() - 1;
  1203. //current_image_ = list_images_->at(image_ID_).image_;
  1204. }
  1205. else {
  1206. image_ID_--;
  1207. //current_image_ = list_images_->at(image_ID_).image_;
  1208. }
  1209. list_images_widget_->setCurrentRow(image_ID_);
  1210. if (!selectImage(image_ID_)) {
  1211. showWarning(tr("Next image is not available"));
  1212. return;
  1213. /* NOTREACHED */
  1214. }
  1215. QString winTitle;
  1216. winTitle.append("ImageLabeler - ");
  1217. winTitle.append(current_image_);
  1218. setWindowTitle(winTitle);
  1219. image_->load(current_image_);
  1220. image_holder_->resize(image_->size());
  1221. image_holder_->setPixmap(*image_);
  1222. list_bounding_box_.clear();
  1223. list_polygon_.clear();
  1224. list_areas_->clear();
  1225. image_holder_->clearAll();
  1226. segmented_image_.clear();
  1227. clearLabelColorList();
  1228. //setWindowTitle(tr("ImageLabeler"));
  1229. }
  1230. void
  1231. ImageLabeler::saveAllInfo()
  1232. {
  1233. if (!list_images_widget_->count()) {
  1234. showWarning("You have not opened any image yet");
  1235. return;
  1236. /* NOTREACHED */
  1237. }
  1238. /* ------------------------------------------------------------------------
  1239. * XML part
  1240. */
  1241. QDomDocument doc(tr("ImageLabeler"));
  1242. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1243. doc.appendChild(root);
  1244. QDomElement image = doc.createElement(tr("image"));
  1245. root.appendChild(image);
  1246. QDomText pathToImage = doc.createTextNode(current_image_);
  1247. image.appendChild(pathToImage);
  1248. if (!segmented_image_.isEmpty()) {
  1249. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1250. root.appendChild(segmentedImage);
  1251. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1252. segmentedImage.appendChild(pathToSegmented);
  1253. }
  1254. QDomElement description = doc.createElement(tr("description"));
  1255. root.appendChild(description);
  1256. QDomText descriptionText = doc.createTextNode(image_description_);
  1257. description.appendChild(descriptionText);
  1258. QDomElement tags = doc.createElement(tr("tags"));
  1259. root.appendChild(tags);
  1260. QDomText tagsText = doc.createTextNode(tags_);
  1261. tags.appendChild(tagsText);
  1262. legendToXml(&doc, &root);
  1263. objectsToXml(&doc, &root);
  1264. setPureData();
  1265. QDomElement pureData = doc.createElement(tr("pure_data"));
  1266. QSize imageSize = image_->size();
  1267. QString pixelValues;
  1268. for (int i = 0; i < imageSize.height(); i++) {
  1269. for (int j = 0; j < imageSize.width(); j++) {
  1270. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1271. //pixelValues.append(QString(""));
  1272. }
  1273. pixelValues.append("\n");
  1274. }
  1275. QDomText pureDataText = doc.createTextNode(pixelValues);
  1276. pureData.appendChild(pureDataText);
  1277. root.appendChild(pureData);
  1278. QString xml = doc.toString();
  1279. /* ------------------------------------------------------------------------
  1280. * XML part ends
  1281. */
  1282. QFileDialog fileDialog(0, tr("Save all info"));
  1283. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1284. fileDialog.setDefaultSuffix("dat");
  1285. fileDialog.setFileMode(QFileDialog::AnyFile);
  1286. QString dir = getDirFromPath(&current_image_);
  1287. /* altering the name of a new file */
  1288. QString newFileName = alterFileName(current_image_, "_labeled");
  1289. newFileName = removePath(newFileName);
  1290. fileDialog.selectFile(newFileName);
  1291. fileDialog.setDirectory(dir);
  1292. QString filename;
  1293. if (fileDialog.exec()) {
  1294. filename = fileDialog.selectedFiles().last();
  1295. }
  1296. else {
  1297. //showWarning(tr("Can not open file dialog"));
  1298. return;
  1299. /* NOTREACHED */
  1300. }
  1301. if (filename.isEmpty()) {
  1302. return;
  1303. /* NOTREACHED */
  1304. }
  1305. QFile file(filename);
  1306. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1307. showWarning(tr("Can not open file for writing"));
  1308. return;
  1309. /* NOTREACHED */
  1310. }
  1311. file.write(xml.toLocal8Bit());
  1312. file.close();
  1313. unsaved_data_ = 0;
  1314. }
  1315. void
  1316. ImageLabeler::saveSegmentedPicture()
  1317. {
  1318. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1319. return;
  1320. /* NOTREACHED */
  1321. }
  1322. setPureData();
  1323. // QString suffix = *current_image_;
  1324. // int dotPos = suffix.lastIndexOf(".");
  1325. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1326. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1327. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1328. fileDialog.setDefaultSuffix("png");
  1329. fileDialog.setFileMode(QFileDialog::AnyFile);
  1330. QString dir = getDirFromPath(&current_image_);
  1331. /* altering the name of a new file */
  1332. QString newFileName = alterFileName(current_image_, "_segmented");
  1333. newFileName = removePath(newFileName);
  1334. fileDialog.selectFile(newFileName);
  1335. fileDialog.setDirectory(dir);
  1336. QString filename;
  1337. if (fileDialog.exec()) {
  1338. filename = fileDialog.selectedFiles().last();
  1339. }
  1340. else {
  1341. return;
  1342. /* NOTREACHED */
  1343. }
  1344. QSize imageSize = image_holder_->pixmap()->size();
  1345. QImage newImage(imageSize, QImage::Format_RGB32);
  1346. bool generateColorsFlag = auto_color_generation_;
  1347. bool flag = 0;
  1348. /* checking if all the colors are are different from white(default) */
  1349. if (!generateColorsFlag) {
  1350. for (int i = 1; i < list_label_->count(); i++) {
  1351. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1352. flag = 1;
  1353. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1354. QMessageBox msgBox;
  1355. msgBox.setText(tr("There are few labels with default white color."));
  1356. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1357. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1358. msgBox.setDefaultButton(QMessageBox::Yes);
  1359. msgBox.setIcon(QMessageBox::Question);
  1360. int ret = msgBox.exec();
  1361. if (QMessageBox::Yes == ret) {
  1362. generateColorsFlag = 1;
  1363. break;
  1364. }
  1365. else {
  1366. return;
  1367. /* NOTREACHED */
  1368. }
  1369. }
  1370. }
  1371. }
  1372. /* generating colors for labels */
  1373. if (list_label_colors_.count() < list_label_->count() ||
  1374. generateColorsFlag) {
  1375. generateColors();
  1376. }
  1377. for (int i = 0; i < imageSize.height(); i++)
  1378. for (int j = 0; j < imageSize.width(); j++) {
  1379. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1380. }
  1381. if (!newImage.save(filename, "png", 100)) {
  1382. showWarning(tr("An error occurred while saving the segmented image"));
  1383. return;
  1384. /* NOTREACHED */
  1385. }
  1386. segmented_image_ = filename;
  1387. action_view_segmented_->setEnabled(true);
  1388. }
  1389. void
  1390. ImageLabeler::saveLegend()
  1391. {
  1392. if (!list_label_->count()) {
  1393. showWarning("You have not added any label yet");
  1394. return;
  1395. /* NOTREACHED */
  1396. }
  1397. /* ------------------------------------------------------------------------
  1398. * XML part
  1399. */
  1400. QDomDocument doc(tr("ImageLabeler"));
  1401. QDomElement root = doc.createElement(tr("root"));
  1402. doc.appendChild(root);
  1403. legendToXml(&doc, &root);
  1404. QString xml = doc.toString();
  1405. /* ------------------------------------------------------------------------
  1406. * XML part ends
  1407. */
  1408. QFileDialog fileDialog(0, tr("Save legend"));
  1409. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1410. fileDialog.setDefaultSuffix("dat");
  1411. fileDialog.setFileMode(QFileDialog::AnyFile);
  1412. QString dir = getDirFromPath(&current_image_);
  1413. /* altering the name of a new file */
  1414. QString newFileName = alterFileName(current_image_, "_legend");
  1415. newFileName = removePath(newFileName);
  1416. fileDialog.selectFile(newFileName);
  1417. fileDialog.setDirectory(dir);
  1418. QString filename;
  1419. if (fileDialog.exec()) {
  1420. filename = fileDialog.selectedFiles().last();
  1421. }
  1422. else {
  1423. //showWarning(tr("Can not open file dialog"));
  1424. return;
  1425. /* NOTREACHED */
  1426. }
  1427. if (filename.isEmpty()) {
  1428. return;
  1429. /* NOTREACHED */
  1430. }
  1431. QFile file(filename);
  1432. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1433. showWarning(tr("Can not open file for writing"));
  1434. return;
  1435. /* NOTREACHED */
  1436. }
  1437. file.write(xml.toLocal8Bit());
  1438. file.close();
  1439. }
  1440. void
  1441. ImageLabeler::loadInfo()
  1442. {
  1443. if (askForUnsavedData()) {
  1444. return;
  1445. /* NOTREACHED */
  1446. }
  1447. QFileDialog fileDialog(0, tr("Load file with info"));
  1448. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1449. fileDialog.setDefaultSuffix("dat");
  1450. fileDialog.setFileMode(QFileDialog::AnyFile);
  1451. QString filename;
  1452. if (fileDialog.exec()) {
  1453. filename = fileDialog.selectedFiles().last();
  1454. }
  1455. else {
  1456. //showWarning(tr("Can not open file dialog"));
  1457. return;
  1458. /* NOTREACHED */
  1459. }
  1460. clearAllTool();
  1461. clearLabelList();
  1462. if (loadInfo(filename)) {
  1463. enableTools();
  1464. Image newImage;
  1465. newImage.image_ = current_image_;
  1466. newImage.labeled_ = 1;
  1467. newImage.pas_ = 0;
  1468. addImage(&newImage);
  1469. image_ID_ = list_images_widget_->count() - 1;
  1470. list_images_widget_->setCurrentRow(image_ID_);
  1471. }
  1472. unsaved_data_ = 0;
  1473. }
  1474. bool
  1475. ImageLabeler::loadInfo(QString filename)
  1476. {
  1477. QDomDocument doc("Image Labeler");
  1478. QFile file(filename);
  1479. if (!file.open(QIODevice::ReadOnly)) {
  1480. showWarning(tr("Can not open such file"));
  1481. return false;
  1482. /* NOTREACHED */
  1483. }
  1484. QString errMsg;
  1485. if (!doc.setContent(&file, &errMsg)) {
  1486. showWarning(errMsg);
  1487. file.close();
  1488. return false;
  1489. /* NOTREACHED */
  1490. }
  1491. file.close();
  1492. //clearAll();
  1493. //enableTools();
  1494. /* getting all info */
  1495. QDomElement elements = doc.documentElement();
  1496. QDomNode rootNode = elements.firstChild();
  1497. QString string;
  1498. while(!rootNode.isNull()) {
  1499. QDomElement element = rootNode.toElement();
  1500. if(!element.isNull()) {
  1501. /* path to the image */
  1502. if (element.tagName() == "image") {
  1503. string = element.text();
  1504. if (string.isEmpty()) {
  1505. showWarning(
  1506. tr(
  1507. "The file with data doesn't contain path to the image"
  1508. )
  1509. );
  1510. return false;
  1511. /* NOTREACHED */
  1512. }
  1513. if (!image_->load(string)) {
  1514. return false;
  1515. /* NOTREACHED */
  1516. }
  1517. // Image newImage;
  1518. // newImage.image_ = string;
  1519. // newImage.labeled_ = 1;
  1520. // addImage(&newImage);
  1521. current_image_ = string;
  1522. // image_ID_ = list_images_widget_->count() - 1;
  1523. // list_images_widget_->setCurrentRow(image_ID_);
  1524. QString winTitle;
  1525. winTitle.append("ImageLabeler - ");
  1526. winTitle.append(current_image_);
  1527. setWindowTitle(winTitle);
  1528. image_holder_->resize(image_->size());
  1529. image_holder_->setPixmap(*image_);
  1530. }
  1531. /* path to the segmented image */
  1532. if (element.tagName() == "segmented") {
  1533. string = element.text();
  1534. if (string.isEmpty()) {
  1535. continue;
  1536. }
  1537. segmented_image_ = string;
  1538. action_view_segmented_->setEnabled(true);
  1539. }
  1540. /* image description */
  1541. else if (element.tagName() == "description" &&
  1542. !element.text().isEmpty()) {
  1543. image_description_ = element.text();
  1544. }
  1545. /* legend */
  1546. else if (element.tagName() == "legend") {
  1547. loadLegendFromNode(&element);
  1548. }
  1549. /* objects */
  1550. else if (element.tagName() == "objects") {
  1551. QDomNode subNode = element.firstChild();
  1552. QDomElement subElement;
  1553. while(!subNode.isNull()) {
  1554. subElement = subNode.toElement();
  1555. if (subElement.isNull() || subElement.text().isEmpty()) {
  1556. subNode = subNode.nextSibling();
  1557. continue;
  1558. }
  1559. string = subElement.attribute("id");
  1560. bool ok = 1;
  1561. int id = string.toInt(&ok, 10);
  1562. if (!ok) {
  1563. qDebug() <<
  1564. "loadInfo: "
  1565. "poly id format is corrupted";
  1566. subNode = subNode.nextSibling();
  1567. continue;
  1568. }
  1569. string = subElement.text();
  1570. if (subElement.tagName() == "bbox") {
  1571. addBBoxFromData(&string, &id);
  1572. }
  1573. if (subElement.tagName() == "poly") {
  1574. addPolyFromData(&string, &id);
  1575. }
  1576. subNode = subNode.nextSibling();
  1577. }
  1578. }
  1579. }
  1580. rootNode = rootNode.nextSibling();
  1581. }
  1582. unsaved_data_ = 0;
  1583. return true;
  1584. }
  1585. void
  1586. ImageLabeler::loadPascalFile()
  1587. {
  1588. if (askForUnsavedData()) {
  1589. return;
  1590. /* NOTREACHED */
  1591. }
  1592. if (PASCALpath_.isEmpty()) {
  1593. showWarning(tr("before opening first PASCAL file please choose \"root\" directory"
  1594. " where a folder with segmentations,"
  1595. " a folder with polygons,"
  1596. " a folder with image descriptions and"
  1597. " a folder with the pure images are."));
  1598. QFileDialog fileDialog(0, tr("root directory for the PASCAL files"));
  1599. fileDialog.setFileMode(QFileDialog::Directory);
  1600. if (fileDialog.exec())
  1601. PASCALpath_ = fileDialog.selectedFiles().last();
  1602. else {
  1603. return;
  1604. /* NOTREACHED */
  1605. }
  1606. }
  1607. QFileDialog fileDialog(0, tr("Load pascal file"));
  1608. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1609. fileDialog.setDefaultSuffix("xml");
  1610. fileDialog.setFileMode(QFileDialog::AnyFile);
  1611. QStringList filters;
  1612. filters << "PASCAL xml data (*.xml)"
  1613. << "Any files (*)";
  1614. fileDialog.setNameFilters(filters);
  1615. QString filename;
  1616. if (fileDialog.exec()) {
  1617. filename = fileDialog.selectedFiles().last();
  1618. }
  1619. else {
  1620. //showWarning(tr("Can not open file dialog"));
  1621. return;
  1622. /* NOTREACHED */
  1623. }
  1624. clearAllTool();
  1625. clearLabelList();
  1626. if (loadPascalFile(filename, PASCALpath_)) {
  1627. enableTools();
  1628. Image newImage;
  1629. newImage.image_ = current_image_;
  1630. newImage.labeled_ = 1;
  1631. newImage.pas_ = 1;
  1632. addImage(&newImage);
  1633. image_ID_ = list_images_widget_->count() - 1;
  1634. list_images_widget_->setCurrentRow(image_ID_);
  1635. }
  1636. unsaved_data_ = 0;
  1637. }
  1638. bool
  1639. ImageLabeler::loadPascalFile(QString aFilename, QString aPath)
  1640. {
  1641. QDomDocument doc;
  1642. QFile file(aFilename);
  1643. if (!file.open(QIODevice::ReadOnly)) {
  1644. showWarning(tr("Can not open such file"));
  1645. return false;
  1646. /* NOTREACHED */
  1647. }
  1648. QString errMsg;
  1649. if (!doc.setContent(&file, &errMsg)) {
  1650. showWarning(errMsg);
  1651. file.close();
  1652. return false;
  1653. /* NOTREACHED */
  1654. }
  1655. file.close();
  1656. //clearAll();
  1657. //enableTools();
  1658. /* getting all info */
  1659. QDomElement elements = doc.documentElement();
  1660. QDomNode rootNode = elements.firstChild();
  1661. QString string;
  1662. QString path;
  1663. if (aPath.isEmpty())
  1664. path = getPathFromFilename(aFilename);
  1665. else
  1666. path = aPath + "/";
  1667. QString filename;
  1668. QStringList labels;
  1669. labels << "BACKGROUND";
  1670. int labelID;
  1671. while(!rootNode.isNull()) {
  1672. QDomElement element = rootNode.toElement();
  1673. if(!element.isNull()) {
  1674. /* folder */
  1675. if (element.tagName() == "folder") {
  1676. string = element.text();
  1677. if (!string.isEmpty()) {
  1678. path.append(string);
  1679. }
  1680. }
  1681. /* filename */
  1682. else if (element.tagName() == "filename") {
  1683. string = element.text();
  1684. if (!string.isEmpty()) {
  1685. filename = string;
  1686. }
  1687. }
  1688. /* object */
  1689. else if (element.tagName() == "object") {
  1690. QDomNode subNode = element.firstChild();
  1691. QDomElement subElement;
  1692. while(!subNode.isNull()) {
  1693. subElement = subNode.toElement();
  1694. if (subElement.isNull() || subElement.text().isEmpty()) {
  1695. subNode = subNode.nextSibling();
  1696. continue;
  1697. }
  1698. /* label */
  1699. if (subElement.tagName() == "name") {
  1700. string = subElement.text();
  1701. if (!string.isEmpty() &&
  1702. !labels.contains(string, Qt::CaseInsensitive))
  1703. {
  1704. addLabel(list_label_->count(), 0, string);
  1705. labelID = labels.count();
  1706. labels << string;
  1707. }
  1708. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1709. for (int i = 0; i < labels.count(); i++) {
  1710. if (labels.at(i) == string)
  1711. labelID = i;
  1712. }
  1713. }
  1714. }
  1715. if (subElement.tagName() == "bndbox") {
  1716. /* 2 points */
  1717. QPoint topLeft;
  1718. QPoint bottomRight;
  1719. QDomNode bboxNode = subElement.firstChild();
  1720. QDomElement bboxElement;
  1721. while(!bboxNode.isNull()) {
  1722. bboxElement = bboxNode.toElement();
  1723. string.clear();
  1724. bool ok = 1;
  1725. if (bboxElement.tagName() == "xmin") {
  1726. string = bboxElement.text();
  1727. if (string.isEmpty())
  1728. ok = 0;
  1729. double xmin = string.toDouble(&ok);
  1730. if (ok)
  1731. topLeft.setX(qRound(xmin));
  1732. }
  1733. else if (bboxElement.tagName() == "ymin") {
  1734. string = bboxElement.text();
  1735. if (string.isEmpty())
  1736. ok = 0;
  1737. double ymin = string.toDouble(&ok);
  1738. if (ok)
  1739. topLeft.setY(qRound(ymin));
  1740. }
  1741. else if (bboxElement.tagName() == "xmax") {
  1742. string = bboxElement.text();
  1743. if (string.isEmpty())
  1744. ok = 0;
  1745. double xmax = string.toDouble(&ok);
  1746. if (ok)
  1747. bottomRight.setX(qRound(xmax));
  1748. }
  1749. else if (bboxElement.tagName() == "ymax") {
  1750. string = bboxElement.text();
  1751. if (string.isEmpty())
  1752. ok = 0;
  1753. double ymax = string.toDouble(&ok);
  1754. if (ok)
  1755. bottomRight.setY(qRound(ymax));
  1756. }
  1757. if (string.isEmpty() || !ok)
  1758. break;
  1759. bboxNode = bboxNode.nextSibling();
  1760. if (bboxNode.isNull()) {
  1761. BoundingBox *bbox = new BoundingBox;
  1762. bbox->rect.setTopLeft(topLeft);
  1763. bbox->rect.setBottomRight(bottomRight);
  1764. bbox->label_ID_ = labelID;
  1765. addBBox(bbox);
  1766. }
  1767. }
  1768. }
  1769. subNode = subNode.nextSibling();
  1770. }
  1771. }
  1772. }
  1773. rootNode = rootNode.nextSibling();
  1774. }
  1775. if (!image_->load(path + "/" + filename)) {
  1776. return false;
  1777. /* NOTREACHED */
  1778. }
  1779. current_image_ = path + filename;
  1780. QString winTitle;
  1781. winTitle.append("ImageLabeler - ");
  1782. winTitle.append(current_image_);
  1783. setWindowTitle(winTitle);
  1784. image_holder_->resize(image_->size());
  1785. image_holder_->setPixmap(*image_);
  1786. unsaved_data_ = 0;
  1787. return true;
  1788. }
  1789. void
  1790. ImageLabeler::loadPascalPolys()
  1791. {
  1792. if (current_image_.isEmpty()) {
  1793. return;
  1794. /* NOTREACHED */
  1795. }
  1796. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1797. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1798. fileDialog.setDefaultSuffix("polygon");
  1799. fileDialog.setFileMode(QFileDialog::AnyFile);
  1800. QStringList filters;
  1801. filters << "PASCAL polygons (*.polygon)"
  1802. << "Any files (*)";
  1803. fileDialog.setNameFilters(filters);
  1804. QString filename;
  1805. if (fileDialog.exec()) {
  1806. filename = fileDialog.selectedFiles().last();
  1807. }
  1808. else {
  1809. //showWarning(tr("Can not open file dialog"));
  1810. return;
  1811. /* NOTREACHED */
  1812. }
  1813. //clearAllTool();
  1814. //clearLabelList();
  1815. if (!loadPascalPolys(filename)) {
  1816. showWarning(tr("File format is corrupted."));
  1817. }
  1818. unsaved_data_ = 0;
  1819. image_holder_->update();
  1820. }
  1821. bool
  1822. ImageLabeler::loadPascalPolys(QString aFilename)
  1823. {
  1824. QFile file(aFilename);
  1825. if (!file.open(QIODevice::ReadOnly)) {
  1826. showWarning(tr("Can not open such file"));
  1827. return false;
  1828. /* NOTREACHED */
  1829. }
  1830. QByteArray data = file.readAll();
  1831. file.close();
  1832. QString label;
  1833. int lastSpace = 0;
  1834. int pointCount = 0;
  1835. Polygon *poly = 0;
  1836. QPoint point;
  1837. bool evenFlag = 0;
  1838. //for (int i = 0; i < data.length(); i++) {
  1839. int i = 0;
  1840. while (i < data.length()) {
  1841. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1842. label = QString(data.mid(lastSpace, i - lastSpace));
  1843. lastSpace = i + 1;
  1844. }
  1845. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1846. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1847. bool ok = 1;
  1848. pointCount = num.toInt(&ok, 10);
  1849. if (!ok) {
  1850. return false;
  1851. /* NOTREACHED */
  1852. }
  1853. poly = new Polygon;
  1854. poly->label_ID_ = -1;
  1855. lastSpace = i + 1;
  1856. }
  1857. else if (data.at(i) == ' ' &&
  1858. !label.isEmpty() &&
  1859. pointCount &&
  1860. !evenFlag)
  1861. {
  1862. evenFlag = 1;
  1863. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1864. bool ok = 1;
  1865. int coor = qRound(num.toDouble(&ok));
  1866. if (!ok) {
  1867. return false;
  1868. /* NOTREACHED */
  1869. }
  1870. point.setX(coor);
  1871. lastSpace = i + 1;
  1872. }
  1873. else if (data.at(i) == ' ' &&
  1874. !label.isEmpty() &&
  1875. pointCount &&
  1876. evenFlag)
  1877. {
  1878. evenFlag = 0;
  1879. pointCount--;
  1880. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1881. bool ok = 1;
  1882. int coor = qRound(num.toDouble(&ok));
  1883. if (!ok) {
  1884. return false;
  1885. /* NOTREACHED */
  1886. }
  1887. point.setY(coor);
  1888. lastSpace = i + 1;
  1889. poly->poly << point;
  1890. }
  1891. else if (data.at(i) == '\n') {
  1892. if (pointCount || !poly) {
  1893. return false;
  1894. /* NOTREACHED */
  1895. }
  1896. lastSpace = i + 1;
  1897. for (int j = 0; j < list_label_->count(); j++) {
  1898. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1899. poly->label_ID_ = j;
  1900. }
  1901. }
  1902. if (-1 == poly->label_ID_) {
  1903. int labelID = list_label_->count();
  1904. addLabel(labelID, 0, label);
  1905. poly->label_ID_ = labelID;
  1906. }
  1907. addPoly(poly);
  1908. label.clear();
  1909. }
  1910. i++;
  1911. }
  1912. //clearAll();
  1913. //enableTools();
  1914. return true;
  1915. }
  1916. void
  1917. ImageLabeler::loadImage()
  1918. {
  1919. if (askForUnsavedData()) {
  1920. return;
  1921. /* NOTREACHED */
  1922. }
  1923. QFileDialog fileDialog(0, tr("Load image"));
  1924. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1925. QStringList filters;
  1926. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1927. << "Any files (*)";
  1928. fileDialog.setNameFilters(filters);
  1929. QString filename;
  1930. if (fileDialog.exec()) {
  1931. filename = fileDialog.selectedFiles().last();
  1932. }
  1933. else {
  1934. //showWarning(tr("Could not open file dialog"));
  1935. return;
  1936. /* NOTREACHED */
  1937. }
  1938. if (filename.isEmpty()) {
  1939. return;
  1940. /* NOTREACHED */
  1941. }
  1942. QString dirPath = getPathFromFilename(filename);
  1943. QDir dir(dirPath);
  1944. QStringList filter;
  1945. filter << "*.dat";
  1946. QStringList fileList = dir.entryList(filter, QDir::Files);
  1947. QString labeled = alterFileName(filename, "_labeled");
  1948. labeled = removePath(labeled);
  1949. labeled.append(".dat");
  1950. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1951. labeled = dir.absoluteFilePath(labeled);
  1952. loadInfo(labeled);
  1953. Image *newImage = new Image;
  1954. newImage->image_ = filename;
  1955. newImage->labeled_ = 1;
  1956. newImage->pas_ = 0;
  1957. addImage(newImage);
  1958. enableTools();
  1959. return;
  1960. /* NOTREACHED */
  1961. }
  1962. clearAllTool();
  1963. QString winTitle;
  1964. winTitle.append("ImageLabeler - ");
  1965. winTitle.append(current_image_);
  1966. setWindowTitle(winTitle);
  1967. if (!image_->load(filename)) {
  1968. return;
  1969. /* NOTREACHED */
  1970. }
  1971. image_holder_->resize(image_->size());
  1972. image_holder_->setPixmap(*image_);
  1973. current_image_ = filename;
  1974. image_ID_ = list_images_widget_->count() - 1;
  1975. list_images_widget_->setCurrentRow(image_ID_);
  1976. Image *newImage = new Image;
  1977. newImage->image_ = filename;
  1978. newImage->labeled_ = 0;
  1979. newImage->pas_ = 0;
  1980. addImage(newImage);
  1981. if (!list_images_widget_->count()) {
  1982. return;
  1983. /* NOTREACHED */
  1984. }
  1985. enableTools();
  1986. }
  1987. void
  1988. ImageLabeler::loadImages()
  1989. {
  1990. if (askForUnsavedData()) {
  1991. return;
  1992. /* NOTREACHED */
  1993. }
  1994. QFileDialog fileDialog(0, tr("Load images"));
  1995. fileDialog.setFileMode(QFileDialog::Directory);
  1996. QString dirName("");
  1997. if (fileDialog.exec()) {
  1998. /* TODO: make it possible to select multiple folders */
  1999. dirName = fileDialog.selectedFiles().last();
  2000. }
  2001. else {
  2002. //showWarning(tr("Could not open file dialog"));
  2003. return;
  2004. /* NOTREACHED */
  2005. }
  2006. clearAllTool();
  2007. QWidget *searchInProgress = new QWidget(0);
  2008. searchInProgress->setWindowTitle(tr("Loading images"));
  2009. QLabel *info = new QLabel(searchInProgress);
  2010. info->setText(
  2011. tr("Program is looking for all image files in your directory recursively."));
  2012. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  2013. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  2014. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  2015. searchInProgress->setLayout(layout);
  2016. layout->addWidget(info);
  2017. layout->addWidget(cancel);
  2018. searchInProgress->adjustSize();
  2019. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  2020. searchInProgress->show();
  2021. getImagesFromDir(QDir(dirName));
  2022. cancel->disconnect();
  2023. searchInProgress->hide();
  2024. delete info;
  2025. delete cancel;
  2026. delete layout;
  2027. delete searchInProgress;
  2028. if (interrupt_search_) {
  2029. interrupt_search_ = 0;
  2030. return;
  2031. /* NOTREACHED */
  2032. }
  2033. if (!list_images_widget_->count()) {
  2034. showWarning(tr("The folder you selected contains no images"));
  2035. return;
  2036. /* NOTREACHED */
  2037. }
  2038. bool ret = 0;
  2039. if (list_images_->at(0).labeled_) {
  2040. QString labeled =
  2041. alterFileName(list_images_->at(0).image_, "_labeled");
  2042. labeled.append(".dat");
  2043. ret = loadInfo(labeled);
  2044. }
  2045. else
  2046. ret = image_->load(list_images_->at(0).image_);
  2047. if (!ret) {
  2048. return;
  2049. /* NOTREACHED */
  2050. }
  2051. current_image_ = list_images_->at(0).image_;
  2052. image_ID_ = 0;
  2053. list_images_widget_->setCurrentRow(image_ID_);
  2054. QString winTitle;
  2055. winTitle.append("ImageLabeler - ");
  2056. winTitle.append(current_image_);
  2057. setWindowTitle(winTitle);
  2058. image_holder_->resize(image_->size());
  2059. image_holder_->setPixmap(*image_);
  2060. enableTools();
  2061. }
  2062. void
  2063. ImageLabeler::loadLegendFromFile()
  2064. {
  2065. QFileDialog fileDialog(0, tr("Load legend"));
  2066. fileDialog.setFileMode(QFileDialog::AnyFile);
  2067. QString filename;
  2068. if (fileDialog.exec()) {
  2069. filename = fileDialog.selectedFiles().last();
  2070. }
  2071. else {
  2072. //showWarning(tr("Could not open file dialog"));
  2073. return;
  2074. /* NOTREACHED */
  2075. }
  2076. QDomDocument doc("Image Labeler");
  2077. QFile file(filename);
  2078. if (!file.open(QIODevice::ReadOnly)) {
  2079. showWarning(tr("Can not open such file"));
  2080. return;
  2081. /* NOTREACHED */
  2082. }
  2083. QString errMsg;
  2084. if (!doc.setContent(&file, &errMsg)) {
  2085. showWarning(errMsg);
  2086. file.close();
  2087. return;
  2088. /* NOTREACHED */
  2089. }
  2090. file.close();
  2091. list_label_->clear();
  2092. /* getting legend */
  2093. QDomElement elements = doc.documentElement();
  2094. QDomNode rootNode = elements.firstChild();
  2095. QString string;
  2096. while(!rootNode.isNull()) {
  2097. QDomElement element = rootNode.toElement();
  2098. if(!element.isNull()) {
  2099. if (element.tagName() == "legend") {
  2100. loadLegendFromNode(&element);
  2101. }
  2102. }
  2103. rootNode = rootNode.nextSibling();
  2104. }
  2105. }
  2106. void
  2107. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2108. {
  2109. if (!anElement) {
  2110. return;
  2111. /* NOTREACHED */
  2112. }
  2113. QDomNode subNode = anElement->firstChild();
  2114. QDomElement subElement;
  2115. QString string;
  2116. int id = -1;
  2117. bool isMain;
  2118. uint color = 0xff000000;
  2119. while(!subNode.isNull()) {
  2120. subElement = subNode.toElement();
  2121. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2122. string = subElement.attribute("id");
  2123. bool ok = 0;
  2124. id = string.toInt(&ok, 10);
  2125. if (!ok) {
  2126. qDebug() <<
  2127. "loadLegendFromNode: "
  2128. "label id format is corrupted";
  2129. subNode = subNode.nextSibling();
  2130. continue;
  2131. }
  2132. string = subElement.attribute("isMain");
  2133. isMain = string.toInt(&ok, 2);
  2134. if (!ok) {
  2135. qDebug() <<
  2136. "loadLegendFromNode: "
  2137. "label isMain flag format is corrupted";
  2138. subNode = subNode.nextSibling();
  2139. continue;
  2140. }
  2141. string = subElement.attribute("color");
  2142. color = string.toUInt(&ok, 16);
  2143. if (!ok) {
  2144. qDebug() <<
  2145. "loadLegendFromNode: "
  2146. "label color format is corrupted";
  2147. subNode = subNode.nextSibling();
  2148. continue;
  2149. }
  2150. string = subElement.text();
  2151. addLabel(id, isMain, string);
  2152. setLabelColor(id, color);
  2153. }
  2154. subNode = subNode.nextSibling();
  2155. }
  2156. }
  2157. void
  2158. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2159. {
  2160. QDomElement legend = aDoc->createElement(tr("legend"));
  2161. aRoot->appendChild(legend);
  2162. /* storing all labels made by user */
  2163. int labelCount = list_label_->count();
  2164. for (int i = 0; i < labelCount; i++) {
  2165. QDomElement label = aDoc->createElement(tr("label"));
  2166. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2167. label.setAttribute("id", i);
  2168. QString priority;
  2169. if (main_label_ == i)
  2170. priority.append("1");
  2171. else
  2172. priority.append("0");
  2173. label.setAttribute("isMain", priority);
  2174. QString labelText = list_label_->item(i)->text();
  2175. /* removing the number prefix of label */
  2176. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2177. labelText = labelText.mid(3, labelText.size() - 3);
  2178. }
  2179. QDomText labelName = aDoc->createTextNode(labelText);
  2180. label.appendChild(labelName);
  2181. legend.appendChild(label);
  2182. }
  2183. /* in case we have no labels */
  2184. if (0 == labelCount) {
  2185. QDomElement label = aDoc->createElement(tr("label"));
  2186. label.setAttribute(tr("id"), -1);
  2187. legend.appendChild(label);
  2188. }
  2189. }
  2190. void
  2191. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2192. {
  2193. QDomElement objects = aDoc->createElement(tr("objects"));
  2194. aRoot->appendChild(objects);
  2195. /* rects first */
  2196. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2197. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2198. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2199. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2200. QString rectDataString =
  2201. QString("%1;%2;%3;%4;").
  2202. arg(rect.x()).
  2203. arg(rect.y()).
  2204. arg(rect.width()).
  2205. arg(rect.height());
  2206. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2207. rectData.appendChild(rectDataText);
  2208. objects.appendChild(rectData);
  2209. }
  2210. /* polys next */
  2211. for (int i = 0; i < list_polygon_.size(); i++) {
  2212. QDomElement polyData = aDoc->createElement(tr("poly"));
  2213. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2214. QPolygon poly = list_polygon_.at(i)->poly;
  2215. QString polyDataString;
  2216. for (int j = 0; j < poly.count(); j++)
  2217. polyDataString.append(
  2218. QString("%1;%2;").
  2219. arg(poly.point(j).x()).
  2220. arg(poly.point(j).y())
  2221. );
  2222. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2223. polyData.appendChild(polyDataText);
  2224. objects.appendChild(polyData);
  2225. }
  2226. }
  2227. void
  2228. ImageLabeler::generateColors()
  2229. {
  2230. int labelCount = list_label_->count();
  2231. if (!labelCount) {
  2232. showWarning(tr("you have not added any labels yet"));
  2233. return;
  2234. /* NOTREACHED */
  2235. }
  2236. int coeff = (0xff / labelCount) * 3;
  2237. //QList< uint > colors;
  2238. list_label_colors_.clear();
  2239. list_label_colors_.append(0);
  2240. uchar red = 0xff;
  2241. uchar green = 0xff;
  2242. uchar blue = 0xff;
  2243. uchar iterationColor = coeff;
  2244. uint color = 0xffffffff;
  2245. int j = 1;
  2246. for (int i = 1; i < labelCount; i++) {
  2247. if (6 == j) {
  2248. iterationColor += coeff;
  2249. j = 1;
  2250. }
  2251. if (5 == j) {
  2252. red = 0xff - iterationColor;
  2253. green = 0xff - iterationColor;
  2254. blue = 0xff;
  2255. }
  2256. else if (4 == j) {
  2257. red = 0xff - iterationColor;
  2258. green = 0xff;
  2259. blue = 0xff - iterationColor;
  2260. }
  2261. else if (3 == j) {
  2262. red = 0xff - iterationColor;
  2263. green = 0xff;
  2264. blue = 0xff;
  2265. }
  2266. else if (2 == j) {
  2267. red = 0xff;
  2268. green = 0xff - iterationColor;
  2269. blue = 0xff;
  2270. }
  2271. else if (1 == j){
  2272. red = 0xff;
  2273. green = 0xff;
  2274. blue = 0xff - iterationColor;
  2275. }
  2276. j++;
  2277. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2278. int itemNo = list_label_colors_.count();
  2279. QPixmap iconPix = QPixmap(20, 20);
  2280. iconPix.fill(color);
  2281. QIcon icon(iconPix);
  2282. list_label_->item(itemNo)->setIcon(icon);
  2283. list_label_colors_.append(color);
  2284. }
  2285. image_holder_->update();
  2286. }
  2287. void
  2288. ImageLabeler::addBBoxFromData(
  2289. QString *aBBoxData,
  2290. int *ID
  2291. )
  2292. {
  2293. BoundingBox *bbox = new BoundingBox;
  2294. *bbox = BBoxFromData(aBBoxData);
  2295. if (!bbox->rect.isValid() || !ID) {
  2296. return;
  2297. /* NOTREACHED */
  2298. }
  2299. bbox->label_ID_ = *ID;
  2300. list_bounding_box_.append(bbox);
  2301. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2302. }
  2303. BoundingBox
  2304. ImageLabeler::BBoxFromData(
  2305. QString *aBBoxData
  2306. )
  2307. {
  2308. BoundingBox bbox;
  2309. QString buffer;
  2310. bbox.rect.setRect(-1, -1, -1, -1);
  2311. int startPos = 0;
  2312. bool ok = 1;
  2313. for (int i = 0; i < aBBoxData->size(); i++) {
  2314. if (';' != aBBoxData->at(i))
  2315. continue;
  2316. buffer = aBBoxData->mid(startPos, i - startPos);
  2317. int bboxData = buffer.toInt(&ok, 10);
  2318. if (!ok) {
  2319. qDebug() <<
  2320. "BBoxFromData: "
  2321. "bbox format is corrupted";
  2322. break;
  2323. }
  2324. if (-1 == bbox.rect.x()) {
  2325. bbox.rect.setX(bboxData);
  2326. bbox.rect.setWidth(-1);
  2327. }
  2328. else if (-1 == bbox.rect.y()) {
  2329. bbox.rect.setY(bboxData);
  2330. bbox.rect.setHeight(-1);
  2331. }
  2332. else if (-1 == bbox.rect.width()) {
  2333. bbox.rect.setWidth(bboxData);
  2334. }
  2335. else if (-1 == bbox.rect.height()) {
  2336. bbox.rect.setHeight(bboxData);
  2337. }
  2338. startPos = i + 1;
  2339. }
  2340. if (!bbox.rect.isValid()) {
  2341. qDebug() <<
  2342. "BBoxFromData: "
  2343. "bbox format is corrupted";
  2344. bbox.rect.setRect(-1, -1, -1, -1);
  2345. }
  2346. else if (!ok) {
  2347. bbox.rect.setRect(-1, -1, -1, -1);
  2348. }
  2349. return bbox;
  2350. }
  2351. BoundingBox
  2352. ImageLabeler::BBoxFromString(
  2353. QString *aString,
  2354. int *oldID
  2355. )
  2356. {
  2357. BoundingBox bbox;
  2358. bbox.label_ID_ = -1;
  2359. *oldID = -1;
  2360. if (!aString) {
  2361. return bbox;
  2362. /* NOTREACHED */
  2363. }
  2364. if (-1 == aString->indexOf("BBox")) {
  2365. return bbox;
  2366. /* NOTREACHED */
  2367. }
  2368. /* getting bbox id in the list(it cannot be changed) */
  2369. bool ok = 0;
  2370. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2371. if (!ok || bboxID <= -1) {
  2372. qDebug() <<
  2373. "BBoxFromString: bboxID is corrupted";
  2374. return bbox;
  2375. /* NOTREACHED */
  2376. }
  2377. /* getting new label id */
  2378. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2379. if (!ok || labelID <= -1) {
  2380. showWarning(
  2381. tr("new LabelID is wrong, area can not be changed")
  2382. );
  2383. return bbox;
  2384. /* NOTREACHED */
  2385. }
  2386. /* getting new points */
  2387. int pointsPos = aString->indexOf("data:") + 5;
  2388. int pointsLen = aString->size() - pointsPos;
  2389. if (pointsLen <= 0) {
  2390. showWarning(
  2391. tr("new data is wrong, area can not be changed")
  2392. );
  2393. return bbox;
  2394. /* NOTREACHED */
  2395. }
  2396. QString pointsData = aString->mid(pointsPos, pointsLen);
  2397. bbox = BBoxFromData(&pointsData);
  2398. bbox.label_ID_ = labelID;
  2399. *oldID = bboxID;
  2400. return bbox;
  2401. }
  2402. void
  2403. ImageLabeler::addPoly(Polygon *poly)
  2404. {
  2405. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2406. return;
  2407. /* NOTREACHED */
  2408. }
  2409. list_polygon_.append(poly);
  2410. addPolyArea(list_polygon_.count() - 1, *poly);
  2411. }
  2412. void
  2413. ImageLabeler::addBBox(BoundingBox *bbox)
  2414. {
  2415. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2416. return;
  2417. /* NOTREACHED */
  2418. }
  2419. list_bounding_box_.append(bbox);
  2420. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2421. }
  2422. void
  2423. ImageLabeler::addPolyFromData(
  2424. QString *aPolyData,
  2425. int *labelID
  2426. )
  2427. {
  2428. Polygon *poly = new Polygon;
  2429. *poly = polyFromData(aPolyData);
  2430. if (poly->poly.isEmpty() || !labelID) {
  2431. return;
  2432. /* NOTREACHED */
  2433. }
  2434. poly->label_ID_ = *labelID;
  2435. list_polygon_.append(poly);
  2436. addPolyArea(list_polygon_.count() - 1, *poly);
  2437. }
  2438. Polygon
  2439. ImageLabeler::polyFromData(
  2440. QString *aPolyData
  2441. )
  2442. {
  2443. Polygon poly;
  2444. poly.label_ID_ = -1;
  2445. QPoint point;
  2446. QString buffer;
  2447. int startPos = 0;
  2448. bool ok = 1;
  2449. bool evenFlag = 0;
  2450. for (int i = 0; i < aPolyData->size(); i++) {
  2451. if (';' != aPolyData->at(i))
  2452. continue;
  2453. buffer = aPolyData->mid(startPos, i - startPos);
  2454. int polyCoor = buffer.toInt(&ok, 10);
  2455. if (!ok) {
  2456. qDebug() <<
  2457. "polyFromData: "
  2458. "poly format is corrupted";
  2459. break;
  2460. }
  2461. if (!evenFlag) {
  2462. point.setX(polyCoor);
  2463. evenFlag = 1;
  2464. }
  2465. else {
  2466. point.setY(polyCoor);
  2467. poly.poly.append(point);
  2468. evenFlag = 0;
  2469. }
  2470. startPos = i + 1;
  2471. }
  2472. if (evenFlag) {
  2473. qDebug() <<
  2474. "polyFromData: "
  2475. "poly format is corrupted";
  2476. poly.poly.clear();
  2477. }
  2478. else if (!ok) {
  2479. poly.poly.clear();
  2480. }
  2481. return poly;
  2482. }
  2483. Polygon
  2484. ImageLabeler::polyFromString(
  2485. QString *aString,
  2486. int *oldID
  2487. )
  2488. {
  2489. Polygon poly;
  2490. poly.label_ID_ = -1;
  2491. *oldID = -1;
  2492. if (!aString) {
  2493. return poly;
  2494. /* NOTREACHED */
  2495. }
  2496. if (-1 == aString->indexOf("Poly")) {
  2497. return poly;
  2498. /* NOTREACHED */
  2499. }
  2500. /* getting poly id in the list(it cannot be changed) */
  2501. bool ok = 0;
  2502. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2503. if (!ok || polyID <= -1) {
  2504. qDebug() <<
  2505. "polyFromString: poly ID is corrupted";
  2506. return poly;
  2507. /* NOTREACHED */
  2508. }
  2509. /* getting new label id */
  2510. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2511. if (!ok || labelID <= -1) {
  2512. showWarning(
  2513. tr("new LabelID is wrong, area can not be changed")
  2514. );
  2515. return poly;
  2516. /* NOTREACHED */
  2517. }
  2518. /* getting new points */
  2519. int pointsPos = aString->indexOf("points:") + 7;
  2520. int pointsLen = aString->size() - pointsPos;
  2521. if (pointsLen <= 0) {
  2522. showWarning(
  2523. tr("new points data is wrong, area can not be changed")
  2524. );
  2525. return poly;
  2526. /* NOTREACHED */
  2527. }
  2528. QString pointsData = aString->mid(pointsPos, pointsLen);
  2529. poly = polyFromData(&pointsData);
  2530. poly.label_ID_ = labelID;
  2531. *oldID = polyID;
  2532. return poly;
  2533. }
  2534. void
  2535. ImageLabeler::showWarning(
  2536. const QString text
  2537. )
  2538. {
  2539. if (text.isEmpty()) {
  2540. return;
  2541. /* NOTREACHED */
  2542. }
  2543. QMessageBox msgBox;
  2544. msgBox.setText(text);
  2545. msgBox.setIcon(QMessageBox::Warning);
  2546. msgBox.exec();
  2547. }
  2548. bool
  2549. ImageLabeler::askForUnsavedData()
  2550. {
  2551. if ((!list_bounding_box_.isEmpty() ||
  2552. !list_polygon_.isEmpty()) &&
  2553. unsaved_data_)
  2554. {
  2555. QMessageBox msgBox;
  2556. msgBox.setText(tr("There is some unsaved data"));
  2557. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2558. msgBox.setStandardButtons(
  2559. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2560. msgBox.setDefaultButton(QMessageBox::Save);
  2561. msgBox.setIcon(QMessageBox::Question);
  2562. int ret = msgBox.exec();
  2563. if (QMessageBox::Save == ret)
  2564. saveAllInfo();
  2565. else if (QMessageBox::Cancel == ret)
  2566. return true;
  2567. }
  2568. return false;
  2569. }
  2570. void
  2571. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2572. {
  2573. if (aButtonPressed)
  2574. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2575. else {
  2576. image_holder_->setTool(ImageHolder::NoTool);
  2577. image_holder_->clearLast();
  2578. }
  2579. }
  2580. void
  2581. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2582. {
  2583. if (aButtonPressed) {
  2584. image_holder_->setTool(ImageHolder::PolygonTool);
  2585. image_holder_->setFocus();
  2586. }
  2587. else {
  2588. image_holder_->setTool(ImageHolder::NoTool);
  2589. image_holder_->clearLast();
  2590. }
  2591. }
  2592. void
  2593. ImageLabeler::onSelectionStarted()
  2594. {
  2595. button_confirm_selection_->setEnabled(true);
  2596. }
  2597. void
  2598. ImageLabeler::onImageScaled()
  2599. {
  2600. }
  2601. void
  2602. ImageLabeler::confirmSelection()
  2603. {
  2604. if (!list_label_->count()) {
  2605. showWarning(tr("You haven't added any label"));
  2606. return;
  2607. /* NOTREACHED */
  2608. }
  2609. image_holder_->confirmSelection();
  2610. if (label_ID_ < 0)
  2611. label_ID_ = 0;
  2612. ImageHolder::Tool tool = image_holder_->tool();
  2613. switch (tool) {
  2614. case ImageHolder::BoundingBoxTool:
  2615. list_bounding_box_.last()->label_ID_ = label_ID_;
  2616. addBBoxArea(
  2617. list_bounding_box_.count() - 1,
  2618. *(list_bounding_box_.last())
  2619. );
  2620. break;
  2621. case ImageHolder::PolygonTool:
  2622. list_polygon_.last()->label_ID_ = label_ID_;
  2623. addPolyArea(
  2624. list_polygon_.count() - 1,
  2625. *(list_polygon_.last())
  2626. );
  2627. break;
  2628. default:
  2629. break;
  2630. }
  2631. button_confirm_selection_->setEnabled(false);
  2632. unsaved_data_ = 1;
  2633. }
  2634. void
  2635. ImageLabeler::clearAll()
  2636. {
  2637. clearLabelList();
  2638. list_areas_->clear();
  2639. list_bounding_box_.clear();
  2640. list_polygon_.clear();
  2641. list_images_->clear();
  2642. list_images_widget_->clear();
  2643. main_label_ = -1;
  2644. image_holder_->clearAll();
  2645. segmented_image_.clear();
  2646. action_view_normal_->setEnabled(false);
  2647. action_view_segmented_->setEnabled(false);
  2648. }
  2649. void
  2650. ImageLabeler::clearAllTool()
  2651. {
  2652. list_areas_->clear();
  2653. list_bounding_box_.clear();
  2654. list_polygon_.clear();
  2655. main_label_ = -1;
  2656. image_holder_->clearAll();
  2657. }
  2658. void ImageLabeler::clearLabelList()
  2659. {
  2660. list_label_->clear();
  2661. addLabel(0, false, "BACKGROUND");
  2662. list_label_->item(0)->setFlags(
  2663. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2664. Qt::ItemIsEnabled
  2665. );
  2666. }
  2667. void ImageLabeler::clearLabelColorList()
  2668. {
  2669. list_label_colors_.clear();
  2670. list_label_colors_.append(0x0);
  2671. }
  2672. void
  2673. ImageLabeler::enableTools()
  2674. {
  2675. action_save_labels_->setEnabled(true);
  2676. action_save_segmented_->setEnabled(true);
  2677. action_save_legend_->setEnabled(true);
  2678. action_bound_box_tool_->setEnabled(true);
  2679. action_polygon_tool_->setEnabled(true);
  2680. action_add_description_->setEnabled(true);
  2681. action_undo_->setEnabled(true);
  2682. action_redo_->setEnabled(true);
  2683. button_bound_box_tool_->setEnabled(true);
  2684. button_polygon_tool_->setEnabled(true);
  2685. button_tagging_tool_->setEnabled(true);
  2686. button_add_label_->setEnabled(true);
  2687. button_remove_label_->setEnabled(true);
  2688. button_prev_image_->setEnabled(true);
  2689. button_next_image_->setEnabled(true);
  2690. button_clear_selection_tool_->setEnabled(true);
  2691. button_generate_colors_->setEnabled(true);
  2692. button_delete_all_labels_->setEnabled(true);
  2693. }
  2694. void
  2695. ImageLabeler::disableTools()
  2696. {
  2697. action_save_labels_->setEnabled(false);
  2698. action_save_segmented_->setEnabled(false);
  2699. action_save_legend_->setEnabled(false);
  2700. action_bound_box_tool_->setEnabled(false);
  2701. action_polygon_tool_->setEnabled(false);
  2702. button_tagging_tool_->setEnabled(false);
  2703. action_add_description_->setEnabled(false);
  2704. action_undo_->setEnabled(false);
  2705. action_redo_->setEnabled(false);
  2706. button_bound_box_tool_->setEnabled(false);
  2707. button_polygon_tool_->setEnabled(false);
  2708. button_add_label_->setEnabled(false);
  2709. button_remove_label_->setEnabled(false);
  2710. button_prev_image_->setEnabled(false);
  2711. button_next_image_->setEnabled(false);
  2712. button_clear_selection_tool_->setEnabled(false);
  2713. button_generate_colors_->setEnabled(false);
  2714. button_delete_all_labels_->setEnabled(false);
  2715. }
  2716. void
  2717. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2718. {
  2719. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2720. QModelIndex index = list_areas_->indexAt(aPos);
  2721. if (-1 == index.row()) {
  2722. return;
  2723. /* NOTREACHED */
  2724. }
  2725. list_areas_->item(index.row())->setSelected(true);
  2726. popup_area_list_->exec(globalPos);
  2727. }
  2728. void
  2729. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2730. {
  2731. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2732. QModelIndex index = list_label_->indexAt(aPos);
  2733. if (-1 == index.row() || !index.row()) {
  2734. return;
  2735. /* NOTREACHED */
  2736. }
  2737. list_label_->item(index.row())->setSelected(true);
  2738. popup_label_list_->exec(globalPos);
  2739. }
  2740. void
  2741. ImageLabeler::setDataFromForm(QString aData)
  2742. {
  2743. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  2744. image_description_ = aData;
  2745. setWindowTitle(image_description_);
  2746. }
  2747. else if (TaggingPurpose == line_edit_form_.purpose()) {
  2748. tags_ = aData;
  2749. }
  2750. }
  2751. void
  2752. ImageLabeler::setPureData()
  2753. {
  2754. /* initializing array */
  2755. if (pure_data_) {
  2756. delete[] *pure_data_;
  2757. delete pure_data_;
  2758. }
  2759. QSize imageSize = image_->size();
  2760. pure_data_ = new int *[imageSize.height()];
  2761. if (!pure_data_) {
  2762. return;
  2763. /* NOTREACHED */
  2764. }
  2765. for (int i = 0; i < imageSize.height(); i++) {
  2766. pure_data_[i] = new int[imageSize.width()];
  2767. if (!pure_data_[i]) {
  2768. return;
  2769. /* NOTREACHED */
  2770. }
  2771. }
  2772. int bboxCnt = list_bounding_box_.count();
  2773. int polyCnt = list_polygon_.count();
  2774. for (int i = 0; i < imageSize.height(); i++)
  2775. for (int j = 0; j < imageSize.width(); j++) {
  2776. pure_data_[i][j] = 0;
  2777. /* bboxes first */
  2778. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2779. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2780. if (bbox->rect.contains(j, i)) {
  2781. pure_data_[i][j] = bbox->label_ID_;
  2782. }
  2783. }
  2784. /* polys next */
  2785. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2786. Polygon *poly = list_polygon_.at(cnt);
  2787. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2788. pure_data_[i][j] = poly->label_ID_;
  2789. }
  2790. }
  2791. }
  2792. }
  2793. void
  2794. ImageLabeler::setLabelColor()
  2795. {
  2796. if (list_label_colors_.count() < list_label_->count()) {
  2797. generateColors();
  2798. }
  2799. QListWidgetItem *current = list_label_->currentItem();
  2800. int labelID = list_label_->row(current);
  2801. QColor defaultColor;
  2802. defaultColor.setRgb(list_label_colors_.at(labelID));
  2803. QColor newColor = QColorDialog::getColor(
  2804. defaultColor,
  2805. list_label_
  2806. );
  2807. if (!newColor.isValid()) {
  2808. return;
  2809. /* NOTREACHED */
  2810. }
  2811. list_label_colors_.takeAt(labelID);
  2812. list_label_colors_.insert(labelID, newColor.rgb());
  2813. QPixmap iconPix = QPixmap(20, 20);
  2814. iconPix.fill(newColor);
  2815. QIcon icon(iconPix);
  2816. current->setIcon(icon);
  2817. image_holder_->update();
  2818. }
  2819. void
  2820. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2821. {
  2822. if (anID < 0) {
  2823. return;
  2824. /* NOTREACHED */
  2825. }
  2826. if (list_label_colors_.count() < list_label_->count()) {
  2827. generateColors();
  2828. }
  2829. QListWidgetItem *item = list_label_->item(anID);
  2830. list_label_colors_.takeAt(anID);
  2831. list_label_colors_.insert(anID, aColor.rgb());
  2832. QPixmap iconPix = QPixmap(20, 20);
  2833. iconPix.fill(aColor);
  2834. QIcon icon(iconPix);
  2835. item->setIcon(icon);
  2836. image_holder_->update();
  2837. }
  2838. void
  2839. ImageLabeler::onOptionsSet()
  2840. {
  2841. auto_color_generation_ = options_form_.autoColorGeneration();
  2842. }
  2843. void
  2844. ImageLabeler::viewNormal()
  2845. {
  2846. if (current_image_.isEmpty()) {
  2847. return;
  2848. /* NOTREACHED */
  2849. }
  2850. image_->load(current_image_);
  2851. image_holder_->setPixmap(*image_);
  2852. action_view_segmented_->setEnabled(true);
  2853. action_view_normal_->setEnabled(false);
  2854. }
  2855. void
  2856. ImageLabeler::viewSegmented()
  2857. {
  2858. if (segmented_image_.isEmpty()) {
  2859. return;
  2860. /* NOTREACHED */
  2861. }
  2862. image_->load(segmented_image_);
  2863. image_holder_->setPixmap(*image_);
  2864. action_view_segmented_->setEnabled(false);
  2865. action_view_normal_->setEnabled(true);
  2866. }
  2867. void
  2868. ImageLabeler::interruptSearch()
  2869. {
  2870. interrupt_search_ = 1;
  2871. }
  2872. void
  2873. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2874. {
  2875. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2876. list_images_->isEmpty())
  2877. {
  2878. return;
  2879. /* NOTREACHED */
  2880. }
  2881. clearAllTool();
  2882. clearLabelList();
  2883. clearLabelColorList();
  2884. image_ID_ = list_images_widget_->row(anItem);
  2885. selectImage(image_ID_);
  2886. // if (list_images_->at(image_ID_).labeled_ &&
  2887. // !list_images_->at(image_ID_).pas_)
  2888. // {
  2889. // list_label_->clear();
  2890. // QString labeled =
  2891. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2892. // labeled.append(".dat");
  2893. // loadInfo(labeled);
  2894. // }
  2895. // else if (list_images_->at(image_ID_).labeled_ &&
  2896. // list_images_->at(image_ID_).pas_)
  2897. // {
  2898. // /* TODO: do the pascal file selecting */
  2899. // showWarning("this function doesn't work at the moment, sorry.");
  2900. // }
  2901. // else {
  2902. // current_image_ = list_images_->at(image_ID_).image_;
  2903. // image_->load(current_image_);
  2904. // image_holder_->setPixmap(*image_);
  2905. // image_holder_->resize(image_->size());
  2906. // }
  2907. }
  2908. bool
  2909. ImageLabeler::selectImage(int anImageID)
  2910. {
  2911. if (anImageID < 0 || list_images_->isEmpty() ||
  2912. list_images_->count() <= anImageID)
  2913. {
  2914. return false;
  2915. /* NOTREACHED */
  2916. }
  2917. if (list_images_->at(anImageID).labeled_ &&
  2918. !list_images_->at(anImageID).pas_)
  2919. {
  2920. list_label_->clear();
  2921. QString labeled =
  2922. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2923. labeled.append(".dat");
  2924. loadInfo(labeled);
  2925. }
  2926. else if (list_images_->at(anImageID).labeled_ &&
  2927. list_images_->at(anImageID).pas_)
  2928. {
  2929. /* TODO: do the pascal file selecting */
  2930. showWarning("this function doesn't work at the moment, sorry.");
  2931. }
  2932. else {
  2933. current_image_ = list_images_->at(anImageID).image_;
  2934. image_->load(current_image_);
  2935. image_holder_->setPixmap(*image_);
  2936. image_holder_->resize(image_->size());
  2937. }
  2938. return true;
  2939. }
  2940. void
  2941. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2942. {
  2943. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2944. QModelIndex index = list_images_widget_->indexAt(aPos);
  2945. if (-1 == index.row()) {
  2946. return;
  2947. /* NOTREACHED */
  2948. }
  2949. list_images_widget_->item(index.row())->setSelected(true);
  2950. popup_images_list_->exec(globalPos);
  2951. }
  2952. void
  2953. ImageLabeler::removeImage()
  2954. {
  2955. int num = list_images_widget_->currentRow();
  2956. list_images_widget_->takeItem(num);
  2957. list_images_->takeAt(num);
  2958. for (int i = num ; i < list_images_widget_->count(); i++) {
  2959. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2960. newStr.prepend(QString("%1: ").arg(i));
  2961. list_images_widget_->item(i)->setText(newStr);
  2962. }
  2963. }
  2964. void
  2965. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2966. {
  2967. QWidget::resizeEvent(anEvent);
  2968. }
  2969. void
  2970. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2971. {
  2972. QWidget::mousePressEvent(anEvent);
  2973. }
  2974. void
  2975. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2976. {
  2977. keyboard_modifier_ = anEvent->modifiers();
  2978. if (Qt::Key_Left == anEvent->key() &&
  2979. Qt::ControlModifier == keyboard_modifier_) {
  2980. prevImage();
  2981. }
  2982. if (Qt::Key_Right == anEvent->key() &&
  2983. Qt::ControlModifier == keyboard_modifier_) {
  2984. nextImage();
  2985. }
  2986. if ((Qt::Key_Enter == anEvent->key() ||
  2987. Qt::Key_Return == anEvent->key()) &&
  2988. // Qt::NoModifier == anEvent->modifiers() &&
  2989. ImageHolder::NewSelection == image_holder_->state()) {
  2990. confirmSelection();
  2991. }
  2992. if ((Qt::Key_Enter == anEvent->key() ||
  2993. Qt::Key_Return == anEvent->key()) &&
  2994. // Qt::NoModifier == anEvent->modifiers() &&
  2995. -1 != image_holder_->focusedSelection()) {
  2996. image_holder_->clearFocusOnArea();
  2997. image_holder_->clearEdition();
  2998. image_holder_->update();
  2999. }
  3000. if (Qt::Key_Escape == anEvent->key()) {
  3001. image_holder_->clearLast();
  3002. image_holder_->clearFocusOnArea();
  3003. image_holder_->clearEdition();
  3004. image_holder_->update();
  3005. }
  3006. if (Qt::Key_Z == anEvent->key() &&
  3007. Qt::ControlModifier == anEvent->modifiers()) {
  3008. image_holder_->undo();
  3009. }
  3010. if (Qt::Key_Y == anEvent->key() &&
  3011. Qt::ControlModifier == anEvent->modifiers()) {
  3012. image_holder_->redo();
  3013. }
  3014. QWidget::keyPressEvent(anEvent);
  3015. }
  3016. void
  3017. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  3018. {
  3019. keyboard_modifier_ = anEvent->modifiers();
  3020. }
  3021. void
  3022. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  3023. {
  3024. /* zoomin */
  3025. if (0 < anEvent->delta() &&
  3026. Qt::ControlModifier == keyboard_modifier_) {
  3027. image_holder_->scaleImage(ZoomIn, 1.1);
  3028. }
  3029. /* zoomout */
  3030. else if (anEvent->delta() < 0 &&
  3031. Qt::ControlModifier == keyboard_modifier_) {
  3032. image_holder_->scaleImage(ZoomOut, 1.1);
  3033. }
  3034. }
  3035. void
  3036. ImageLabeler::closeEvent(QCloseEvent *anEvent)
  3037. {
  3038. Q_UNUSED(anEvent)
  3039. writeSettings();
  3040. }
  3041. /*
  3042. *
  3043. */