#include "CsvImportDialog.h" #include "../main-view/DictTableView.h" #include "../main-view/DictTableModel.h" #include "../dictionary/Dictionary.h" #include "../dictionary/DicCsvReader.h" CsvImportDialog::CsvImportDialog(QWidget* parent, QString filePath, const AppModel* appModel): CsvDialog(parent), filePath(filePath), dictionary(NULL), appModel(appModel) { init(); setWindowTitle(tr("Import from CSV")); connect( textDelimiterCB, SIGNAL(stateChanged(int)), this, SLOT(updateTextDelimiterCombo()) ); connect( commentCharCB, SIGNAL(stateChanged(int)), this, SLOT(UpdateCommentCharacterCombo()) ); connect( this, SIGNAL(rejected()), this, SLOT(DeleteDictionary()) ); connect( charSetCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePreview()) ); connect( fromLineSpin, SIGNAL(valueChanged(int)), this, SLOT(updatePreview()) ); connect( firstLineIsHeaderCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) ); connect( anyCharacterRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) ); connect( anyCombinationRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) ); connect( exactStringRB, SIGNAL(toggled(bool)), this, SLOT(updatePreview()) ); connect( commentCharCB, SIGNAL(stateChanged(int)), this, SLOT(updatePreview()) ); connect( colsToImportSpin, SIGNAL(valueChanged(int)), this, SLOT(updatePreview()) ); } CsvImportDialog::~CsvImportDialog() { delete iPreviewModel; delete dicReader; } QLayout* CsvImportDialog::createLeftGroupLayout() { QFormLayout* lt = new QFormLayout; lt->addRow(tr("C&haracter set:"), createCharSetCombo()); lt->addRow(tr("From &line:"), createFromLineSpin()); lt->addRow(tr("Number of colum&ns:"), createColsToImportSpin()); lt->addRow(createFirstLineIsHeaderCB()); return lt; } QWidget* CsvImportDialog::createFromLineSpin() { fromLineSpin = new QSpinBox(); fromLineSpin->setRange(1, 100); return fromLineSpin; } QWidget* CsvImportDialog::createColsToImportSpin() { colsToImportSpin = new QSpinBox(); colsToImportSpin->setRange(0, 9); colsToImportSpin->setSpecialValueText(tr("All")); return colsToImportSpin; } QWidget* CsvImportDialog::createFirstLineIsHeaderCB() { firstLineIsHeaderCB = new QCheckBox(tr("&First line has field names")); firstLineIsHeaderCB->setChecked(false); return firstLineIsHeaderCB; } QLayout* CsvImportDialog::createSeparatorsLayout() { createSeparationRadioButtons(); QFormLayout* lt = new QFormLayout; lt->addRow(tr("Field &separator:"), createFieldSeparatorWidget()); lt->addRow(tr("Separation mode:"), anyCharacterRB); // The label is a workaround to make enough space for the radio button text lt->addRow(" ", anyCombinationRB); lt->addRow(NULL, exactStringRB); lt->addRow(createTextDelimiterBox(), createTextDelimiterCombo()); lt->addRow(createCommentCharBox(), createCommentCharCombo()); return lt; } void CsvImportDialog::createSeparationRadioButtons() { anyCharacterRB = new QRadioButton(tr("An&y character")); anyCharacterRB->setToolTip(tr("Fields are separated by any separator character")); anyCombinationRB = new QRadioButton(tr("A co&mbination of characters")); anyCombinationRB->setToolTip(tr("Fields are separated by a combination of separator characters, in any order")); anyCombinationRB->setChecked( true ); exactStringRB = new QRadioButton(tr("E&xact string")); exactStringRB->setToolTip(tr("Fields are separated by the exact string of separators, in the above defined order")); } QWidget* CsvImportDialog::createCommentCharBox() { commentCharCB = new QCheckBox(tr("&Comment character:")); commentCharCB->setChecked(true); return commentCharCB; } QLayout* CsvImportDialog::createPreviewLt() { QVBoxLayout* lt = new QVBoxLayout; lt->addWidget(createPreview()); return lt; } QWidget* CsvImportDialog::createPreview() { dictionary = new Dictionary( filePath + Dictionary::DictFileExtension, true, appModel ); dicReader = new DicCsvReader( dictionary ); iPreviewModel = new DictTableModel( dictionary ); previewTable = new DictTableView(iPreviewModel); return previewTable; } void CsvImportDialog::UpdateCommentCharacterCombo() { commentCharCombo->setEnabled( commentCharCB->isChecked() ); } void CsvImportDialog::updatePreview() { CsvImportData params; params.textCodec = getTextCodec(); params.fromLine = fromLineSpin->value(); params.fieldSeparators = setCharVisibility(separatorsEdit->text(), false); params.fieldSeparationMode = getSeparationMode(); params.commentChar = commentCharCB->isChecked()? commentCharCombo->currentText()[0]: QChar(0); params.textDelimiter = getTextDelimiterChar(); params.colsToImport = colsToImportSpin->value(); params.firstLineIsHeader = firstLineIsHeaderCB->isChecked(); dicReader->readDict( filePath, params ); iPreviewModel->resetData(); } FieldSeparationMode CsvImportDialog::getSeparationMode() { if(anyCharacterRB->isChecked()) return EFieldSeparatorAnyCharacter; else if (anyCombinationRB->isChecked()) return EFieldSeparatorAnyCombination; else return EFieldSeparatorExactString; } void CsvImportDialog::DeleteDictionary() { delete dictionary; dictionary = NULL; }