Advertisement
Guest User

Слоты и доп функции

a guest
Dec 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void NumismatistHandbook::openFileSlot()
  2. {
  3.     //сохранение текущего файла, если нужно
  4.     if (fileChanged())
  5.     {
  6.         if (askToSafe(/*includeCancelButton = */false) == SafeAccepted)
  7.             saveFileSlot();
  8.     }
  9.  
  10.     //открываем новый файл
  11.     QString newFilename = QFileDialog::getOpenFileName(this, "Открыть файл", "/", "Файлы монет (*.coins)");
  12.     if (!newFilename.isEmpty())
  13.     {
  14.         currentFilename = newFilename;
  15.  
  16.         model.load(currentFilename);
  17.         updateForm();
  18.         ui->currentFileLabel->setText(currentFilename);
  19.     }
  20. }
  21.  
  22. void NumismatistHandbook::createFileSlot()
  23. {
  24.     if (fileChanged())
  25.     {
  26.         if (askToSafe(/*includeCancelButton = */false) == SafeAccepted)
  27.             saveFileSlot();
  28.     }
  29.  
  30.     currentFilename = "";
  31.     model.clear();
  32.     updateForm();
  33.     ui->currentFileLabel->setText(currentFilename);
  34. }
  35.  
  36. void NumismatistHandbook::saveFileSlot()
  37. {
  38.     if (!currentFilename.isEmpty())
  39.         model.save(currentFilename);
  40.     else
  41.         saveAsFileSlot();
  42.  
  43.     ui->currentFileLabel->setText(currentFilename);
  44. }
  45.  
  46. void NumismatistHandbook::saveAsFileSlot()
  47. {
  48.     QString newFilename = QFileDialog::getSaveFileName(this, "Сохранить как", "/", "Файлы монет (*.coins)");
  49.  
  50.     if (!newFilename.isEmpty())
  51.     {
  52.         currentFilename = newFilename;
  53.         model.save(currentFilename);
  54.     }
  55.     ui->currentFileLabel->setText(currentFilename);
  56. }
  57.  
  58. bool NumismatistHandbook::fileChanged()
  59. {
  60.     //файл был изменен и не является только что созданным не сохраннёным файлом без записей
  61.     return model.isModified() && not(currentFilename.isEmpty() && model.count() == 0);
  62. }
  63.  
  64. NumismatistHandbook::SafeState NumismatistHandbook::askToSafe(bool includeCancelButton)
  65. {
  66.     //NumismatistHandbook::SafeState это синоним типа QMessageBox::StandartButton
  67.     QMessageBox::StandardButtons buttons = QMessageBox::Yes | QMessageBox::No;
  68.     if (includeCancelButton)
  69.         buttons |= QMessageBox::Cancel;
  70.  
  71.     QMessageBox::StandardButton selectedButton =
  72.             QMessageBox::question(this, "База данных", "Сохранить изменения в текущем файле?", buttons);
  73.  
  74.     return selectedButton;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement