CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / linguisticamainwindow_commandline.cpp
blobbb1c5f45366e23551d717bca2963374a3cc0ba01
1 // Updating the main window’s additional-info area
2 // Copyright © 2009 The University of Chicago
3 #include "linguisticamainwindow.h"
5 #include <algorithm>
6 #include <Q3TextStream>
7 #include <QString>
8 #include <QFont>
9 #include "FSA.h"
10 #include "MiniLexicon.h"
11 #include "Lexicon.h"
12 #include "GraphicView.h"
13 #include "LPreferences.h"
14 #include "Compound.h"
15 #include "Template.h"
16 #include "Suffix.h"
17 #include "Prefix.h"
18 #include "Stem.h"
19 #include "POS.h"
20 #include "SignatureCollection.h"
21 #include "TemplateCollection.h"
22 #include "SuffixCollection.h"
23 #include "PrefixCollection.h"
25 void LinguisticaMainWindow::setCommandLineStyle( QString style )
27 QFont font = m_eastFont;
28 QString color = GetPreference( style + "_Color" );
29 if( color.length() == 0 ) color = "black";
31 m_commandLine->setColor( QColor( color ) );
33 m_commandLine->setFamily( font.family() );
34 m_commandLine->setPointSize( font.pointSize() );
36 font = GetFontPreference( style );
37 m_commandLine->setBold( font.bold() );
38 m_commandLine->setItalic( font.italic() );
39 m_commandLine->setUnderline( font.underline() );
43 void LinguisticaMainWindow::updateCommandLineSlot()
45 updateCommandLineSlot( NULL );
48 void LinguisticaMainWindow::updateCommandLineSlot(Q3ListViewItem* item)
50 if (item != 0) {
51 // set m_commandParse and m_commandMini for the selected
52 // collection view item.
54 switch (m_docType) {
55 case WORDS:
56 case ANALYZED_WORDS:
57 case ALL_ANALYZED_WORDS:
58 if (m_lexicon->GetActiveMiniIndex() != -1) {
59 CWordListViewItem* word =
60 static_cast<CWordListViewItem*>(item);
62 if (!word->isFullLex()) {
63 m_commandParse = word->GetWord();
64 item = 0;
65 } else {
66 m_commandParse = 0;
68 m_commandMini = -1;
70 break;
71 case SUFFIXES:
73 CSuffixListViewItem* suffix_item =
74 static_cast<CSuffixListViewItem*>(item);
75 if (CSuffix* suffix = suffix_item->GetAffix()) {
76 m_commandParse = suffix;
77 m_commandMini = suffix->GetMyMini()->GetIndex();
79 break;
81 case PREFIXES:
83 CPrefixListViewItem* prefix_item =
84 static_cast<CPrefixListViewItem*>(item);
85 if (CPrefix* prefix = prefix_item->GetAffix()) {
86 m_commandParse = prefix;
87 m_commandMini = prefix->GetMyMini()->GetIndex();
89 break;
91 case TRIE:
92 break;
93 case STEMS_SUFFIXED:
95 CStemListViewItem* stem_item =
96 static_cast<CStemListViewItem*>(item);
97 if (CStem* stem = stem_item->GetStem()) {
98 m_commandParse = stem;
99 m_commandMini = stem->GetMyMini()->GetIndex();
101 break;
103 case SIGNATURES:
104 case PREFIX_SIGNATURES:
105 case SUFFIX_SIGNATURES:
107 CSignatureListViewItem* sig_item =
108 static_cast<CSignatureListViewItem*>(item);
109 if (CSignature* sig = sig_item->GetSignature()) {
110 m_commandParse = sig;
111 m_commandMini = sig->GetMyMini()->GetIndex();
113 break;
115 case COMPOUNDS:
116 m_commandParse = static_cast<CCompoundListViewItem*>(item)
117 ->GetCompound();
118 break;
119 case INITIALSTRINGEDITTEMPLATES:
120 m_CommonTemplate =
121 static_cast<CTemplateListViewItem*>(item)
122 ->GetTemplate();
123 break;
124 case WORKINGSTRINGEDITTEMPLATES:
125 m_CommonTemplate =
126 static_cast<CTemplateListViewItem*>(item)
127 ->GetTemplate();
128 break;
129 case POS:
131 GUIPOSListViewItem* pos_item =
132 static_cast<GUIPOSListViewItem*>(item);
133 if (LxPoS* pos = pos_item->GetPoS())
134 m_commandParse = pos;
135 break;
137 case FSA_DOC:
138 case NO_DOC:
139 default:
140 m_commandMini = -1;
141 break;
145 // Clear the command line
146 m_commandLine->clear();
147 m_commandLine->setFont(m_eastFont);
149 if (m_commandParse == 0 && m_CommonState == 0 && item == 0)
150 return;
152 QString text;
153 QTextStream ts(&text, QIODevice::WriteOnly);
155 CMiniLexicon* mini = m_lexicon->GetMiniLexicon(m_commandMini);
156 QMap<QString, QString>* const filter = m_lexicon->GetOutFilter();
158 switch (m_docType) {
159 case WORDS:
160 case ANALYZED_WORDS:
161 case ALL_ANALYZED_WORDS:
162 if (item != 0) {
163 // selected item in collection view
164 CWordListViewItem* word_item =
165 static_cast<CWordListViewItem*>(item);
166 CStem* word = word_item->GetWord();
168 setCommandLineStyle("Stem");
169 m_commandLine->insert(item->text(1));
171 setCommandLineStyle("Affix");
172 const int nColumns = m_lexicon->GetMiniCount() + 2;
173 for (int i = 2; i < nColumns; ++i)
174 if (!item->text(i).isEmpty()) {
175 m_commandLine->insert(QString(" %1")
176 .arg(item->text(i)));
179 m_commandLine->insert(QString("\n%1").arg(
180 word->GetProbabilityInformation()));
182 // Graphic Display
183 updateSmallGraphicDisplaySlotForPhonogy(word);
184 } else {
185 // just updating the “command line” text
186 // XXX. Reconcile with other case.
188 CStem* word = static_cast<CStem*>(m_commandParse);
190 if (CPrefix* pPrefix = word->GetPrefixPtr()) {
191 setCommandLineStyle("Affix");
192 m_commandLine->insert(QString("%1 ")
193 .arg(pPrefix->Display(filter)));
196 if (CStem* pStem = word->GetStemPtr()) {
197 setCommandLineStyle("Stem");
198 m_commandLine->insert(QString("%1 ")
199 .arg(pStem->Display(filter)));
200 } else {
201 m_commandLine->insert(word->Display(filter));
204 if (CSuffix* pSuffix = word->GetSuffixPtr()) {
205 setCommandLineStyle("Affix");
206 m_commandLine->insert(pSuffix->Display(filter));
209 m_commandLine->insert(QString("\n%1")
210 .arg(word->GetProbabilityInformation()));
212 // Graphic display
213 updateSmallGraphicDisplaySlotForPhonogy(word);
215 break;
216 case SUFFIXES:
218 if (m_commandParse == 0)
219 break;
221 CSuffix* suffix = static_cast<CSuffix*>(m_commandParse);
222 QString selected = suffix->Display(filter);
223 if (selected.isEmpty()) {
224 setCommandLineStyle("Error");
225 m_commandLine->insert(QString("Not found."));
226 return;
229 // Suffix name
230 setCommandLineStyle("Affix");
231 m_commandLine->insert(selected);
233 // XXX. deletees?
235 // Corresponding signatures
236 setCommandLineStyle("Main");
237 m_commandLine->insert("\n\nSignatures:\n");
239 Q_ASSERT(mini != 0);
240 CSignatureCollection* Signatures = mini->GetSignatures();
241 setCommandLineStyle("Signature");
242 for (int i = 0; i < Signatures->GetCount(); ++i) {
243 const CSignature& sig = *Signatures->GetAt(i);
245 if (sig.Contains(selected))
246 m_commandLine->insert(QString("\t%1\n")
247 .arg(sig.Display('.', filter)));
250 // Corresponding stems
251 setCommandLineStyle("Main");
252 m_commandLine->insert("\n\nStems:\n\t");
253 setCommandLineStyle("Stem");
254 int nColumns = 3;
255 int tablength = 0;
256 if (CSuffix* pSuffix = *mini->GetSuffixes() ^= selected) {
257 CParse* pParse = pSuffix->GetStemString();
259 for (int i = 1; i <= pParse->Size(); ) {
260 for(int j = 1; j <= nColumns; ++j) {
261 QString text = pParse->GetPiece(i)
262 .Display(filter);
263 ++i;
265 m_commandLine->insert(QString("%1\t")
266 .arg(text));
268 // Make tab length depend on the longest
269 // word and font size
270 tablength = std::max(tablength,
271 text.size() *
272 m_commandLine->pointSize());
273 m_commandLine->insert("\n\t");
275 m_commandLine->setTabStopWidth(tablength);
278 break;
280 case PREFIXES:
282 if (m_commandParse == 0)
283 break;
285 CPrefix* prefix = static_cast<CPrefix*>(m_commandParse);
286 QString selected = prefix->Display(filter);
287 if (selected.isEmpty()) {
288 setCommandLineStyle("Error");
289 m_commandLine->insert(QString("Not found."));
290 return;
293 // Prefix name
294 setCommandLineStyle("Affix");
295 m_commandLine->insert(selected);
297 // XXX. deletees?
299 // Corresponding signatures
300 setCommandLineStyle("Main");
301 m_commandLine->insert("\n\nSignatures:\n");
303 Q_ASSERT(mini != 0);
304 CSignatureCollection* Signatures = mini->GetSignatures();
305 setCommandLineStyle("Signature");
306 for (int i = 0; i < Signatures->GetCount(); ++i) {
307 const CSignature& sig = *Signatures->GetAt(i);
309 if (sig.Contains(selected))
310 m_commandLine->insert(QString("\t%1\n")
311 .arg(sig.Display('.', filter)));
314 // Corresponding stems
315 setCommandLineStyle("Main");
316 m_commandLine->insert("\n\nStems:\n\t");
317 setCommandLineStyle("Stem");
318 int nColumns = 3;
319 int tablength = 0;
320 if (CPrefix* pPrefix = *mini->GetPrefixes() ^= selected) {
321 CParse* pParse = pPrefix->GetStemString();
323 for (int i = 1; i <= pParse->Size(); ) {
324 for(int j = 1; j <= nColumns; ++j) {
325 QString text = pParse->GetPiece(i)
326 .Display(filter);
327 ++i;
329 m_commandLine->insert(QString("%1\t")
330 .arg(text));
332 // Make tab length depend on the longest
333 // word and font size
334 tablength = std::max(tablength,
335 text.size() *
336 m_commandLine->pointSize());
337 m_commandLine->insert("\n\t");
339 m_commandLine->setTabStopWidth(tablength);
342 break;
344 case TRIE:
345 break;
346 case STEMS_SUFFIXED:
347 break;
348 case SIGNATURE_SUBSET:
349 case SIGNATURES:
350 case PREFIX_SIGNATURES:
351 case SUFFIX_SIGNATURES:
353 const QString sigPrefix = " : ";
355 if (m_commandParse == 0)
356 break;
358 // XXX. avoid work if “command line” is not visible
360 CSignature* sig = static_cast<CSignature*>(m_commandParse);
361 QString selected = sig->Display('.', filter);
362 if (selected.left(3) == sigPrefix)
363 selected = selected.mid(3);
365 if (selected.isEmpty()) {
366 setCommandLineStyle("Error");
367 m_commandLine->insert(QString("Not found."));
368 return;
371 setCommandLineStyle("Signature");
372 m_commandLine->insert(selected);
374 setCommandLineStyle("Normal");
375 m_commandLine->insert("\n\nStems:\n");
376 setCommandLineStyle("Stem");
378 CSignature* pSig = *mini->GetSignatures() ^= selected ;
379 Q_ASSERT(pSig != 0);
381 // alphabetize stems from the right end.
382 CParse StemList = pSig->GetStems();
383 StemList.Display();
384 StemList.ReverseAlphabetize(); // from the end of the stem.
386 m_commandLine->insert("\n\n");
387 int nColumns = 5;
388 int tablength = 0;
389 for (int i = 1; i <= pSig->GetNumberOfStems(); ) {
390 for (int j = 1; j <= nColumns; ++j) {
391 QString text = StemList[i].Display(filter)
392 .trimmed();
393 ++i;
395 m_commandLine->insert(QString("%1\t")
396 .arg(text));
398 // Make tab length depend on the longest
399 // word and font size
400 tablength = std::max(tablength,
401 text.length() *
402 m_commandLine->pointSize());
405 m_commandLine->insert("\n");
407 m_commandLine->setTabStopWidth(tablength);
409 m_commandLine->insert("\nDL of my corpus: ");
410 m_commandLine->insert(QString::number(
411 pSig->ComputeDLofMyCorpus()));
413 m_commandLine->insert("\nDL of my stem pointers: ");
414 m_commandLine->insert(QString::number(
415 pSig->GetDLofMyStemPointers()));
417 m_commandLine->insert("\nDL of my suffix pointers: ");
418 m_commandLine->insert(QString::number(
419 pSig->GetDLofMyAffixPointers()));
421 m_commandLine->insert("\nNumber of stem pointers: ");
422 m_commandLine->insert(QString::number(
423 pSig->GetStemPtrList()->count()));
425 // XXX. Display signature's stems graphically
426 // updateSmallGraphicDisplaySlotForMultiDimensionData(...)
427 break;
429 case POS:
431 QString dummy;
432 CSignature* pSig;
433 if (m_commandParse == 0)
434 break;
435 LxPoS* pos = static_cast<LxPoS*>(m_commandParse);
436 pSig = pos->GetSignature();
437 QString selected = pSig->Display('.', filter);
439 if (selected.isEmpty()) {
440 setCommandLineStyle("Error");
441 m_commandLine->insert(QString("Not found."));
442 return;
447 setCommandLineStyle("Signature");
448 m_commandLine->insert(selected + "\t" + dummy.setNum(pSig->GetNumberOfStems() ) + " stems") ;
451 for (int signo = 0; signo < pos->GetNumberOfSignatures()-1; signo++ ) // it's -1 because the number of signatures is one more than in the List; don't forget the "mother" signature
453 CSignature* qSig = pos->GetSignature(signo);
454 m_commandLine->insert( "\n" + qSig->Display() + "\t"+ dummy.setNum( qSig->GetNumberOfStems()) + " stems") ;
458 setCommandLineStyle("Normal");
459 m_commandLine->insert("\n\nSatellite affixes:\n");
460 break;
463 case COMPOUNDS:
464 updateSmallGraphicDisplaySlot();
465 break;
466 case FSA_DOC:
468 FSAListViewItem* pFSAListViewItem =
469 static_cast<FSAListViewItem*>(item);
470 pFSAListViewItem->DisplayEdgePath(m_commandLine);
471 QPixmap* pFSAImage = pFSAListViewItem->GetQPixmap();
472 if(!pFSAImage->isNull()) //if using graph-viz
474 this->pImgLabel->setPixmap(
475 pFSAImage->scaled(pImgLabel->size(),
476 Qt::KeepAspectRatio,
477 Qt::SmoothTransformation) );
479 updateSmallGraphicDisplaySlot();
480 break;
482 case NO_DOC:
483 default:
484 break;
487 // further additions use plain text
488 setCommandLineStyle("Main");
489 m_commandLine->setCursorPosition(0, 0);