00001 #include "SequenceDialog.h"
00002
00003 SequenceDialog::SequenceDialog( Preferences* prefs, QWidget* parent )
00004 : QDialog( parent, 0, true ), prefs( prefs ) {
00005 init();
00006 }
00007
00008 SequenceDialog::~SequenceDialog() {
00009 }
00010
00011 void SequenceDialog::init() {
00012 sequencePanel = new QHBox( this );
00013 sequencePanel->setSpacing( 10 );
00014
00015 quizPanelWrapper = new QVGroupBox( tr( "Quiz" ), sequencePanel, "QuizPanelWrapper" );
00016 quizPanel = new QVBox( quizPanelWrapper );
00017
00018 quizTopPanel = new QHBox( quizPanel );
00019
00020 quizTopLeftPanel = new QVBox( quizTopPanel );
00021 quizTopLeftPanel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
00022
00023 quizFirstLangPanel = new QHGroupBox( tr( "FirstLanguage" ), quizTopLeftPanel, "QuizFirstLangPanel" );
00024 quizFirstLangPanelWrapper = new QHBox( quizFirstLangPanel );
00025 quizFirstLangPanelWrapper->setSpacing( 2 );
00026 quizFirstLangLabel = new QLabel( tr( "Word/Expr." ), quizFirstLangPanelWrapper, "QuizFirstLangLabel" );
00027 quizFirstLangTermButton = new QPushButton( "a", quizFirstLangPanelWrapper, "QuizFirstLangTermButton" );
00028 quizFirstLangTermButton->installEventFilter( this );
00029 quizFirstLangPanelWrapper->setStretchFactor( quizFirstLangTermButton, 1 );
00030 quizFirstLangPanel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00031
00032 quizTopPanel->setStretchFactor( quizTopLeftPanel, 1 );
00033
00034 quizTestLangPanel = new QHGroupBox( tr( "TestLanguage" ), quizTopLeftPanel, "QuizTestLangPanel" );
00035 quizTestLangPanelWrapper = new QHBox( quizTestLangPanel );
00036 quizTestLangPanelWrapper->setSpacing( 2 );
00037 quizTestLangLabelsPanel = new QVBox( quizTestLangPanelWrapper );
00038 quizTestLangLabelsPanel->setSpacing( 2 );
00039 quizTestLangButtonsPanel = new QVBox( quizTestLangPanelWrapper );
00040 quizTestLangButtonsPanel->setSpacing( 2 );
00041 quizTestLangAltLabel = new QLabel( tr( "Alt./Phon." ), quizTestLangLabelsPanel, "QuizTestLangAltLabel" );
00042 quizTestLangTermLabel = new QLabel( tr( "Word/Expr." ), quizTestLangLabelsPanel, "QuizTestLangTermLabel" );
00043 quizTestLangAltButton = new QPushButton( "b", quizTestLangButtonsPanel, "QuizTestLangAltButton" );
00044 quizTestLangAltButton->installEventFilter( this );
00045 quizTestLangTermButton = new QPushButton( "c", quizTestLangButtonsPanel, "QuizTestLangTermButton" );
00046 quizTestLangTermButton->installEventFilter( this );
00047 quizTestLangPanelWrapper->setStretchFactor( quizTestLangButtonsPanel, 1 );
00048
00049 quizImagePanel = new QVGroupBox( tr( "Image" ), quizTopPanel, "QuizImagePanel" );
00050 quizImagePanel->setFixedWidth( 130 );
00051 quizImageButton = new QPushButton( "e", quizImagePanel, "QuizImageButton" );
00052 quizImageButton->installEventFilter( this );
00053 quizImageButton->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00054
00055 quizCommentBox = new QVBox( quizPanel );
00056 quizCommentLabel = new QLabel( tr( "CommentLabelPanel" ), quizCommentBox );
00057 quizCommentButton = new QPushButton( "d", quizCommentBox, "QuizCommentButton" );
00058 quizCommentButton->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00059 quizCommentButton->installEventFilter( this );
00060 quizCommentBox->setStretchFactor( quizCommentButton, 1 );
00061 quizPanel->setStretchFactor( quizCommentBox, 1 );
00062
00063 sequencePanelButtons = new QVBox( sequencePanel );
00064 addSequenceMarkButton = new QPushButton( ">", sequencePanelButtons, "AddSequenceMarkButton" );
00065 addSequenceMarkButton->setEnabled( false );
00066 addSequenceMarkButton->setMinimumWidth( 50 );
00067 connect( addSequenceMarkButton, SIGNAL( clicked() ), this, SLOT( addSequenceMark() ) );
00068 addGroupMarkButton = new QPushButton( "+", sequencePanelButtons, "AddGroupMarkButton" );
00069 addGroupMarkButton->setEnabled( false );
00070 addGroupMarkButton->setMinimumWidth( addSequenceMarkButton->sizeHint().width() );
00071 connect( addGroupMarkButton, SIGNAL( clicked() ), this, SLOT( addGroupMark() ) );
00072 removeLastMarkButton = new QPushButton( "X", sequencePanelButtons, "RemoveLastMarkButton" );
00073 removeLastMarkButton->setEnabled( false );
00074 removeLastMarkButton->setMinimumWidth( addSequenceMarkButton->sizeHint().width() );
00075 connect( removeLastMarkButton, SIGNAL( clicked() ), this, SLOT( removeLastToken() ) );
00076
00077 sequenceLinePanel = new QHBox( this );
00078 sequenceLinePanel->setSpacing( 10 );
00079 sequenceLineLabel = new QLabel( tr( "Sequence" ), sequenceLinePanel, "SequenceLineLabel" );
00080 sequenceLineLineEdit = new QLineEdit( sequenceLinePanel, "SequenceLineLineEdit" );
00081 sequenceLineLineEdit->setReadOnly( true );
00082
00083 mainLayout = new QVBoxLayout( this );
00084 mainLayout->setSpacing( 6 );
00085 mainLayout->setMargin( 6 );
00086 mainLayout->addWidget( sequenceLinePanel );
00087 mainLayout->addWidget( sequencePanel, 1 );
00088 mainLayout->activate();
00089
00090 setCaption( tr( "AddRevealingSequence" ) );
00091 }
00092
00093 Sequence SequenceDialog::getSequence() const {
00094 return( sequence );
00095 }
00096
00097 void SequenceDialog::accept() {
00098 if( sequenceLineLineEdit->text().isEmpty() ) {
00099 QMessageBox::warning( this, QObject::tr( "Warning" ), tr( "SequenceIsEmpty" ) );
00100 return;
00101 }
00102
00103 QString lastChar = sequenceLineLineEdit->text().right( 1 );
00104 if( lastChar == " " || lastChar == "+" ) {
00105 QMessageBox::warning( this, QObject::tr( "Warning" ), tr( "SequenceNotTerminated" ) );
00106 return;
00107 }
00108
00109 currGroup.append( items.top() );
00110 sequence.addGroup( currGroup );
00111 currGroup.clear();
00112
00113 QDialog::accept();
00114 }
00115
00116 bool SequenceDialog::eventFilter( QObject* obj, QEvent* evt ) {
00117 if( evt->type() == QEvent::MouseButtonRelease ) {
00118 QMouseEvent* mouseEvt = (QMouseEvent*)evt;
00119 if( mouseEvt->button() == LeftButton ) {
00120 if( obj && obj->inherits( "QPushButton" ) ) {
00121 QPushButton* button = (QPushButton*)obj;
00122 addSequenceItem( button->text() );
00123 }
00124 }
00125 }
00126 return( false );
00127 }
00128
00129 void SequenceDialog::addSequenceItem( const QString& itemStr ) {
00130 sequenceLineLineEdit->setText( items.count() == 0 ? itemStr : sequenceLineLineEdit->text() + itemStr );
00131 Sequence::Item item = Sequence::stringToItem( itemStr );
00132 items.push( item );
00133 setItemButtonsEnabled( false );
00134 setUnionButtonsEnabled( true );
00135 removeLastMarkButton->setEnabled( true );
00136 }
00137
00138 void SequenceDialog::addSequenceMark() {
00139 currGroup.append( items.top() );
00140 sequence.addGroup( currGroup );
00141 currGroup.clear();
00142 sequenceLineLineEdit->setText( sequenceLineLineEdit->text() + " > " );
00143 setItemButtonsEnabled( true );
00144 setUnionButtonsEnabled( false );
00145 removeLastMarkButton->setEnabled( true );
00146 }
00147
00148 void SequenceDialog::addGroupMark() {
00149 currGroup.append( items.top() );
00150 sequenceLineLineEdit->setText( sequenceLineLineEdit->text() + "+" );
00151 setItemButtonsEnabled( true );
00152 setUnionButtonsEnabled( false );
00153 removeLastMarkButton->setEnabled( true );
00154 }
00155
00156 void SequenceDialog::removeLastToken() {
00157 QString seqStr = sequenceLineLineEdit->text();
00158 QString lastChar = seqStr.right( 1 );
00159 if( lastChar == "+" ) {
00160 currGroup.remove( items.top() );
00161 setItemButtonsEnabled( false );
00162 setUnionButtonsEnabled( true );
00163 sequenceLineLineEdit->setText( seqStr.left( seqStr.length() - 1 ) );
00164 }
00165 else if( lastChar == " " ) {
00166 currGroup.remove( items.top() );
00167 Sequence::ItemList lastGroup = sequence.getGroupAt( sequence.getGroupCount() );
00168 sequence.removeLastGroup();
00169 currGroup = lastGroup;
00170 setItemButtonsEnabled( false );
00171 setUnionButtonsEnabled( true );
00172 sequenceLineLineEdit->setText( seqStr.left( seqStr.length() - 3 ) );
00173 }
00174 else {
00175 items.pop();
00176 setItemButtonsEnabled( true );
00177 setUnionButtonsEnabled( false );
00178 sequenceLineLineEdit->setText( seqStr.left( seqStr.length() - 1 ) );
00179 }
00180 removeLastMarkButton->setEnabled( sequenceLineLineEdit->text().length() > 0 );
00181 }
00182
00183 void SequenceDialog::setUnionButtonsEnabled( bool isEnabled ) {
00184 addSequenceMarkButton->setEnabled( isEnabled && areItemsLeft() );
00185 addGroupMarkButton->setEnabled( isEnabled && areItemsLeft() );
00186 }
00187
00188 bool SequenceDialog::isItemUsed( const Sequence::Item& item ) const {
00189 return( items.contains( item ) );
00190 }
00191
00192 bool SequenceDialog::areItemsLeft() const {
00193 return( !isItemUsed( Sequence::FIRST_LANG_TERM ) || !isItemUsed( Sequence::TEST_LANG_ALT ) || !isItemUsed( Sequence::TEST_LANG_TERM ) ||
00194 !isItemUsed( Sequence::COMMENT ) || !isItemUsed( Sequence::IMAGE ) );
00195 }
00196
00197 void SequenceDialog::setItemButtonsEnabled( bool isEnabled ) {
00198 quizFirstLangTermButton->setEnabled( isEnabled && !isItemUsed( Sequence::FIRST_LANG_TERM ) );
00199 quizTestLangAltButton->setEnabled( isEnabled && !isItemUsed( Sequence::TEST_LANG_ALT ) );
00200 quizTestLangTermButton->setEnabled( isEnabled && !isItemUsed( Sequence::TEST_LANG_TERM ) );
00201 quizCommentButton->setEnabled( isEnabled && !isItemUsed( Sequence::COMMENT ) );
00202 quizImageButton->setEnabled( isEnabled && !isItemUsed( Sequence::IMAGE ) );
00203 }