mainwindow.cpp 31.9 KB
Newer Older
Denis Khachko's avatar
Denis Khachko committed
1 2 3 4
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "interface.h"

Victor Yacovlev's avatar
Victor Yacovlev committed
5

Denis Khachko's avatar
Denis Khachko committed
6 7 8 9 10 11 12 13 14 15 16 17
MainWindowTask::MainWindowTask(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowTask)
{

cursFile="";
  course=NULL;
    curDir="";
    progChange.clear();
    connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(aboutToQuit()));

}
18
 void MainWindowTask::setup(const QDir & resourcesRoot, ExtensionSystem::SettingsPtr sett)
Denis Khachko's avatar
Denis Khachko committed
19
 {
Denis Khachko's avatar
Denis Khachko committed
20
       course=NULL;
Denis Khachko's avatar
Denis Khachko committed
21
     ui->setupUi(this);
Denis Khachko's avatar
Denis Khachko committed
22
     isReadOnly=false;
Denis Khachko's avatar
Denis Khachko committed
23 24 25
     ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
     ui->treeView->setIconSize(QSize(25,25));
     ui->treeView->setStyleSheet("icon-size: 25px;font-size: 14px;");
Denis Khachko's avatar
Denis Khachko committed
26
     settings = sett;
Denis Khachko's avatar
Denis Khachko committed
27
     customMenu.hide();
28 29
     connect(ui->loadCurs,SIGNAL(triggered()),this,SLOT(loadCourse()));
     connect(ui->actionSave,SIGNAL(triggered()),this,SLOT(saveCourse()));
Denis Khachko's avatar
Denis Khachko committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
     connect(ui->treeView,SIGNAL(clicked(QModelIndex)),this,SLOT(showText(QModelIndex)));
       connect(ui->do_task,SIGNAL(triggered()),this,SLOT(startTask()));
       qDebug()<<"Check Connect tttttttttttttttttt";
       connect(ui->checkTask,SIGNAL(triggered()),this,SLOT(checkTask()));
      connect(ui->actionReset,SIGNAL(triggered()),this,SLOT(resetTask()));
        connect(ui->actionClose,SIGNAL(triggered()),this,SLOT(Close()));
           connect(ui->actionTested,SIGNAL(triggered()),this,SLOT(returnTested()));
           connect(ui->treeView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(customContextMenuRequested(QPoint)));

           customMenu.addAction(ui->actionAdd);
           customMenu.addAction(ui->actionRemove);
           customMenu.addAction(ui->actionEdit);
       customMenu.addSeparator();
       customMenu.addAction(ui->actionup);
       customMenu.addAction(ui->actionDown);
         customMenu.addAction(ui->addDeep);
       connect(ui->actionup,SIGNAL(triggered()),this,SLOT(moveUp()));
       connect(ui->actionDown,SIGNAL(triggered()),this,SLOT(moveDown()));
           connect(ui->actionAdd,SIGNAL(triggered()),this,SLOT(addTask()));
            connect(ui->addDeep,SIGNAL(triggered()),this,SLOT(addDeepTask()));
           connect(ui->actionSaveK,SIGNAL(triggered()),this,SLOT(saveKurs()));
            connect(ui->actionSaveKas,SIGNAL(triggered()),this,SLOT(saveKursAs()));
           connect(ui->actionRemove,SIGNAL(triggered()),this,SLOT(deleteTask()));
Denis Khachko's avatar
Denis Khachko committed
53 54 55 56
         //  newDialog=new newKursDialog();
          // connect(ui->actionNewK,SIGNAL(triggered()),this,SLOT(newKurs()));
         //  editDialog = new EditDialog(this);
          // connect(ui->actionEdit,SIGNAL(triggered()),this,SLOT(editTask()));
Denis Khachko's avatar
Denis Khachko committed
57 58 59 60 61 62
          // ui->menuKurs->menuAction()->setEnabled(false);
           setEditTaskEnabled(false);
           ui->treeView->setSelectionMode(QAbstractItemView::SingleSelection);
           editRoot=new QLineEdit(ui->treeView);
           editRoot->hide();
           connect(editRoot,SIGNAL(editingFinished ()),this,SLOT(endRootEdit()));
Denis Khachko's avatar
Denis Khachko committed
63
           ui->treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Denis Khachko's avatar
Denis Khachko committed
64 65 66
isTeacher=false;
       onTask=false;
       cursFile="";
67
       setWindowIcon(QIcon(resourcesRoot.absoluteFilePath("10.png")));
68 69 70
#ifdef QT_DEBUG
     ui->webView->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
#endif
Denis Khachko's avatar
Denis Khachko committed
71 72
       //ui->textBrowser->setVisible(false);
 };
Denis Khachko's avatar
Lost 2  
Denis Khachko committed
73
QList<QAction*> MainWindowTask::getActions()
Denis Khachko's avatar
Denis Khachko committed
74 75 76 77
{
    QList<QAction*> toRet;
    toRet.append(ui->loadCurs);
    toRet.append(ui->checkTask);
Denis Khachko's avatar
Denis Khachko committed
78 79 80
    toRet.append(ui->actionSave);
    toRet.append(ui->actionReset);
    toRet.append(ui->actionTested);
Denis Khachko's avatar
Lost 2  
Denis Khachko committed
81
    return toRet;
Denis Khachko's avatar
Denis Khachko committed
82
}
Denis Khachko's avatar
Denis Khachko committed
83

Denis Khachko's avatar
Denis Khachko committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
MainWindowTask::~MainWindowTask()
{
    delete ui;
}

void MainWindowTask::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
Denis Khachko's avatar
Denis Khachko committed
100 101
void MainWindowTask::updateLastFiles(const QString newFile )
{
Denis Khachko's avatar
Denis Khachko committed
102
      QStringList lastFiles= settings->value("Courses/LastFiles","").toString().split(";");
Denis Khachko's avatar
Denis Khachko committed
103
    qDebug()<<lastFiles;
Denis Khachko's avatar
Denis Khachko committed
104
     qDebug()<<settings->locationDirectory();
Denis Khachko's avatar
Denis Khachko committed
105 106 107 108 109 110 111
    if(lastFiles.indexOf(newFile)<0)lastFiles.prepend(newFile);
    int max_fid=std::min(lastFiles.count(),10);
    QString sett="";
    for(int i=0;i<max_fid;i++)
    {
        sett+= lastFiles.at(i)+";";
    }
Denis Khachko's avatar
Denis Khachko committed
112 113
    settings->setValue("Courses/LastFiles",sett);
    interface->rebuildRescentMenu();
Denis Khachko's avatar
Denis Khachko committed
114
};
Denis Khachko's avatar
Denis Khachko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
void MainWindowTask::loadCourseData(const QString fileName)
{

course=new courseModel();
connect(course,SIGNAL(dataChanged (QModelIndex ,QModelIndex )),ui->treeView,SLOT(dataChanged(QModelIndex,QModelIndex)));
int tasks=course->loadCourse(fileName);
course->setTeacher(isTeacher);
if(tasks==-1)
{
//QMessageBox::information( 0, "", trUtf8("Ошибка открытия файла: ") + fileName, 0,0,0);
return;
};
ui->treeView->setModel(course);
curTaskIdx=QModelIndex();
onTask=false;
ui->actionSave->setEnabled(true);

changes.cleanChanges();
cursFile=fileName;
};
void MainWindowTask::loadMarks(const QString fileName)
{
    QDomDocument workXml;
    QFile f(fileName);
Denis Khachko's avatar
Denis Khachko committed
139
    
Denis Khachko's avatar
Denis Khachko committed
140 141 142 143 144 145
    if  (!f.open(QIODevice::ReadOnly))
    {
        QMessageBox::information( 0, "", trUtf8("Ошибка открытия файла: ") + fileName, 0,0,0);
        return;

    };
Denis Khachko's avatar
Denis Khachko committed
146 147 148 149 150 151 152
    QFileInfo fi(f);
    if(!fi.isWritable())
    {
       QMessageBox::information( 0, "", trUtf8("Файл загружен только для чтения, изменение не будут сохранены: ") + fileName, 0,0,0);
        isReadOnly=true;
    }else
     isReadOnly=false;   
Denis Khachko's avatar
Denis Khachko committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
if(f.atEnd())
    {
    QMessageBox::information( 0, "", trUtf8("Ошибка открытия файла ,файл пуст: ") + fileName, 0,0,0);
    return;
    };
cursWorkFile.setFileName(f.fileName());
QString error;
int str,pos;
workXml.setContent(f.readAll(),true,&error,&str,&pos);
qDebug()<<"File parce:"<<error<<"str"<<str<<" pos"<<pos;

QDomElement root=workXml.documentElement ();
if(root.tagName()!="COURSE")
  {
        QMessageBox::information( 0, "", trUtf8("Ошибка загрузки файла: ") + fileName, 0,0,0);
        return;
  };
QDomElement fileEl=root.firstChildElement("FILE");
QString krsFile=fileEl.attribute("fileName");
loadCourseData(krsFile);//Gruzim kurs

if(cursFile!=krsFile){//Esli ne udalos po puti - ishem v toyje direktorii
    QFileInfo finf(fileEl.attribute("fileName"));
    QFileInfo cfi(cursWorkFile);
    qDebug()<<"PATH"<<cfi.dir().canonicalPath()+"/"+finf.fileName();
    krsFile=cfi.dir().canonicalPath()+"/"+finf.fileName();
    curDir=cfi.dir().canonicalPath();
Denis Khachko's avatar
Denis Khachko committed
180 181
    QFileInfo krsFi(krsFile);
    if(krsFi.isReadable())
Denis Khachko's avatar
Denis Khachko committed
182 183 184 185 186
    loadCourseData(krsFile);

}

QString fileN=fileEl.attribute("fileName");
Denis Khachko's avatar
Denis Khachko committed
187
//qDebug()<<"KURS ZAGRUZILI";
Denis Khachko's avatar
Denis Khachko committed
188 189 190 191 192 193 194 195 196
if(cursFile!=krsFile){
    QMessageBox::information( 0, "", trUtf8("Не наеден файл курса:") + fileEl.attribute("fileName"), 0,0,0);
    fileN=getFileName(krsFile);
    loadCourseData(fileN);
    if(cursFile!=fileN)return;
    }
QFileInfo fi_kurs=QFileInfo(krsFile);
curDir=fi_kurs.absolutePath();
QDomNodeList marksElList=root.elementsByTagName("MARK"); //Оценки
Denis Khachko's avatar
Denis Khachko committed
197
//qDebug()<<"Loading marks "<<marksElList.count();
Denis Khachko's avatar
Denis Khachko committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
for(int i=0;i<marksElList.count();i++)
 {
 int taskId=marksElList.at(i).toElement().attribute("testId").toInt();
 int mark=marksElList.at(i).toElement().attribute("mark").toInt();
 qDebug()<<"task:"<<taskId<<" mark:"<<mark;
 course->setMark(taskId,mark);
 changes.setMark(taskId,mark);
 };

qDebug()<<"Loading user prgs...";
QDomNodeList prgElList=root.elementsByTagName("USER_PRG");//Программы
for(int i=0;i<prgElList.count();i++)
 {
 int taskId=prgElList.at(i).toElement().attribute("testId").toInt();
 qDebug()<<"Tassk id"<<taskId;
 QString prg =prgElList.at(i).toElement().attribute("prg");
 QModelIndex tIdx=course->getIndexById(taskId);

 if(progChange.indexOf(taskId)==-1){

     progChange.append(taskId);
      };
 course->setUserText(taskId,prg);

 };

QDomNodeList prgElListT=root.elementsByTagName("TESTED_PRG");//Программы тестированные
for(int i=0;i<prgElListT.count();i++)
 {
 int taskId=prgElListT.at(i).toElement().attribute("testId").toInt();
 QString prg =prgElListT.at(i).toElement().attribute("prg");

 course->setUserTestedText(taskId,prg);

 };
};
Denis Khachko's avatar
Denis Khachko committed
234 235 236 237 238 239 240
 void MainWindowTask::loadCourseFromFile(const QString & file)
{QFileInfo fi(file);
    if(!fi.exists())
    {
        
        return;
    };
Denis Khachko's avatar
Denis Khachko committed
241

Denis Khachko's avatar
Denis Khachko committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    baseKursFile=fi;
    curDir=fi.absolutePath ();
    settings->setValue("Directories/Kurs", curDir);
    qDebug()<<"curDir"<<curDir;
    QString fileName=file;
    progChange.clear();
    if(fileName.right(9)==".work.xml")//Загрузка оценок и программ
    {
        loadMarks(fileName);
        this->show();
        return;
    }else
        cursWorkFile.setFileName("");
    loadCourseData(fileName);
    QString cText=course->courceDescr();
    
    
    if(cText.right(4)==".htm" ||cText.right(5)==".html" )
    {
        loadHtml(cText);
    }else ui->webView->setHtml(cText);
    // if(isTeacher)ui->actionEdit->setEnabled(true);
    setWindowTitle(course->name()+trUtf8(" - Практикум"));
    updateLastFiles(fileName);
    interface->lockContrls();
267
    interface->setPreProgram(QVariant(""));
Denis Khachko's avatar
Denis Khachko committed
268 269 270
    ui->checkTask->setEnabled(false);
    this->show();
};
Denis Khachko's avatar
Denis Khachko committed
271 272 273 274 275 276 277 278 279 280 281 282 283

void MainWindowTask::loadCourse()
{
    editRoot->hide();
    ui->splitter->setEnabled(true);
    QString dir=settings->value("Directories/Kurs","").toString();
    QDir chD(dir);
    if(!chD.exists())dir=QDir::homePath();
//    QFileDialog dialog(this,trUtf8("Открыть файл"),dir, "(*.kurs.xml *.work.xml)");
//     dialog.setAcceptMode(QFileDialog::AcceptOpen);
//     if(!dialog.exec())return;


Denis Khachko's avatar
Denis Khachko committed
284
     QString	File=QFileDialog::getOpenFileName(this, QString::fromUtf8 ("Открыть файл"), dir, "Xml (*.xml)");
Denis Khachko's avatar
Denis Khachko committed
285
     QFileInfo fi(File);
Denis Khachko's avatar
Denis Khachko committed
286 287 288 289 290
    if(!fi.exists())
    {
           
        return;
    };
Denis Khachko's avatar
Denis Khachko committed
291
    this->showNormal();
Denis Khachko's avatar
Denis Khachko committed
292 293 294 295 296 297 298 299 300

     baseKursFile=fi;
     curDir=fi.absolutePath ();
     settings->setValue("Directories/Kurs", curDir);
     qDebug()<<"curDir"<<curDir;
     QString fileName=File;
     progChange.clear();
     if(fileName.right(9)==".work.xml")//Загрузка оценок и программ
     {
Denis Khachko's avatar
Denis Khachko committed
301
         isReadOnly=false;
Denis Khachko's avatar
Denis Khachko committed
302
         loadMarks(fileName);
303 304
         Q_EMIT activateRequest();
//         this->show();
Denis Khachko's avatar
Denis Khachko committed
305
         return;
306 307 308 309 310 311 312 313 314 315
     }
    bool createDefaultWorkFile=true;
    QMessageBox::StandardButton ans;
            ans = QMessageBox::question(this, trUtf8("Практикум"), trUtf8("Вы хотите создать тетрадь?"),
                                                                                                                            QMessageBox::Yes | QMessageBox::No , QMessageBox::Yes);
            if (ans == QMessageBox::Yes)
            {
                createDefaultWorkFile=false;
                
            };
Denis Khachko's avatar
Denis Khachko committed
316
    
Denis Khachko's avatar
Denis Khachko committed
317 318
     cursWorkFile.setFileName("");
     loadCourseData(fileName);
Denis Khachko's avatar
Denis Khachko committed
319
     isReadOnly=false;
320
     interface->setPreProgram(QVariant(""));
Denis Khachko's avatar
Denis Khachko committed
321
     QString cText=course->courceDescr();
Denis Khachko's avatar
Denis Khachko committed
322
  
Denis Khachko's avatar
Denis Khachko committed
323 324 325 326 327

  if(cText.right(4)==".htm" ||cText.right(5)==".html" )
  {
      loadHtml(cText);
  }else ui->webView->setHtml(cText);
Denis Khachko's avatar
Denis Khachko committed
328
 // if(isTeacher)ui->actionEdit->setEnabled(true);
Denis Khachko's avatar
Denis Khachko committed
329
  setWindowTitle(course->name()+trUtf8(" - Практикум"));
Denis Khachko's avatar
Denis Khachko committed
330
    updateLastFiles(fileName);
331 332
    interface->lockContrls();
    ui->checkTask->setEnabled(false);
333 334
    Q_EMIT activateRequest();
//    this->show();
335 336 337 338 339 340 341
    if(createDefaultWorkFile)
    {
        markProgChange();
        //curDir=QDir::currentPath();
        qDebug()<<curDir;

        cursWorkFile.setFileName(QDir::currentPath()+"/default.work.xml");
Denis Khachko's avatar
Denis Khachko committed
342
        
343 344 345 346 347
        saveCourseFile();
    }else
    {
        saveCourse();
    };
Denis Khachko's avatar
Denis Khachko committed
348
    
Denis Khachko's avatar
Denis Khachko committed
349 350
};

Denis Khachko's avatar
Denis Khachko committed
351 352 353
void MainWindowTask::openRescent()
{
    
Denis Khachko's avatar
Denis Khachko committed
354

Denis Khachko's avatar
Denis Khachko committed
355 356
    
    QAction *s = qobject_cast<QAction*>(sender());
Denis Khachko's avatar
Denis Khachko committed
357 358
    
    loadCourseFromFile(s->property ( "fullName" ).toString());
Denis Khachko's avatar
Denis Khachko committed
359
  //  if( LoadFromFile(RobotFile)!=0)QMessageBox::information( mainWidget(), "", QString::fromUtf8("Ошибка открытия файла! ")+RobotFile, 0,0,0);
Denis Khachko's avatar
Denis Khachko committed
360
Q_EMIT activateRequest();
Denis Khachko's avatar
Denis Khachko committed
361
};
Denis Khachko's avatar
Denis Khachko committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

void MainWindowTask::setUpDown(QModelIndex index)
{
    if(!isTeacher){ui->actionup->setEnabled(false);
        ui->actionDown->setEnabled(false);
    }
    return;
    if(course->hasUpSib(index))ui->actionup->setEnabled(true);
      else ui->actionup->setEnabled(false);
      if(course->hasDownSib(index))ui->actionDown->setEnabled(true);
        else ui->actionDown->setEnabled(false);
};


void MainWindowTask::moveUp()

{
    curTaskIdx=course->moveUp(curTaskIdx);
    QModelIndex par=curTaskIdx.parent();
    ui->treeView->setCurrentIndex(curTaskIdx);
    setUpDown(curTaskIdx);


   ui->treeView->collapse(par);
   ui->treeView->expand(par);
saveBaseKurs();
};
void MainWindowTask:: moveDown()
{ ui->treeView->setCurrentIndex(curTaskIdx);

    curTaskIdx=course->moveDown(curTaskIdx);
    ui->treeView->setCurrentIndex(curTaskIdx);
    setUpDown(curTaskIdx);
  //ui->treeView->dataChanged(curTaskIdx,ui->treeView->indexAbove(curTaskIdx));
   //showText(curTaskIdx);
   // ui->treeView->update(curTaskIdx);

   ui->treeView->collapse(curTaskIdx.parent());
   ui->treeView->expand(curTaskIdx.parent());
};


void MainWindowTask::showText(const QModelIndex & index )
 {
      editRoot->hide();
    qDebug()<<"TASK IDX:"<<curTaskIdx.internalId();
    if(index.internalId()>0)setEditTaskEnabled(true);
    else setEditTaskEnabled(false);
    setUpDown(index);
    if(index==curTaskIdx)return;
//    if(onTask)
//    {
//        QMessageBox::StandardButton ans;
//        ans = QMessageBox::question(this, trUtf8("Задание"), trUtf8("Вы хотите сменить задание?"),
//                                                                                                                        QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes);
//        if ((ans == QMessageBox::Cancel)||(ans == QMessageBox::No))
//        {
//            qDebug()<<"Cancel";
//        if(curTaskIdx.internalId()>0)ui->treeView->setCurrentIndex(curTaskIdx);
//            return;};

//    }
    if(curTaskIdx.internalId()>0)
    {
        markProgChange();
    }
    onTask=false;
    ui->checkTask->setEnabled(false);
    QString taskText=course->getTaskText(index);
 //ui->textBrowser->setText(taskText);



 if(taskText.right(4)==".htm" ||taskText.right(5)==".html" )
 {
     loadHtml(taskText);
 }else ui->webView->setHtml(taskText);
  qDebug()<<"TaskText:"<<course->getTaskText(index);
 curTaskIdx=index;

 if(course->isTask(curTaskIdx.internalId()) && curTaskIdx.internalId()>0 && course->taskAvailable(curTaskIdx.internalId()))
   {
     ui->do_task->setEnabled(true);
     startTask();
     ui->do_task->setEnabled(false);
      ui->actionReset->setEnabled(true);
      QString testedText=course->getUserTestedText(curTaskIdx.internalId());
      qDebug()<<"TESTED TEXT"<<testedText;
      if(testedText!="")ui->actionTested->setEnabled(true);
   }
 else {
           ui->do_task->setEnabled(false);
           ui->actionReset->setEnabled(false);
           ui->actionTested->setEnabled(false);
       };
 };

void MainWindowTask::loadHtml(QString fileName)
{
    qDebug()<<"LoadHtml"<<fileName;
    if(fileName.isEmpty())return;
    QFile inp(curDir+'/'+fileName);
    if  (!inp.open(QIODevice::ReadOnly))
    {
    QMessageBox::information( 0, "", trUtf8("Ошибка чтения: ") + fileName, 0,0,0);
    return;
    };
    QString htmlData=QString::fromUtf8(inp.readAll());
    //ui->textBrowser->setHtml(htmlData);

    ui->webView->setHtml(htmlData,QUrl("file://"+curDir+'/'+fileName));
    inp.close();

};
void MainWindowTask::startTask()
 {editRoot->hide();
    qDebug()<<"StartTask";
     if(curTaskIdx.internalId()<=0)
     {
         QMessageBox::about(NULL, trUtf8("Не выбрано задание"),trUtf8("Необходимо выбрать задание"));
         return;
     }
484
     if(course->csName(curTaskIdx.internalId()).toLower()!=CS)
Denis Khachko's avatar
Denis Khachko committed
485 486 487 488 489 490
     {
         QMessageBox::about(NULL, trUtf8("Неправильное окружение"),trUtf8("Необходим ")+course->csName(curTaskIdx.internalId()));
         return;
     }

     QString progFile=course->progFile(curTaskIdx.internalId());
Denis Khachko's avatar
Denis Khachko committed
491
//interface->setTesting(loadTestAlg(course->getTaskCheck(curTaskIdx)));
Denis Khachko's avatar
Denis Khachko committed
492 493 494 495 496 497 498 499 500
QFileInfo ioDir(curDir+"/"+course->progFile(curTaskIdx.internalId()));
qDebug()<<"PRG FILE"<<course->progFile(curTaskIdx.internalId());
if(ioDir.isFile())
     {
     interface->setParam("input dir",ioDir.absoluteFilePath());
     };
     task.isps=course->Modules(curTaskIdx.internalId());
     task.name=course->getTitle(curTaskIdx.internalId());
       qDebug()<<"ISPS"<<task.isps;
501
       task.fields.clear();
Denis Khachko's avatar
Denis Khachko committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
       for(int i=0;i<task.isps.count();i++)
       {
      // task.Scripts.append(loadScript(course->Script(curTaskIdx.internalId(),task.isps[i])));
       QStringList t_fields=course->Fields(curTaskIdx.internalId(),task.isps[i]);
       qDebug()<<"fields"<<t_fields;
       task.fields.clear();
        for(int j=0;j<t_fields.count();j++)
            {
                qDebug()<<"Cur Dir"<<curDir;
            task.fields.insertMulti(task.isps[i],curDir+'/'+t_fields[j]);
            qDebug()<<curDir+'/'+t_fields[j];

            };
        qDebug()<<"Fields!!!!"<<task.fields;
        }
       qDebug()<<"MODULES:"<<course->Modules(curTaskIdx.internalId());
Denis Khachko's avatar
Denis Khachko committed
518
  if(!interface->startNewTask(course->Modules(curTaskIdx.internalId()),&task))QMessageBox::about(NULL, trUtf8("Невозможно выполнить задание"),trUtf8("Нет неоходимых исполнителей"));
Denis Khachko's avatar
Denis Khachko committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  if(course->getUserText(curTaskIdx.internalId())!="")
  {
      interface->setPreProgram(QVariant(course->getUserText(curTaskIdx.internalId())));
      ui->actionReset->setEnabled(true);
  }
   else
      if(!progFile.isEmpty())interface->setPreProgram(QVariant(curDir+'/'+progFile));



//qDebug()<<"Scripts "<<task.Scripts;
 ui->do_task->setEnabled(false);
 ui->checkTask->setEnabled(true);
 onTask=true;
 //ui->loadCurs->setEnabled(false);
 qDebug()<<"end load task";
 if(progChange.indexOf(curTaskIdx.internalId())==-1)progChange.append(curTaskIdx.internalId());
 };
 void MainWindowTask::checkTask()
 {
     qDebug()<<"CheckTASK";
     if(!onTask){qDebug()<<"!onTASK";return;};


markProgChange();
if(!cursWorkFile.exists()){

    QMessageBox::information( 0, "", trUtf8("Нужно завести файл рабочей тетради "), 0,0,0);
    saveCourse();
      };
course->setMark(curTaskIdx.internalId(),0);
Denis Khachko's avatar
Denis Khachko committed
550

Denis Khachko's avatar
Denis Khachko committed
551 552
     qDebug()<<"task"<<task.isps;
interface->startProgram(QVariant("TODO LOAD SCRIPT"),&task);
Denis Khachko's avatar
Denis Khachko committed
553 554 555 556
   //ui->loadCurs->setEnabled(false);

 };

Denis Khachko's avatar
Denis Khachko committed
557 558 559 560 561 562
 void MainWindowTask::lockControls()
{
    ui->splitter->setEnabled(false);
    ui->checkTask->setEnabled(false);
    ui->loadCurs->setEnabled(false);  
};
Denis Khachko's avatar
Denis Khachko committed
563

Denis Khachko's avatar
Denis Khachko committed
564 565 566 567 568 569
void MainWindowTask::unlockControls()
{
    ui->splitter->setEnabled(true);
    ui->checkTask->setEnabled(true);
    ui->loadCurs->setEnabled(true);  
};
Denis Khachko's avatar
Denis Khachko committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

void  MainWindowTask::setMark(int mark)
{
    ui->loadCurs->setEnabled(true);
    if(!onTask)return;
    ui->loadCurs->setEnabled(true);
  qDebug()<<"ui->cource enabled!";
    ui->splitter->setEnabled(true);
    ui->actionTested->setEnabled(true);
    qDebug()<<"ui->treeView enabled!";
    ui->checkTask->setEnabled(true);
    if((course->taskMark(curTaskIdx.internalId())<mark) &&  (course->taskMark(curTaskIdx.internalId())>0))return;

    course->setUserTestedText(curTaskIdx.internalId(),interface->getText());
    qDebug()<<"Mark:"<<mark;
    course->setMark(curTaskIdx.internalId(),mark);
    changes.setMark(curTaskIdx.internalId(),mark);
    ui->treeView->dataChanged(curTaskIdx,curTaskIdx);
    //if(mark==10)onTask=false;else onTask=true;

};
QString MainWindowTask::loadScript(QString file_name)
{
    qDebug()<<"Script file name:"<<file_name;
    if(file_name.isEmpty())return "";
    QFile file(curDir+"/"+file_name);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::about(NULL, trUtf8("Ошибка"),trUtf8("Невозможно открыть ")+curDir+"/"+file_name);
        return "";
    };
    return file.readAll();
};


void MainWindowTask::saveCourse()
{
    editRoot->hide();
   markProgChange();
Denis Khachko's avatar
Denis Khachko committed
609 610
    QFileDialog dialog(this,trUtf8("Сохранить изменения"),curDir, "Work files(*.work.xml);;All files (*)");
    dialog.setDefaultSuffix("work.xml");
Denis Khachko's avatar
Denis Khachko committed
611 612 613 614 615 616
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    if(!dialog.exec())return;
    QFileInfo fi(dialog.selectedFiles().first());
    //curDir=fi.absolutePath ();
    qDebug()<<"curDir"<<curDir;
    QString fileName=dialog.selectedFiles().first();
Denis Khachko's avatar
Denis Khachko committed
617 618
   // QString type=fileName.right(9);
   // if(type!=".work.xml")fileName+=".work.xml";
Denis Khachko's avatar
Denis Khachko committed
619
    cursWorkFile.setFileName(fileName);
Denis Khachko's avatar
Denis Khachko committed
620
    updateLastFiles(fileName);
Denis Khachko's avatar
Denis Khachko committed
621 622 623 624 625
    saveCourseFile();

};
void MainWindowTask::saveCourseFile()
{
Denis Khachko's avatar
Denis Khachko committed
626
    if(isReadOnly)return;
Denis Khachko's avatar
Denis Khachko committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    qDebug()<<"Save cource file";
    QDomDocument saveXml;

    QDomElement	crsEl=saveXml.createElement("COURSE");
    QDomElement	fileEl=saveXml.createElement("FILE");
    QDomAttr crsAtt=saveXml.createAttribute("fileName");
    crsAtt.setValue(cursFile);
    fileEl.setAttributeNode(crsAtt);

    crsEl.appendChild(fileEl);
    saveXml.appendChild(crsEl);
    QDomElement	mrksEl=saveXml.createElement("MARKS");

    //USER PROGRAMS n TESTED PROGRAMS
    for(int i=0;i<progChange.count();i++){
    QDomElement	prgEl=saveXml.createElement("USER_PRG");
    QDomAttr testIdprg=saveXml.createAttribute("testId");
    testIdprg.setValue(QString::number(progChange[i]));
    QDomAttr userPrg=saveXml.createAttribute("prg");
    userPrg.setValue(course->getUserText(progChange[i]));
    prgEl.setAttributeNode(testIdprg);
    prgEl.setAttributeNode(userPrg);
    crsEl.appendChild(prgEl);

    QDomElement	prgElT=saveXml.createElement("TESTED_PRG");
    QDomAttr testIdprgT=saveXml.createAttribute("testId");
    testIdprgT.setValue(QString::number(progChange[i]));
    QDomAttr userPrgT=saveXml.createAttribute("prg");
    userPrgT.setValue(course->getUserTestedText(progChange[i]));
    prgElT.setAttributeNode(testIdprg);
    prgElT.setAttributeNode(userPrgT);
    crsEl.appendChild(prgElT);



     }
    //END USER PROGRAMS




    QMapIterator<int, int> i(changes.marksChanged);
    while (i.hasNext()) {
        i.next();
        QDomElement mrk=saveXml.createElement("MARK");
        QDomAttr testId=saveXml.createAttribute("testId");
        testId.setValue(QString::number(i.key()));
        QDomAttr mvalue=saveXml.createAttribute("mark");
        mvalue.setValue(QString::number(i.value()));
        mrk.setAttributeNode(testId);
        mrk.setAttributeNode(mvalue);
        mrksEl.appendChild(mrk);
    }
    crsEl.appendChild(mrksEl);





          if  (!cursWorkFile.open(QIODevice::WriteOnly))
          {
          QMessageBox::information( 0, "", trUtf8("Ошибка записи: ") + cursWorkFile.fileName(), 0,0,0);
          return;
          };
    cursWorkFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    cursWorkFile.write(saveXml.toByteArray());
    cursWorkFile.close();

};

void MainWindowTask::markProgChange()
{

    course->setUserText(curTaskIdx,interface->getText());
    if(progChange.indexOf(curTaskIdx.internalId())==-1)progChange.append(curTaskIdx.internalId());
    if(!cursWorkFile.exists())
    {
      qDebug()<<"!cursWorkFile";
    }else saveCourseFile();

}
QString MainWindowTask::getFileName(QString fileName)
{
    QFileInfo finf(fileName);
    qDebug()<<"GET FILE!";
//    QFileDialog dialog(this,trUtf8("Открыть файл"),curDir, finf.fileName()+" *.kurs.xml");
//    dialog.setAcceptMode(QFileDialog::AcceptOpen);
//    if(!dialog.exec())return "";
//    QFileInfo fi(dialog.selectedFiles().first());



    QString	File=QFileDialog::getOpenFileName(this, QString::fromUtf8 ("Открыть файл"), curDir,  finf.fileName()+" *.kurs.xml");
    QFileInfo fi(File);


    return File;
};

void MainWindowTask::resetTask()
{
    QString progFile=course->progFile(curTaskIdx.internalId());


     if(!progFile.isEmpty())interface->setPreProgram(QVariant(curDir+'/'+progFile));
    course->setUserText(curTaskIdx,"");


};
void MainWindowTask::Close()
{
    qDebug()<<cursFile;
    if(cursFile!="")markProgChange();
    saveBaseKurs();
    close();
};
Denis Khachko's avatar
Denis Khachko committed
743 744
void MainWindowTask::showEvent(QShowEvent * event)
{
745
    ui->splitter->restoreState(settings->value("Window/SpliterState")
Denis Khachko's avatar
Denis Khachko committed
746
                 .toByteArray());
747
    QByteArray settlist=settings->value("Window/SpliterPos").toByteArray();
Denis Khachko's avatar
Denis Khachko committed
748 749 750 751
    qDebug()<<settlist;
    ui->splitter->restoreGeometry(settlist);
};

Denis Khachko's avatar
Denis Khachko committed
752 753
void MainWindowTask::closeEvent(QCloseEvent *event)
{
Denis Khachko's avatar
Denis Khachko committed
754 755 756
    
   
    
757 758 759
    settings->setValue("Window/SpliterPos",ui->splitter->saveGeometry());
    settings->setValue("Window/SpliterState",ui->splitter->saveState());
    settings->flush();
Denis Khachko's avatar
Denis Khachko committed
760 761
    qDebug()<<ui->splitter->saveGeometry();
    qDebug()<<"CLOSE TASK WINDOW";
Denis Khachko's avatar
Denis Khachko committed
762 763 764 765 766 767 768
  if(!course)return;
  qDebug()<<"START CLOSE TASK WINDOW";
  if(!cursWorkFile.exists())
  {
      QMessageBox::StandardButton ans;
            ans = QMessageBox::question(this, trUtf8("Курсы"), trUtf8("Вы хотите сохранить работу?"),
                              QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes);
Victor Yacovlev's avatar
Victor Yacovlev committed
769
            if (ans == QMessageBox::No)
Denis Khachko's avatar
Denis Khachko committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

            {
                event->accept();
                if(course)delete course;
                course=NULL;
                return;
             };
            if(ans == QMessageBox::Cancel){event->ignore();
                return;};

            saveCourse();
  }
        markProgChange();
        qDebug()<<"CLOSE TASK WINDOW";
        close();

};
void MainWindowTask::returnTested()
{
   interface->setPreProgram(QVariant(course->getUserTestedText(curTaskIdx.internalId())));
};
QString MainWindowTask::loadTestAlg(QString file_name)
{
    if(file_name.isEmpty())return "";
    QFile file(curDir+"/"+file_name);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {

        QMessageBox::about(NULL, trUtf8("Ошибка"),trUtf8("Невозможно открыть ")+curDir+"/"+file_name);
        return "";
    };
    QTextStream ts(&file);
    ts.setCodec("UTF-16LE");
    QString testalg= ts.readAll();
    file.close();

    qDebug()<<"Test alg"<<testalg;
    return testalg;
}
void MainWindowTask::aboutToQuit()
{
  close();
};
void MainWindowTask:: customContextMenuRequested(QPoint  pos)
{
editRoot->hide();
    if(!isTeacher)return;

    curTaskIdx=ui->treeView->currentIndex();
    if(curTaskIdx.internalId()==0)
    {setEditTaskEnabled(false);
        ui->actionEdit->setEnabled(true);
        ui->addDeep->setEnabled(true);
    }else setEditTaskEnabled(true);
        if(!baseKursFile.isReadable())
    {
            return;
        };
    pos.setY(pos.y()+ui->mainToolBar->height());
    customMenu.move(pos+this->pos()+ui->centralWidget->pos());
    customMenu.show();
  qDebug()<<"Menu Request!!!";
};
void MainWindowTask::addTask()
{
    qDebug()<<"Add task";
       QModelIndex par=curTaskIdx.parent();
course->addSiblingTask(curTaskIdx.internalId());
ui->treeView->collapse(par);
ui->treeView->expand(par);
};
void MainWindowTask::addDeepTask(){
    qDebug()<<"Add deep task";
       QModelIndex par=curTaskIdx.parent();
course->addDeepTask(curTaskIdx.internalId());
ui->treeView->collapse(par);
ui->treeView->expand(par);
saveBaseKurs();
if(curTaskIdx.internalId()==0)
 {
      loadCourseData(baseKursFile.absoluteFilePath());
 }
};
void MainWindowTask::deleteTask()
{
    QModelIndex par=curTaskIdx.parent();
course->removeNode(curTaskIdx.internalId());
ui->treeView->collapse(par);
ui->treeView->expand(par);
};

void MainWindowTask::saveKurs()
{
    qDebug()<<"Save Kurs teacher";
    QFile cursKursFile("../../test.kurs.xml");
    if  (!cursKursFile.open(QIODevice::WriteOnly))
    {
    QMessageBox::information( 0, "", trUtf8("Ошибка записи: ") + cursKursFile.fileName(), 0,0,0);
    return;
    };

    cursKursFile.write(course->document()->toByteArray());
    cursKursFile.close();
    ui->actionEdit->setEnabled(true);
};
void MainWindowTask::saveBaseKurs()
{
    QFile cursKursFile(baseKursFile.absoluteFilePath());
    if  (!cursKursFile.open(QIODevice::WriteOnly))
    {
    QMessageBox::information( 0, "", trUtf8("Ошибка записи: ") + cursKursFile.fileName(), 0,0,0);
    return;
    };

    cursKursFile.write(course->document()->toByteArray());
    cursKursFile.close();
}

void MainWindowTask::saveKursAs()
{
    QString dir=curDir;
    QDir chD(curDir);
    if(!chD.exists())dir=QDir::homePath ();
     QFileDialog dialog(this,trUtf8("Сохранить файл курса"),dir, "(*.kurs.xml )");
     dialog.setAcceptMode(QFileDialog::AcceptSave);

     if(!dialog.exec())return;
     QFileInfo fi(dialog.selectedFiles().first());
     baseKursFile=fi;






     saveBaseKurs();

;
};
void MainWindowTask:: setTeacher(bool mode)
910
{
Denis Khachko's avatar
Denis Khachko committed
911 912 913 914 915
    ui->actionup->setVisible(false);
     ui->actionDown->setVisible(false);
    isTeacher=mode;};
void MainWindowTask::editTask()
{
Denis Khachko's avatar
Denis Khachko committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
//if(curTaskIdx.internalId()==0)//ROOT
//    {
//
//    QRect rect=ui->treeView->visualRect(curTaskIdx);
//    editRoot->resize(rect.width(),rect.height());
//     editRoot->setText(course->rootText());
//    editRoot->move(rect.topLeft());
//    editRoot->show();
//    return;
//     };
//    QModelIndex par=curTaskIdx.parent();
//
//editDialog->setTitle(course->getTitle(curTaskIdx.internalId()));
//editDialog->setDesc(course->getTaskText(curTaskIdx));
//editDialog->setProgram(course->progFile(curTaskIdx.internalId()));
//QStringList isps=course->Modules(curTaskIdx.internalId());
//
//if(isps.count()>0)editDialog->setUseIsps(isps.first());
//   else editDialog->setUseIsps("");
//
//if(course->Modules(curTaskIdx.internalId()).count()>0)editDialog->setEnvs(course->Fields(curTaskIdx.internalId(),isps.first()));
// else editDialog->setEnvs(QStringList());
//editDialog->setCurDir(curDir);
// if(editDialog->exec ())
// {
//  course->setUserText(curTaskIdx.internalId(),"");
//  course->setTitle(curTaskIdx.internalId(),editDialog->getTitle());
//  course->setDesc(curTaskIdx.internalId(),editDialog->getDesc());
//  qDebug()<<"Desc"<<editDialog->getDesc();
//  course->setProgram(curTaskIdx.internalId(),editDialog->getProgram());
//  qDebug()<<"EDIT ISPS"<<editDialog->getUseIsps();
//
//
//  course->setIsps(curTaskIdx,editDialog->getUseIsps());
//  qDebug()<<"PRG"<<editDialog->getProgram();
//  if(course->Modules(curTaskIdx.internalId()).count()>0)course->setIspEnvs(curTaskIdx,course->Modules(curTaskIdx.internalId()).first(),editDialog->getEnvs());
// showText(curTaskIdx);
//  ui->treeView->collapse(par);
//  ui->treeView->expand(par);
//  qDebug()<<"Set task isps:"<<course->Modules(curTaskIdx.internalId());
//  qDebug()<<"EDIT DIALOG EXEC OK";
//  saveBaseKurs();
//  resetTask();
// };
//
Denis Khachko's avatar
Denis Khachko committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
};

void MainWindowTask::setEditTaskEnabled(bool flag)
{
   // ui->actionEdit->setEnabled(flag);
    ui->actionup->setEnabled(flag);
    ui->actionDown->setEnabled(flag);

    ui->actionSaveKas->setEnabled(flag);
    ui->actionAdd->setEnabled(flag);
    ui->actionRemove->setEnabled(flag);
    ui->addDeep->setEnabled(flag);
};

void MainWindowTask::newKurs()
{

Denis Khachko's avatar
Denis Khachko committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
// if(newDialog->exec())
// {
//    QFile newKurs(newDialog->fileName());
//
//    if  (!newKurs.open(QIODevice::WriteOnly))
//    {
//    QMessageBox::information( 0, "", trUtf8("Ошибка записи: ") + newKurs.fileName(), 0,0,0);
//    return;
//    };
//    QString toWr="<?xml version='1.0' encoding='UTF-8'?>\n";
//    newKurs.write(toWr.toUtf8());
//    toWr="<KURS xml:id=\"0\" xml:name=\""+newDialog->name()+"\">\n";
//    newKurs.write(toWr.toUtf8());
//
//    toWr=QString::fromUtf8("<T xml:id=\"1\" xml:name=\"Новое задание\">\n<DESC>Нет Описания</DESC>\n<CS>Кумир</CS>\n <ISP xml:ispname=\"Robot\">\n</ISP>\n<READY>false</READY>\n</T>\n");
//    newKurs.write(toWr.toUtf8());
//
//    toWr="</KURS>\n";
//    newKurs.write(toWr.toUtf8());
//    newKurs.close();
//    loadCourseData(newKurs.fileName());
//    baseKursFile=QFileInfo(newKurs);
//    curDir=baseKursFile.absolutePath();
//    ui->actionEdit->setEnabled(true);
// }
Denis Khachko's avatar
Denis Khachko committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

};
  void  MainWindowTask::createMoveMenu()
{

  }
void MainWindowTask::endRootEdit()
{
    course->setRootText(editRoot->text());
   editRoot->hide();
}
 void MainWindowTask::cancelRootEdit()
 {  editRoot->hide();
 };
void MainWindowTask::lockCheck()
{
    ui->checkTask->setDisabled(true);
}