1 
2 
3 #include "magpiefileimportpopup.h"
4 #include "tapp.h"
5 #include "tfilepath_io.h"
6 #include "tsystem.h"
7 #include "iocommand.h"
8 #include "flipbook.h"
9 #include "toonz/tscenehandle.h"
10 #include "toonz/toonzscene.h"
11 #include "toonzqt/filefield.h"
12 #include "toonzqt/intfield.h"
13 #include "toonzqt/gutil.h"
14 
15 #include <QLabel>
16 #include <QPushButton>
17 #include <QTextStream>
18 #include <QMainWindow>
19 
20 //=============================================================================
21 // MagpieInfo
22 //-----------------------------------------------------------------------------
23 
MagpieInfo(TFilePath path)24 MagpieInfo::MagpieInfo(TFilePath path)
25     : m_fileName(QString::fromStdWString(path.getWideName())) {
26   QFile file(QString::fromStdWString(path.getWideString()));
27   if (!file.open(QFile::ReadOnly)) return;
28   QTextStream textStream(&file);
29   QString line;
30   do {
31     line = textStream.readLine();
32     // E' la prima riga
33     if (line == QString("Toonz")) continue;
34     if (!line.contains(L'|')) {
35       if (!line.isEmpty()) m_actsIdentifier.append(line);
36       continue;
37     }
38     QStringList list = line.split(QString("|"));
39     assert(list.size() == 3);
40     m_actorActs.append(list.at(1));
41     m_comments.append(list.at(2));
42   } while (!line.isNull());
43 }
44 
45 //=============================================================================
46 // MagpieFileImportPopup
47 //-----------------------------------------------------------------------------
48 
MagpieFileImportPopup()49 MagpieFileImportPopup::MagpieFileImportPopup()
50     : Dialog(TApp::instance()->getMainWindow(), true, true, "MagPieFileImport")
51     , m_levelField(0)
52     , m_fromField(0)
53     , m_toField(0)
54     , m_flipbook(0)
55     , m_levelPath() {
56   setWindowTitle(tr("Import Toonz Lip Sync File"));
57 
58   beginVLayout();
59 
60   setLabelWidth(60);
61 
62   addSeparator(tr("Frame Range"));
63 
64   QWidget *fromToWidget = new QWidget(this);
65   fromToWidget->setFixedHeight(DVGui::WidgetHeight);
66   fromToWidget->setFixedSize(210, DVGui::WidgetHeight);
67   QHBoxLayout *fromToLayout = new QHBoxLayout(fromToWidget);
68   fromToLayout->setMargin(0);
69   fromToLayout->setSpacing(0);
70   m_fromField = new DVGui::IntLineEdit(fromToWidget, 1, 1, 1);
71   fromToLayout->addWidget(m_fromField, 0, Qt::AlignLeft);
72   m_toField       = new DVGui::IntLineEdit(fromToWidget, 1, 1, 1);
73   QLabel *toLabel = new QLabel(tr("To: "));
74   toLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
75   toLabel->setFixedSize(20, DVGui::WidgetHeight);
76   fromToLayout->addWidget(toLabel, 0, Qt::AlignRight);
77   fromToLayout->addWidget(m_toField, 0, Qt::AlignLeft);
78   fromToWidget->setLayout(fromToLayout);
79   addWidget(tr("From:"), fromToWidget);
80 
81   addSeparator(tr("Animation Level"));
82 
83   m_levelField = new DVGui::FileField(this);
84   m_levelField->setFileMode(QFileDialog::AnyFile);
85   m_levelField->setFixedWidth(200);
86   bool ret =
87       connect(m_levelField, SIGNAL(pathChanged()), SLOT(onLevelPathChanged()));
88 
89   addWidget(tr("Level:"), m_levelField);
90 
91   QLabel *frameLabel = new QLabel(" Frame", this);
92   frameLabel->setFixedHeight(DVGui::WidgetHeight);
93   frameLabel->setAlignment(Qt::AlignVCenter);
94   addWidget(tr("Phoneme"), frameLabel);
95   int i;
96   for (i = 0; i < 9; i++) {
97     DVGui::IntLineEdit *field = new DVGui::IntLineEdit(this, 1, 1);
98     field->setFixedSize(54, DVGui::WidgetHeight);
99     QLabel *label = new QLabel("", this);
100     label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
101     label->setFixedSize(getLabelWidth(), DVGui::WidgetHeight);
102     m_actFields.append(QPair<QLabel *, DVGui::IntLineEdit *>(label, field));
103     addWidgets(label, field);
104   }
105 
106   endVLayout();
107 
108   QFrame *frame = new QFrame(this);
109   frame->setFrameStyle(QFrame::StyledPanel);
110   frame->setObjectName("LipSynkViewer");
111   frame->setStyleSheet(
112       "#LipSynkViewer { border: 1px solid rgb(150,150,150); }");
113   QVBoxLayout *frameLayout = new QVBoxLayout(frame);
114   frameLayout->setMargin(0);
115   frameLayout->setSpacing(0);
116   std::vector<int> buttonMask = {FlipConsole::eRate,
117                                  FlipConsole::eSound,
118                                  FlipConsole::eSaveImg,
119                                  FlipConsole::eHisto,
120                                  FlipConsole::eCompare,
121                                  FlipConsole::eCustomize,
122                                  FlipConsole::eSave,
123                                  FlipConsole::eBegin,
124                                  FlipConsole::eEnd,
125                                  FlipConsole::eFirst,
126                                  FlipConsole::eNext,
127                                  FlipConsole::ePause,
128                                  FlipConsole::ePlay,
129                                  FlipConsole::ePrev,
130                                  FlipConsole::eRate,
131                                  FlipConsole::eWhiteBg,
132                                  FlipConsole::eCheckBg,
133                                  FlipConsole::eBlackBg,
134                                  FlipConsole::eNext,
135                                  FlipConsole::eLast,
136                                  FlipConsole::eLoop,
137                                  FlipConsole::eGRed,
138                                  FlipConsole::eGGreen,
139                                  FlipConsole::eGBlue,
140                                  FlipConsole::eRed,
141                                  FlipConsole::eGreen,
142                                  FlipConsole::eBlue,
143                                  FlipConsole::eMatte,
144                                  FlipConsole::eDefineSubCamera,
145                                  FlipConsole::eDefineLoadBox,
146                                  FlipConsole::eUseLoadBox,
147                                  FlipConsole::eFilledRaster,
148                                  FlipConsole::eLocator,
149                                  FlipConsole::eZoomIn,
150                                  FlipConsole::eZoomOut,
151                                  FlipConsole::eFlipHorizontal,
152                                  FlipConsole::eFlipVertical,
153                                  FlipConsole::eResetView};
154   m_flipbook = new FlipBook(this, tr("Import Toonz Lip Sync File"), buttonMask);
155   m_flipbook->setFixedHeight(250);
156   frameLayout->addWidget(m_flipbook);
157   frame->setLayout(frameLayout);
158   addWidget(frame);
159 
160   QPushButton *okBtn = new QPushButton(tr("Import"), this);
161   okBtn->setDefault(true);
162   QPushButton *cancelBtn = new QPushButton(tr("Cancel"), this);
163   ret = ret && connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkPressed()));
164   ret = ret && connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
165   assert(ret);
166 
167   addButtonBarWidget(okBtn, cancelBtn);
168 }
169 
170 //-----------------------------------------------------------------------------
171 
setFilePath(TFilePath path)172 void MagpieFileImportPopup::setFilePath(TFilePath path) {
173   m_info = new MagpieInfo(path);
174 }
175 
176 //-----------------------------------------------------------------------------
177 
showEvent(QShowEvent *)178 void MagpieFileImportPopup::showEvent(QShowEvent *) {
179   if (m_info == 0) return;
180 
181   int frameCount = m_info->getFrameCount();
182   m_fromField->setRange(1, frameCount);
183   m_fromField->setValue(1);
184   m_toField->setRange(1, frameCount);
185   m_toField->setValue(frameCount);
186 
187   int i;
188   QList<QString> actsIdentifier = m_info->getActsIdentifier();
189   for (i = 0; i < m_actFields.size(); i++) {
190     DVGui::IntLineEdit *field = m_actFields.at(i).second;
191     QLabel *label             = m_actFields.at(i).first;
192     if (i >= actsIdentifier.size()) {
193       field->hide();
194       label->hide();
195       continue;
196     }
197     QString act = actsIdentifier.at(i);
198     field->setProperty("act", QVariant(act));
199     field->show();
200     label->setText(act + ":");
201     label->show();
202   }
203   QString oldLevelPath = m_levelField->getPath();
204   TFilePath oldFilePath(oldLevelPath.toStdWString());
205   TFilePath perntDir = oldFilePath.getParentDir();
206   m_levelField->setPath(QString::fromStdWString(perntDir.getWideString()));
207 }
208 
209 //-----------------------------------------------------------------------------
210 
hideEvent(QHideEvent *)211 void MagpieFileImportPopup::hideEvent(QHideEvent *) {
212   // Devo svuotare il flibook
213   emit closeButtonPressed();
214 }
215 
216 //-----------------------------------------------------------------------------
217 
onLevelPathChanged()218 void MagpieFileImportPopup::onLevelPathChanged() {
219   ToonzScene *scene = TApp::instance()->getCurrentScene()->getScene();
220   TFilePath levelPath(m_levelField->getPath().toStdWString());
221   levelPath = scene->decodeFilePath(levelPath);
222   if (levelPath.isEmpty() || levelPath.getUndottedType().empty() ||
223       !TSystem::doesExistFileOrLevel(levelPath)) {
224     DVGui::error(tr("The file path is missing."));
225     return;
226   }
227   m_levelPath = levelPath;
228 
229   std::string format = m_levelPath.getType();
230   if (format == "tzp" || format == "tzu") {
231     std::wstring name                       = m_levelPath.getWideName();
232     IoCmd::ConvertingPopup *convertingPopup = new IoCmd::ConvertingPopup(
233         TApp::instance()->getMainWindow(),
234         QString::fromStdWString(name) +
235             QString::fromStdString(m_levelPath.getDottedType()));
236     convertingPopup->show();
237 
238     bool ok = scene->convertLevelIfNeeded(m_levelPath);
239     convertingPopup->hide();
240     if (!ok) return;
241   }
242   m_flipbook->setLevel(m_levelPath);
243 }
244 
245 //-----------------------------------------------------------------------------
246 
onOkPressed()247 void MagpieFileImportPopup::onOkPressed() {
248   if (m_levelPath.isEmpty() || m_levelPath.getUndottedType().empty() ||
249       !TSystem::doesExistFileOrLevel(m_levelPath)) {
250     DVGui::error(tr("The file path is missing."));
251     return;
252   }
253 
254   QList<QString> actorActs = m_info->getActorActs();
255   QList<QString> comments  = m_info->getComments();
256   int from                 = m_fromField->getValue() - 1;
257   int to                   = m_toField->getValue() - 1;
258   assert(to < m_info->getFrameCount());
259 
260   QList<TFrameId> frameList;
261   QList<QString> commentList;
262   int commentCount   = comments.size();
263   int actorActsCount = actorActs.size();
264   for (int i = from; i <= to; i++) {
265     if (commentCount <= i || actorActsCount <= i) continue;
266     commentList.append(comments.at(i));
267     QString actorAct = actorActs.at(i);
268     if (actorAct == QString("<none>")) {
269       frameList.push_back(TFrameId());
270       continue;
271     }
272     for (int j = 0; j < m_actFields.size(); j++) {
273       DVGui::IntLineEdit *field = m_actFields.at(j).second;
274       QString act               = field->property("act").toString();
275       if (actorAct != act) continue;
276       frameList.push_back(TFrameId(field->getValue()));
277       break;
278     }
279   }
280 
281   bool ret = IoCmd::importLipSync(m_levelPath, frameList, commentList,
282                                   m_info->getFileName());
283   if (!ret) return;
284   accept();
285 }
286