void Speller::populateContextMenu(QMenu* menu, const QWebHitTestResult &hitTest) { m_element = hitTest.element(); if (!m_enabled || m_element.isNull() || m_element.attribute(QLatin1String("type")) == QLatin1String("password")) { return; } const QString text = m_element.evaluateJavaScript("this.value").toString(); const int pos = m_element.evaluateJavaScript("this.selectionStart").toInt() + 1; QTextBoundaryFinder finder = QTextBoundaryFinder(QTextBoundaryFinder::Word, text); finder.setPosition(pos); m_startPos = finder.toPreviousBoundary(); m_endPos = finder.toNextBoundary(); const QString &word = text.mid(m_startPos, m_endPos - m_startPos).trimmed(); if (!isValidWord(word) || !isMisspelled(word)) { return; } const int limit = 6; QStringList suggests = suggest(word); int count = suggests.count() > limit ? limit : suggests.count(); QFont boldFont = menu->font(); boldFont.setBold(true); for (int i = 0; i < count; ++i) { QAction* act = menu->addAction(suggests.at(i), this, SLOT(replaceWord())); act->setData(suggests.at(i)); act->setFont(boldFont); } if (count == 0) { menu->addAction(tr("No suggestions"))->setEnabled(false); } menu->addAction(tr("Add to dictionary"), this, SLOT(addToDictionary()))->setData(word); menu->addSeparator(); }
int test_addToDictionary(){ MyString* W = newString(); appendString(W, 0); appendString(W, 1); appendString(W, 2); appendString(W, 3); int count = addToDictionary(dict, W); if(count != 257){ printf("FAIL:\taddToDictionary: dictionary word count should be 257 after adition, was %d.\n", count); return 1; } int i; for(i=0; i<4; i++){ if(dict->words[256]->S->data[i] != i){ printf("FAIL:\taddToDictionary: the string saved to dictionary is wrong. Should be \"0123\", was \"%d%d%d%d\"\n", dict->words[256]->S->data[0], dict->words[256]->S->data[1], dict->words[256]->S->data[2], dict->words[256]->S->data[3]); return 1; } } return 0; }
QMenu * SimpleRichTextEdit::createContextMenu(const QPoint &mouseGlobalPos) { Qt::TextInteractionFlags interactionFlags = this->textInteractionFlags(); QTextDocument *document = this->document(); QTextCursor cursor = textCursor(); const bool showTextSelectionActions = (Qt::TextEditable | Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse) & interactionFlags; QMenu *menu = new QMenu(this); if(interactionFlags & Qt::TextEditable) { m_actions[Undo]->setEnabled(document->isUndoAvailable()); menu->addAction(m_actions[Undo]); m_actions[Redo]->setEnabled(document->isRedoAvailable()); menu->addAction(m_actions[Redo]); menu->addSeparator(); m_actions[Cut]->setEnabled(cursor.hasSelection()); menu->addAction(m_actions[Cut]); } if(showTextSelectionActions) { m_actions[Copy]->setEnabled(cursor.hasSelection()); menu->addAction(m_actions[Copy]); } if(interactionFlags & Qt::TextEditable) { #if !defined(QT_NO_CLIPBOARD) m_actions[Paste]->setEnabled(canPaste()); menu->addAction(m_actions[Paste]); #endif m_actions[Delete]->setEnabled(cursor.hasSelection()); menu->addAction(m_actions[Delete]); m_actions[Clear]->setEnabled(!document->isEmpty()); menu->addAction(m_actions[Clear]); if(m_insertUnicodeControlCharMenu && interactionFlags & Qt::TextEditable) { menu->addSeparator(); menu->addMenu(m_insertUnicodeControlCharMenu); } } if(showTextSelectionActions) { menu->addSeparator(); m_actions[SelectAll]->setEnabled(!document->isEmpty()); menu->addAction(m_actions[SelectAll]); } if(interactionFlags & Qt::TextEditable) { menu->addSeparator(); m_actions[ToggleBold]->setCheckable(true); m_actions[ToggleBold]->setChecked(fontBold()); menu->addAction(m_actions[ToggleBold]); m_actions[ToggleItalic]->setCheckable(true); m_actions[ToggleItalic]->setChecked(fontItalic()); menu->addAction(m_actions[ToggleItalic]); m_actions[ToggleUnderline]->setCheckable(true); m_actions[ToggleUnderline]->setChecked(fontUnderline()); menu->addAction(m_actions[ToggleUnderline]); m_actions[ToggleStrikeOut]->setCheckable(true); m_actions[ToggleStrikeOut]->setChecked(fontStrikeOut()); menu->addAction(m_actions[ToggleStrikeOut]); menu->addAction(m_actions[ChangeTextColor]); menu->addSeparator(); m_actions[CheckSpelling]->setEnabled(!document->isEmpty()); menu->addAction(m_actions[CheckSpelling]); m_actions[ToggleAutoSpellChecking]->setChecked(checkSpellingEnabled()); menu->addAction(m_actions[ToggleAutoSpellChecking]); if(checkSpellingEnabled()) { setupWordUnderPositionCursor(mouseGlobalPos); QString selectedWord = m_selectedWordCursor.selectedText(); if(!selectedWord.isEmpty() && highlighter() && highlighter()->isWordMisspelled(selectedWord)) { QMenu *suggestionsMenu = menu->addMenu(i18n("Suggestions")); suggestionsMenu->addAction(i18n("Ignore"), this, SLOT(addToIgnoreList())); suggestionsMenu->addAction(i18n("Add to Dictionary"), this, SLOT(addToDictionary())); suggestionsMenu->addSeparator(); QStringList suggestions = highlighter()->suggestionsForWord(m_selectedWordCursor.selectedText()); if(suggestions.empty()) suggestionsMenu->addAction(i18n("No suggestions"))->setEnabled(false); else { for(QStringList::ConstIterator it = suggestions.begin(), end = suggestions.end(); it != end; ++it) suggestionsMenu->addAction(*it, this, SLOT(replaceWithSuggestion())); } } } menu->addSeparator(); m_actions[AllowTabulations]->setCheckable(true); m_actions[AllowTabulations]->setChecked(!tabChangesFocus()); menu->addAction(m_actions[AllowTabulations]); } return menu; }
int main(int argc, char* argv[]){ if ( argc != 3 ) // argc should be 3 for correct execution { printf( "usage: %s input_filename output_filename\n", argv[0] ); } //read text file into input array OriginalData* orig = readOriginalData(argv[1]); //allocate the same amount of compressed data (lets hope it is enough) CompressedData* compressed = newCompressedData(orig->dataLength, 8); //initializez the dictionary and the symbols in the dictionary Dictionary* dict = newDictionary(8); initDictionary(dict); printf("Compressing...\n"); writeToCompressedData(compressed, dict->clearCode); MyString* S = newString(); int codeword; int dictReturn; while(hasNextSymbol(orig)){ appendString(S, (uint8_t) nextSymbol(orig)); if(getCodeword(dict, S) != -1){ ; } else { //output S minus the last char S->length--; codeword = getCodeword(dict, S); writeToCompressedData(compressed, codeword); S->length++; //great for debugging : // printf("wrote a %d bit codeword: %d\n", compressed->bitWidth, codeword); // printf("Saved new word: %d \t", dict->wordCount); // printString(S); // printf("\n"); //add S to the dictionary dictReturn = addToDictionary(dict, S); if(dictReturn == -2){ //for debugging : // printf("--------------------------Increased Bit Width\n"); //increase the bit width by one compressed->bitWidth++; dict->bitWidth *= 2; } if(dictReturn == -1){ //dictionary is full, clear it and write a clearCode clearDictionary(dict); writeToCompressedData(compressed, dict->clearCode); compressed->bitWidth = compressed->rootBitWidth+1; //step one symbol back in original data orig->nextSymbol--; orig->dataLeft++; //make S empty S->length = 0; }else{ //"delete" the last character (it will still be in the data) S->length--; S->data[0] = S->data[S->length];//make S start with the "deleted" character S->length = 1; //make the length of S 1, now S is the earlier last character } } } //write the last codeword codeword = getCodeword(dict, S); writeToCompressedData(compressed, codeword); //write EOI writeToCompressedData(compressed, dict->endOfInformation); writeCompressedDataToFile(compressed, argv[2]); printf("File %s compressed succesfully to %s\n", argv[1], argv[2]); return 0; }