mainwindow.cpp
来自「Qt开发的GRE背单词软件」· C++ 代码 · 共 737 行 · 第 1/2 页
CPP
737 行
}
void MainWindow::gotoNext() {
selectRow(currentRow+1);
}
void MainWindow::gotoPrev() {
selectRow(currentRow-1);
}
void MainWindow::slotRight()
{
setCredit(model->getCredit(currentRow) + 1);
if(!ui.buttonAutoPlay->isChecked())
gotoNext();
}
void MainWindow::slotWrong()
{
setCredit(model->getCredit(currentRow) - 1);
if(!ui.buttonAutoPlay->isChecked())
gotoNext();
}
void MainWindow::slotShuffle()
{
submitAll(); // submit appending changes
model->shuffle();
}
void MainWindow::slotUpdateFilter()
{
submitAll();
model->updateFilter(exportDictSetting());
model->select();
updateProgress();
slotGotoCurrentID();
}
void MainWindow::slotSetMaxThreshold1(int value) {
ui.spinBoxThreshold1->setMaximum(value-1);
}
void MainWindow::slotUpdateTitle() {
setWindowTitle(tr("MyWords - %1 - %2 - Unit %3").arg(userName)
.arg(getCurrentDictName())
.arg(getUnit()));
}
void MainWindow::slotPrevUnit() {
setUnit(getUnit() - 1);
}
void MainWindow::slotNextUnit() {
setUnit(getUnit() + 1);
}
void MainWindow::slotAbout() {
QMessageBox::about(this, tr("About"), tr("<h3><b>MyWords - 我的红宝解决方案</b></h3>"
"<p>Build 2008.7.25</p>"
"<p>本软件基于GPL协议</p>"
"<p>chencongsytu@163.com</p>"));
}
void MainWindow::slotSetting()
{
SettingDlg dlg(userName, this);
dlg.importSetting(*setting);
if(dlg.exec() == QDialog::Accepted)
{
importSetting(dlg.exportSetting());
updateProgress(); // may change color
}
}
void MainWindow::slotPlay(bool play) {
play ? timer.start(getPlayDelay()) : timer.stop();
}
void MainWindow::slotTimeout()
{
static bool showChinese = true;
if(showChinese)
slotForceShowChinese();
else
{
if(currentRow == model->rowCount() - 1) // stop at last row
ui.buttonAutoPlay->toggle();
else
gotoNext();
}
showChinese = !showChinese;
}
void MainWindow::speak()
{
const QString word = getCurrentEnglish();
const QString fileName = wavDir + "/" + word.left(1) + "/" + word + ".wma";
if(QFile::exists(fileName))
QSound::play(fileName);
}
void MainWindow::slotJump(int id, bool back)
{
if(id < 1) return;
if(back)
{
enableJumpBack(true);
jumpHistroy.push(currentID);
}
setUnit((id-1)/getUnitSize() + 1); // jump to page
QModelIndexList indexes = model->match(
model->index(0, 0), Qt::DisplayRole, id, 1, Qt::MatchExactly | Qt::MatchWrap);
if(indexes.isEmpty())
{
ui.checkBoxNew ->setChecked(true); // show all words
ui.checkBoxUnfamiliar->setChecked(true);
ui.checkBoxFamiliar ->setChecked(true);
}
jumpInPage(id);
}
void MainWindow::slotBack()
{
if(!jumpHistroy.empty())
{
slotJump(jumpHistroy.top(), false);
jumpHistroy.pop();
}
else
enableJumpBack(false);
}
void MainWindow::jumpInPage(int id)
{
QModelIndexList indexes = model->match(
model->index(0, 0), Qt::DisplayRole, id, 1, Qt::MatchExactly | Qt::MatchWrap);
if(!indexes.isEmpty())
{
currentRow = indexes.at(0).row();
selectRow(currentRow);
}
}
void MainWindow::slotGotoCurrentID() {
jumpInPage(currentID); // keep highlighting, can't use currentRow
}
void MainWindow::slotSectionPressed() {
submitAll();
}
void MainWindow::slotDelWord()
{
if(QMessageBox::warning(this, "Warning", tr("真的要删除么?"),
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
model->removeRow(currentRow);
submitAll();
selectRow(currentRow);
}
}
void MainWindow::slotChangeUser()
{
setting->setValue("Other/isDefault", false);
Entrance* entrance = new Entrance;
entrance->show();
close();
}
int MainWindow::getUnit() const {
return ui.spinBoxUnit->value();
}
int MainWindow::getUnitSize() const {
return ui.spinBoxUnitSize->value();
}
void MainWindow::setUnit(int unit)
{
ui.spinBoxUnit->setValue(unit);
slotUpdateTitle();
}
void MainWindow::setUnitSize(int size) {
ui.spinBoxUnitSize->setValue(size);
}
void MainWindow::slotExport()
{
QDir::current().mkdir("Export");
QString destDir = tr("Export/%1-%2-Unit%3").arg(userName).arg(getCurrentDictName()).arg(getUnit());
QDir::current().mkdir(destDir);
QFile file(destDir + ".txt");
if(!file.open(QFile::WriteOnly | QFile::Truncate))
{
QMessageBox::critical(this, tr("打开文件错误"), tr("无法创建文件"));
return;
}
QTextStream ofs(&file);
ofs << destDir << "\r\n\r\n";
ui.buttonRecite->setChecked(false); // learning mode
while(model->canFetchMore())
model->fetchMore();
int count = model->rowCount();
QProgressDialog progress(tr("一共%1个单词,正在导出...").arg(count),
0, 0, count);
progress.setWindowTitle(tr("单词导出中"));
progress.setWindowModality(Qt::WindowModal);
for(int row=0; row<count; ++row)
{
const QString word = model->getEnglish(row);
const QString chinese = model->getChinese(row);
Library::exportMp3(destDir, word);
ofs << row + 1 << " " << word << " " << chinese << "\r\n";
progress.setValue(row);
qApp->processEvents();
}
}
void MainWindow::importSetting(const UserSetting& setting)
{
UserSetting* globalSetting = UserSetting::getInstance("Global");
wavDir = globalSetting->value("WavDir").toString();
mp3Dir = globalSetting->value("Mp3Dir").toString();
QApplication::setFont (setting.getFont("guiFont"));
ui.tableView ->setFont (setting.getFont("tableFont"));
ui.detailWidget->setEnglishFont (setting.getFont("wordFont"));
ui.detailWidget->setPhoneticFont (setting.getFont("phoneticFont"));
ui.detailWidget->setChineseFont (setting.getFont("chineseFont"));
ui.detailWidget->setNoteFont (setting.getFont("noteFont"));
model->setNewWordColor (setting.getColor("newWordColor"));
model->setUnfamiliarColor (setting.getColor("unFamiliarColor"));
model->setFamiliarColor (setting.getColor("familiarColor"));
ui.detailWidget->setPhoneticColor(setting.getColor("phoneticColor"));
ui.buttonRight ->setShortcut(setting.getShortcut("rightShortcut"));
ui.buttonWrong ->setShortcut(setting.getShortcut("wrongShortcut"));
ui.buttonChinese ->setShortcut(setting.getShortcut("chineseShortcut"));
ui.buttonSetFamiliar->setShortcut(setting.getShortcut("setFamiliarShortcut"));
}
void MainWindow::delOldBackups()
{
const QDate today = QDate::currentDate();
const int days = setting->value("Other/backupDays").toInt();
const QFileInfoList fileInfos =
QDir("Backup").entryInfoList(QStringList() << "*.db", QDir::Files);
foreach(QFileInfo fileInfo, fileInfos)
if(QDate::fromString(fileInfo.baseName(), Qt::ISODate).daysTo(today) > days)
QFile::remove(fileInfo.filePath());
}
void MainWindow::backup(const QString& fileName)
{
QDir::current().mkdir("Backup");
QString backupFileName = fileName.isEmpty()
? "./Backup/" + QDate::currentDate().toString(Qt::ISODate) + ".db"
: fileName;
if(QFile::exists(backupFileName))
QFile::remove(backupFileName);
QFile file(Library::getDBFileName());
file.copy(backupFileName);
file.close();
}
int MainWindow::getThreshold1() const {
return ui.spinBoxThreshold1->value();
}
int MainWindow::getThreshold2() const {
return ui.spinBoxThreshold2->value();
}
int MainWindow::getPlayDelay() const{
return ui.spinBoxDelay->value() * 1000;
}
void MainWindow::submitAll() {
model->submitAll();
}
void MainWindow::slotFullScreen()
{
showFullScreen();
ui.menuBar->hide();
}
void MainWindow::slotExitFullScreen()
{
showMaximized();
ui.menuBar->show();
}
void MainWindow::slotHideControlBar(bool hide)
{
ui.frameControl->setMaximumSize(QSize(1024, hide ? 0 : 1024));
ui.actionHideControlBar->setChecked(hide);
}
void MainWindow::slotBackup()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("备份数据库"),
"./Backup/word.db", tr("Database file (*.db);;All file (*.*)"));
if(!fileName.isEmpty())
backup(fileName);
}
void MainWindow::slotUpper(bool upper) {
model->setUpper(upper);
}
void MainWindow::enableJumpBack(bool enable) {
ui.buttonBack->setEnabled(enable);
}
void MainWindow::loadDictSetting()
{
if(setting->containsDictSetting(getCurrentDictName()))
{
DictSetting dictSetting = setting->getDictSetting(getCurrentDictName());
slotResizeUnit(dictSetting.unitSize);
setUnitSize (dictSetting.unitSize);
setUnit (dictSetting.currentUnit);
ui.spinBoxThreshold1->setValue(dictSetting.threshold1);
ui.spinBoxThreshold2->setValue(dictSetting.threshold2);
ui.checkBoxNew ->setChecked(dictSetting.showNew);
ui.checkBoxUnfamiliar->setChecked(dictSetting.showUnFamiliar);
ui.checkBoxFamiliar ->setChecked(dictSetting.showFamiliar);
slotSetMaxThreshold1(getThreshold2());
}
slotUpdateFilter();
}
void MainWindow::saveDictSetting(const QString& user) {
UserSetting::getInstance(user)->setDictSetting(lastDict, exportDictSetting());
}
DictSetting MainWindow::exportDictSetting() const
{
DictSetting result;
result.unitSize = getUnitSize();
result.currentUnit = getUnit();
result.threshold1 = getThreshold1();
result.threshold2 = getThreshold2();
result.showNew = ui.checkBoxNew->isChecked();
result.showUnFamiliar = ui.checkBoxUnfamiliar->isChecked();
result.showFamiliar = ui.checkBoxFamiliar->isChecked();
return result;
}
void MainWindow::slotSetFamiliar()
{
setCredit(getThreshold2());
if(!ui.buttonAutoPlay->isChecked())
gotoNext();
}
void MainWindow::slotMiniPlayer()
{
if(miniPlayer == 0)
{
miniPlayer = new MiniPlayer(model, this);
connect(miniPlayer, SIGNAL(accepted()), this, SLOT(showMaximized()));
}
miniPlayer->show();
hide();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?