void NewsPaper::debugShortcuts(QMenu *shortcuts) { QMenu *debug_menu = shortcuts->addMenu("Spell Check"); QAction *act1 = debug_menu->addAction(IconManager::instance().loadIcon("16/document-print.png"), "Enable Spell Check"); act1->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M, Qt::CTRL+Qt::Key_X)); connect(act1, SIGNAL(triggered()), this, SLOT(actionDebug1())); QAction *act2 = debug_menu->addAction(IconManager::instance().loadIcon("16/document-print.png"), "Disable Spell Check"); act2->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M, Qt::CTRL+Qt::Key_Y)); connect(act2, SIGNAL(triggered()), this, SLOT(actionDebug2())); QAction *act3 = debug_menu->addAction(IconManager::instance().loadIcon("16/document-print.png"), "Upload Malayalam Words"); act3->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M, Qt::CTRL+Qt::Key_Z)); connect(act3, SIGNAL(triggered()), this, SLOT(actionDebug3())); QAction *act4 = debug_menu->addAction(IconManager::instance().loadIcon("16/document-print.png"), "Spell Check All"); act4->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M, Qt::CTRL+Qt::Key_S)); connect(act4, SIGNAL(triggered()), this, SLOT(actionDebug4())); } void NewsPaper::actionDebug1() { if(!checkDoc()) return; itemSpellCheck(); } void NewsPaper::actionDebug2() { if(!checkDoc()) return; disableSpellCheck(); return; } void NewsPaper::actionDebug3() { if(!checkDoc()) return; updateDictionary(); return; } void NewsPaper::actionDebug4() { if(!checkDoc()) return; spellCheckAll(); } void NewsPaper::itemSpellCheck() { PageItem *item = selectedItem(); if (item == nullptr) return; instanceItemSpellCheck->item = item; QList story; double height = pageHeight(); double width = pageWidth(); double xmin = xOffset(); double ymin = yOffset(); double xmax = xmin + width; double ymax = ymin + height; if (item->isGroup()) { foreach(PageItem *child, item->getAllChildren()) { if (child->isTextFrame()) { if (child->xPos() < xmin) continue; if (child->yPos() < ymin) continue; if ((child->xPos() + child->width()) > xmax) continue; if ((child->yPos() + child->height()) > ymax) continue; QString data = child->itemText.plainText(); data = data.replace("\r","\n"); story.append(data+QString("\n")); } } } else if (item->isTextFrame()) { if (item->xPos() < xmin) return; if (item->yPos() < ymin) return; if ((item->xPos() + item->width()) > xmax) return; if ((item->yPos() + item->height()) > ymax) return; QString data = item->itemText.plainText(); data = data.replace("\r","\n"); story.append(data+QString("\n")); } else { return; } QByteArray postData; QString sstory = story.join(QString(" ")); postData.append(QString("udata=")+sstory); instanceItemSpellCheck->sendData(&postData); return; } void NewsPaper::spellCheckAll() { if(!checkDoc()) return; disableSpellCheck(); QList story; int N = ScCore->primaryMainWindow()->doc->Items->count(); double height = pageHeight(); double width = pageWidth(); double xmin = xOffset(); double ymin = yOffset(); double xmax = xmin + width; double ymax = ymin + height; for (int j = 0; j < N; j++) { PageItem *item = ScCore->primaryMainWindow()->doc->Items->at(j); if (item->isGroup()) { foreach(PageItem *child, item->getAllChildren()) { if (child->isTextFrame()) { if (child->xPos() < xmin) continue; if (child->yPos() < ymin) continue; if ((child->xPos() + child->width()) > xmax) continue; if ((child->yPos() + child->height()) > ymax) continue; QString data = child->itemText.plainText(); data = data.replace("\r","\n"); story.append(data+QString("\n")); } } } if (item->isTextFrame()) { if (item->xPos() < xmin) continue; if (item->yPos() < ymin) continue; if ((item->xPos() + item->width()) > xmax) continue; if ((item->yPos() + item->height()) > ymax) continue; QString data = item->itemText.plainText(); data = data.replace("\r","\n"); story.append(data+QString("\n")); } } QString sstory = story.join(QString(" ")); QByteArray postData; postData.append(QString("udata=")+sstory); spell->sendData(&postData); return; } void NewsPaper::textCursor(int pos) { qDebug() << pos; } SpellCheck::SpellCheck(QObject *parent): QObject(parent) { manager = new QNetworkAccessManager(this); QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(receiveData(QNetworkReply *))); } void SpellCheck::sendData(QByteArray *postData) { QUrl url("http://10.1.2.29:9800/erp5/spellcheck_service"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); manager->post(request, *postData); } void SpellCheck::receiveData(QNetworkReply *rep) { QByteArray bts = rep->readAll(); QString str(bts); QJsonDocument jsonResponse = QJsonDocument::fromJson(str.toUtf8()); QJsonArray data = jsonResponse.array(); emit dataReceived(data); // SpellCheck ==> NewsPaper::drawSpellCheck } UploadWords::UploadWords(QObject *parent): QObject(parent) { manager = new QNetworkAccessManager(this); QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(receiveData(QNetworkReply *))); } void UploadWords::sendData(QByteArray *postData) { QUrl url("http://10.1.2.29:9800/erp5/save_public_words"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); manager->post(request, *postData); } void UploadWords::receiveData(QNetworkReply *rep) { QByteArray bts = rep->readAll(); QString str(bts); QJsonDocument jsonResponse = QJsonDocument::fromJson(str.toUtf8()); QJsonObject data = jsonResponse.object(); QJsonValue value = data.value(QString("message")); message = value.toString(); emit statusReceived(); // UploadWords ==> NewsPaper::drawUploadWordsStatus } void NewsPaper::drawUploadWordsStatus() { QString message = instanceUploadWords->message; upload_status_message->setText(message); } void NewsPaper::drawSpellCheck(QJsonArray data) { int no = data.count(); for(int i=0; i < no; ++i) { QJsonValue row_val = data.at(i); QJsonArray row = row_val.toArray(); int mo = row.count(); for(int j=0; j < mo; ++j) { QJsonValue col_val = row.at(j); QJsonObject col_dict = col_val.toObject(); QJsonArray vals = col_dict.value(QString('i')).toArray(); int lo = vals.count(); for(int k=0; k < lo; ++k) { QJsonArray word = vals.at(k).toArray(); int status = word.at(0).toInt(); QString text = word.at(1).toString(); malayalamWords[text] = status; } } } int N = ScCore->primaryMainWindow()->doc->Items->count(); double height = pageHeight(); double width = pageWidth(); double xmin = xOffset(); double ymin = yOffset(); double xmax = xmin + width; double ymax = ymin + height; QList itemList; for (int j = 0; j < N; j++) { PageItem *item = ScCore->primaryMainWindow()->doc->Items->at(j); if (item->xPos() < xmin) continue; if (item->yPos() < ymin) continue; if ((item->xPos() + item->width()) > xmax) continue; if ((item->yPos() + item->height()) > ymax) continue; itemList.append(item); } N = itemList.count(); for (int j = 0; j < N; j++) { PageItem *item = itemList.at(j); if (item->isGroup()) { foreach(PageItem *child, item->getAllChildren()) { if (child->isTextFrame()) { drawSpellCheckItem(child); } } } else if (item->isTextFrame()) { drawSpellCheckItem(item); } } } void NewsPaper::myDocChanged() { alert("My Doc Changed"); } void NewsPaper::disableSpellCheckOld() { if(!checkDoc()) return; QList itemList; int N = ScCore->primaryMainWindow()->doc->Items->count(); for (int j = 0; j < N; j++) { PageItem *item = ScCore->primaryMainWindow()->doc->Items->at(j); if (item->isGroup()) { foreach(PageItem *child, item->getAllChildren()) { if (child->itemName().startsWith("ERR")) { itemList.append(child); } } } else if (item->itemName().startsWith("ERR")) { itemList.append(item); } } foreach(PageItem *item, itemList) { selectItem(item); ScCore->primaryMainWindow()->doc->itemSelection_DeleteItem(); } return; } PageItem *NewsPaper::drawRule3(double x1, double y1, double x2, double y2, double thickness) { if (!checkHaveDocument()) return nullptr; QString lineName("NONE"); PageItem *it = nullptr; if (y1 == y2) { double width = x2-x1; int i = ScCore->primaryMainWindow()->doc->itemAdd(PageItem::Line, PageItem::Unspecified, x1, y1, width, 0.0, ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth, QString("Black"), ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineColor); it = ScCore->primaryMainWindow()->doc->Items->at(i); it->Sizing = false; it->updateClip(); it->setRedrawBounding(); it->setLineWidth(thickness); lineName = it->itemName(); } else { double width = x2-x1; int i = ScCore->primaryMainWindow()->doc->itemAdd(PageItem::Line, PageItem::Unspecified, x1, y1, width, 0.0, ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth, QString("Black"), ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineColor); it = ScCore->primaryMainWindow()->doc->Items->at(i); it->Sizing = false; it->updateClip(); it->setRedrawBounding(); it->setLineWidth(thickness); lineName = it->itemName(); } if (lineName != QString("NONE")) { lineName = QString("ERR")+lineName; it->setItemName(lineName); sendBack(it); ScCore->primaryMainWindow()->toggleColumnBorders(); ScCore->primaryMainWindow()->toggleColumnBorders(); } return it; } void NewsPaper::updateDictionary() { if (!checkDoc()) return; int x = ScCore->primaryMainWindow()->centralWidget()->x(); int y = ScCore->primaryMainWindow()->centralWidget()->y(); int w = ScCore->primaryMainWindow()->centralWidget()->width(); int h = ScCore->primaryMainWindow()->centralWidget()->height(); if (windowDictionary == nullptr) { windowDictionary = new GWidget(); windowDictionary->setWindowModality(Qt::ApplicationModal); windowDictionary->setWindowTitle( "Upload Malayalam Words" ); ///////////////////////////////////// tempWidget = new QWidget; malayalamGrid = new QGridLayout(tempWidget); QScrollArea* scrollArea = new QScrollArea; scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn ); scrollArea->setWidget(tempWidget); scrollArea->setWidgetResizable(true); QVBoxLayout *vbox = new QVBoxLayout(); vbox->addWidget(scrollArea); windowDictionary->setLayout(vbox); QHBoxLayout *hbox1 = new QHBoxLayout(); QPushButton *select_all = new QPushButton("Select All"); QPushButton *unselect_all = new QPushButton("Unselect All"); QPushButton *toggle = new QPushButton("Toggle"); hbox1->addWidget(select_all); hbox1->addWidget(unselect_all); hbox1->addWidget(toggle); vbox->addLayout(hbox1); QHBoxLayout *hbox2 = new QHBoxLayout(); QPushButton *upload = new QPushButton("Upload Words"); QPushButton *cancel = new QPushButton("Close"); hbox2->addWidget(upload); hbox2->addWidget(cancel); vbox->addLayout(hbox2); QLabel *message = new QLabel("STATUS:"); vbox->addWidget(message); upload_status_message = message; connect(select_all, SIGNAL(clicked()), this, SLOT(selectMalayalamWords())); connect(unselect_all, SIGNAL(clicked()), this, SLOT(unselectMalayalamWords())); connect(toggle, SIGNAL(clicked()), this, SLOT(toggleMalayalamWords())); connect(upload, SIGNAL(clicked()), this, SLOT(uploadMalayalamWords())); connect(cancel, SIGNAL(clicked()), this, SLOT(closeMalayalamWords())); } QList list = malayalamWords.keys(); std::sort(list.begin(), list.end()); int no = 0; checkboxList.clear(); clearLayout(malayalamGrid); foreach(QString text, list) { if (text == QString("∙")) continue; int status = malayalamWords.value(text,100); if (status == 0) continue; if (status == 2) continue; if (status == 3) continue; if (status == 4) continue; int col = no % 3; int row = no / 3; QCheckBox *checkbox = new QCheckBox(text, nullptr); malayalamGrid->addWidget(checkbox,row, col); checkboxList.append(checkbox); no++; } windowDictionary->resize(w,h); windowDictionary->update(x,y,w,h); windowDictionary->show(); windowDictionary->raise(); // for MacOS windowDictionary->activateWindow(); // for Windows return; } void NewsPaper::selectMalayalamWords() { foreach(QCheckBox *child, checkboxList) { child->setCheckState(Qt::Checked); } } void NewsPaper::unselectMalayalamWords() { foreach(QCheckBox *child, checkboxList) { child->setCheckState(Qt::Unchecked); } } void NewsPaper::toggleMalayalamWords() { foreach(QCheckBox *child, checkboxList) { if (child->checkState() == Qt::Checked) { child->setCheckState(Qt::Unchecked); } else { child->setCheckState(Qt::Checked); } } } void NewsPaper::uploadMalayalamWords() { QStringList words; foreach(QCheckBox *child, checkboxList) { if (child->checkState() == Qt::Checked) { words << child->text(); } } words.removeDuplicates(); QString data = words.join(" "); QByteArray postData; postData.append(QString("WORDS=")+data); instanceUploadWords->sendData(&postData); return; } void NewsPaper::closeMalayalamWords() { windowDictionary->hide(); } ItemSpellCheck::ItemSpellCheck(QObject *parent): QObject(parent) { manager = new QNetworkAccessManager(this); QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(receiveData(QNetworkReply *))); } void ItemSpellCheck::sendData(QByteArray *postData) { QUrl url("http://10.1.2.29:9800/erp5/spellcheck_service"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); manager->post(request, *postData); } void ItemSpellCheck::receiveData(QNetworkReply *rep) { QByteArray bts = rep->readAll(); QString str(bts); QJsonDocument jsonResponse = QJsonDocument::fromJson(str.toUtf8()); QJsonArray data = jsonResponse.array(); emit dataReceived(data); // ItemSpellCheck ==> NewsPaper::drawSpellCheck2 } void NewsPaper::drawSpellCheck2(QJsonArray data) { int no = data.count(); for(int i=0; i < no; ++i) { QJsonValue row_val = data.at(i); QJsonArray row = row_val.toArray(); int mo = row.count(); for(int j=0; j < mo; ++j) { QJsonValue col_val = row.at(j); QJsonObject col_dict = col_val.toObject(); QJsonArray vals = col_dict.value(QString('i')).toArray(); int lo = vals.count(); for(int k=0; k < lo; ++k) { QJsonArray word = vals.at(k).toArray(); int status = word.at(0).toInt(); QString text = word.at(1).toString(); malayalamWords[text] = status; } } } PageItem *item = instanceItemSpellCheck->item; if (item==nullptr) return; drawSpellCheckItem(item); } void NewsPaper::drawSpellCheckItemOld(PageItem *item) { if(!checkDoc()) return; if (item == nullptr) return; int n = item->itemText.length(); float X = item->xPos(); float Y = item->yPos(); for(int i=0; i < n; ) { int pos = item->itemText.nextWord(i); QString word = item->itemText.text(i, pos-i).trimmed(); int length = word.length(); if (length == 0) { i = pos; continue; } if (word == QString("∙")) { i = pos; continue; } int status = malayalamWords.value(word,100); if (status == 0) { i = pos; continue; } if (status == 2) { i = pos; continue; } if (status == 3) { i = pos; continue; } if (status == 4) { i = pos; continue; } QLineF loc1 = item->textLayout.positionToPoint(i); QLineF loc2 = item->textLayout.positionToPoint(i+length); if (loc2.y2() > loc1.y2()) { double maxx = loc1.x1(); double minx = maxx; for (int k = i; k < i+length; ++k) { QLineF loc3 = item->textLayout.positionToPoint(k); double x = loc3.x2(); if (x > maxx) maxx = x; if (x < minx) minx = x; } drawRule3(X + loc1.x1(), Y + loc1.y2(), X + maxx, Y + loc1.y2(), 2.0); drawRule3(X + minx, Y + loc2.y2(), X + loc2.x2(), Y + loc2.y2(), 2.0); } else if (loc2.y1() < loc1.y1()) { double x = loc1.x1(); double y = loc1.y1(); double xx = x; QLineF loc3; for (int k = i; k < i+length; ++k) { loc3 = item->textLayout.positionToPoint(k); if (loc3.y1() < y) { xx = loc3.x1(); break; } x = loc3.x2(); } drawRule3(X + loc1.x1(), Y + loc1.y2(), X + x, Y + loc1.y2(), 2.0); drawRule3(X + xx, Y + loc2.y2(), X + loc2.x2(), Y + loc2.y2(), 2.0); } else if ((loc1.x1() == loc2.x1()) && (loc1.y2() == loc2.y2())); else { drawRule3(X + loc1.x1(), Y + loc1.y2(), X + loc2.x2(), Y + loc2.y2(), 2.0); } i = pos; } return; } QString NewsPaper::toMalayalam(QString word) { QList array; for(int i = 0; i < word.size(); i++) { int val = word.at(i).unicode(); if (val < 0x0d00) continue; if (val > 0x0d7F) continue; array.append(word.at(i)); } return array.join("").trimmed(); } void NewsPaper::drawSpellCheckItem(PageItem *item) { if(!checkDoc()) return; if (item == nullptr) return; int n = item->itemText.length(); selectItem(item); item->itemText.setCursorPosition(0); item->update(); ScribusDoc* doc = ScCore->primaryMainWindow()->doc; ScribusView *view = ScCore->primaryMainWindow()->view; view->requestMode(modeEdit); for(int i=0; i < n; ) { int pos = item->itemText.nextWord(i); QString word = item->itemText.text(i, pos-i).trimmed(); word = toMalayalam(word); int length = word.length(); if (length == 0) { i = pos; continue; } if (word == QString("∙")) { i = pos; continue; } if (word == QString(",")) { i = pos; continue; } int status = malayalamWords.value(word,100); if (status == 0) { i = pos; continue; } if (status == 2) { i = pos; continue; } if (status == 3) { i = pos; continue; } if (status == 4) { i = pos; continue; } ////////////////////////////////////////////////////////////// int mid = i + (length/2); const CharStyle& style = item->itemText.charStyle(mid); int flag = style.effects(); int myFlag = 16; //----------------------------------------- int oldAppMode = doc->appMode; if (item->HasSel) doc->appMode = modeEdit; item->itemText.deselectAll(); item->itemText.select(i, length); Selection tmpSelection(nullptr, false); tmpSelection.addItem(item); item->itemText.selectWord(i); item->itemText.setCursorPosition(i); doc->itemSelection_SetEffects(flag | myFlag, &tmpSelection); item->itemText.deselectAll(); doc->appMode = oldAppMode; ////////////////////////////////////////////////////////////// i = pos; } return; } void NewsPaper::disableSpellCheck() { if(!checkDoc()) return; int N = ScCore->primaryMainWindow()->doc->Items->count(); double height = pageHeight(); double width = pageWidth(); double xmin = xOffset(); double ymin = yOffset(); double xmax = xmin + width; double ymax = ymin + height; QList itemList; for (int j = 0; j < N; j++) { PageItem *item = ScCore->primaryMainWindow()->doc->Items->at(j); if (item->xPos() < xmin) continue; if (item->yPos() < ymin) continue; if ((item->xPos() + item->width()) > xmax) continue; if ((item->yPos() + item->height()) > ymax) continue; itemList.append(item); } N = itemList.count(); for (int j = 0; j < N; j++) { PageItem *item = itemList.at(j); if (item->isGroup()) { foreach(PageItem *child, item->getAllChildren()) { if (child->isTextFrame()) { removeSpellCheckItem(child); } } } else if (item->isTextFrame()) { removeSpellCheckItem(item); } } return; } void NewsPaper::removeSpellCheckItem(PageItem *item) { if(!checkDoc()) return; if (item == nullptr) return; if (!item->isTextFrame()) return; int n = item->itemText.length(); if (n == 0) return; selectItem(item); item->itemText.setCursorPosition(0); item->update(); //ScribusDoc* doc = ScCore->primaryMainWindow()->doc; ScribusView *view = ScCore->primaryMainWindow()->view; view->requestMode(modeEdit); for(int i=0; i < n; ) { int pos = item->itemText.nextWord(i); QString word = item->itemText.text(i, pos-i).trimmed(); int length = word.length(); if (length == 0) { i = pos; break; } int mid = i + (length/2); const CharStyle& style = item->itemText.charStyle(mid); int flag = style.effects(); int myFlag = 16; if ((flag & myFlag) != myFlag) { i = pos; continue; } //----------------------------------------- ScribusDoc* doc = ScCore->primaryMainWindow()->doc; int oldAppMode = doc->appMode; //item->itemText.select(i, length); item->itemText.selectWord(i); item->itemText.setCursorPosition(i); Selection tmpSelection(nullptr, false); tmpSelection.addItem(item); if (item->HasSel) doc->appMode = modeEdit; item->itemText.selectWord(i); item->itemText.setCursorPosition(i); doc->itemSelection_SetEffects(flag - myFlag, &tmpSelection); item->itemText.deselectAll(); doc->appMode = oldAppMode; ////////////////////////////////////////////////////////////// i = pos; } return; } void NewsPaper::myDebug1() { if(!checkDoc()) return; qDebug() << "My Debug1"; ScribusMainWindow* win = ScCore->primaryMainWindow(); if (win->ClipB == nullptr) { qDebug() << "NO Clipboard"; return; } QString text = win->ClipB->text(); qDebug() << "++++++++++++++++++"; qDebug() << text; return; } QString NewsPaper::getUsername() { /* if (loginUsername == nullptr) return QString("NONAME"); QString username = loginUsername->text().trimmed(); if (username.length() == 0) return QString("NONAME"); return username; */ QString username = qgetenv("USER"); if (username.isEmpty()) username = qgetenv("USERNAME"); if (username.length() == 0) return QString("NONAME"); return username; } bool NewsPaper::writeUsername(QFile &file) { QString name = getUsername(); if (name == QString("NONAME")) return false; file.open(QIODevice::WriteOnly); file.write(name.toUtf8()); file.close(); return true; } QString NewsPaper::readUsername(QFile &file) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return QString(""); } QTextStream in(&file); QList dataList; while (!in.atEnd()) { QString line = in.readLine(); dataList.append(line); } return dataList.join(QString(" ")).trimmed(); } bool NewsPaper::checkUsername(QFile &file) { QString name = getUsername(); if (name == QString("NONAME")) return false; QString data = readUsername(file); if (data.count(name) == 0) return false; return true; }