1 
2 
3 #include "toonzqt/validatedchoicedialog.h"
4 
5 // Qt includes
6 #include <QLabel>
7 #include <QPushButton>
8 #include <QButtonGroup>
9 
10 //************************************************************************
11 //    ValidatedChoiceDialog  implementation
12 //************************************************************************
13 
ValidatedChoiceDialog(QWidget * parent,Options opts)14 DVGui::ValidatedChoiceDialog::ValidatedChoiceDialog(QWidget *parent,
15                                                     Options opts)
16     : Dialog(parent, true, false) {
17   setModal(true);
18 
19   m_buttonGroup = new QButtonGroup(this);
20   m_buttonGroup->setExclusive(true);
21 
22   bool ret = true;
23 
24   QPushButton *okBtn = new QPushButton(QString(tr("Apply")), this);
25   ret                = ret && connect(okBtn, SIGNAL(clicked()), SLOT(accept()));
26   addButtonBarWidget(okBtn);
27 
28   QPushButton *okToAllBtn = new QPushButton(QString(tr("Apply to All")), this);
29   ret = ret && connect(okToAllBtn, SIGNAL(clicked()), SLOT(onApplyToAll()));
30   addButtonBarWidget(okToAllBtn);
31 
32   QPushButton *cancelBtn = new QPushButton(QString(tr("Cancel")), this);
33   ret = ret && connect(cancelBtn, SIGNAL(clicked()), SLOT(reject()));
34   addButtonBarWidget(cancelBtn);
35 
36   assert(ret);
37 
38   reset();
39 
40   // Start layout
41   beginVLayout();
42 
43   m_label = new QLabel(this);
44   addWidget(m_label);
45 
46   // enbVLayout() must be invoked by derived classes
47 }
48 
49 //------------------------------------------------------------------------
50 
reset()51 void DVGui::ValidatedChoiceDialog::reset() {
52   m_appliedToAllRes = NO_REQUIRED_RESOLUTION;
53   m_appliedToAll    = false;
54 }
55 
56 //------------------------------------------------------------------------
57 
58 QString validate(void *obj);
59 
execute(void * obj)60 int DVGui::ValidatedChoiceDialog::execute(void *obj) {
61   struct Resol {
62     int m_res;
63     bool m_all;
64     Resol(int res, bool all) : m_res(res), m_all(all) {}
65   };
66 
67   Resol curResolution(NO_REQUIRED_RESOLUTION, false),
68       newResolution(m_appliedToAll ? m_appliedToAllRes : NO_REQUIRED_RESOLUTION,
69                     m_appliedToAll);
70 
71   bool initialize = true;
72 
73   // Loop until a resolution gets accepted
74   do {
75     QString err =
76         acceptResolution(obj, curResolution.m_res, curResolution.m_all);
77 
78     if (err.isEmpty()) break;
79 
80     if (newResolution.m_res == NO_REQUIRED_RESOLUTION) {
81       // No new resolution selected - prompt for user interaction
82       m_label->setText(err);
83       m_applyToAll = false;
84 
85       if (initialize) initializeUserInteraction(obj), initialize = false;
86 
87       if (exec() == QDialog::Rejected) {
88         curResolution.m_res = CANCEL;
89         break;
90       }
91 
92       newResolution = Resol(m_buttonGroup->checkedId(), m_applyToAll);
93       assert(newResolution.m_res >= DEFAULT_RESOLUTIONS_COUNT);
94     }
95 
96     // Substitute resolution and retry
97     curResolution = newResolution,
98     newResolution = Resol(NO_REQUIRED_RESOLUTION, false);
99 
100   } while (true);
101 
102   return curResolution.m_res;
103 }
104 
105 //------------------------------------------------------------------------
106 
onApplyToAll()107 void DVGui::ValidatedChoiceDialog::onApplyToAll() {
108   m_appliedToAllRes = m_buttonGroup->checkedId();
109   m_applyToAll = m_appliedToAll = true;
110 
111   assert(m_appliedToAllRes >= DEFAULT_RESOLUTIONS_COUNT);
112 
113   accept();
114 }
115