void SourceViewer::doHighlightText(const QString &text, bool caseSensitive) { qDebug() << "doHighlightText : text=" << text; workingHighlightText_ = true; do { QTextEdit *logTextEdit = getCurrentSourceEdit(); if(logTextEdit == NULL) break; int orgValueVert = logTextEdit->verticalScrollBar()->value(); int orgValueHor = logTextEdit->horizontalScrollBar()->value(); QTextCursor orgCursor = logTextEdit->textCursor(); QTextDocument::FindFlags flags = 0x0; if(caseSensitive) flags |= QTextDocument::FindCaseSensitively; QList<QTextEdit::ExtraSelection> extraSelections; logTextEdit->moveCursor(QTextCursor::Start); QColor fgColor = QColor("white"); QColor bgColor = QColor("#C32438"); while(logTextEdit->find(text, flags)) { QTextEdit::ExtraSelection extra; extra.format.setForeground(fgColor); extra.format.setBackground(bgColor); extra.cursor = logTextEdit->textCursor(); extraSelections.append(extra); } logTextEdit->setExtraSelections(extraSelections); logTextEdit->setTextCursor(orgCursor); logTextEdit->verticalScrollBar()->setValue(orgValueVert); logTextEdit->horizontalScrollBar()->setValue(orgValueHor); } while(false); workingHighlightText_ = false; }
void QAccessibleTextEdit::scrollToSubstring(int startIndex, int endIndex) { QTextEdit *edit = textEdit(); QTextCursor cursor = textCursor(); cursor.setPosition(startIndex); QRect r = edit->cursorRect(cursor); cursor.setPosition(endIndex); r.setBottomRight(edit->cursorRect(cursor).bottomRight()); r.moveTo(r.x() + edit->horizontalScrollBar()->value(), r.y() + edit->verticalScrollBar()->value()); // E V I L, but ensureVisible is not public if (!QMetaObject::invokeMethod(edit, "_q_ensureVisible", Q_ARG(QRectF, r))) qWarning("AccessibleTextEdit::scrollToSubstring failed!"); }
void VBoxVMLogViewer::showEvent (QShowEvent *aEvent) { QIMainDialog::showEvent (aEvent); /* One may think that QWidget::polish() is the right place to do things * below, but apparently, by the time when QWidget::polish() is called, * the widget style & layout are not fully done, at least the minimum * size hint is not properly calculated. Since this is sometimes necessary, * we provide our own "polish" implementation. */ if (mIsPolished) return; mIsPolished = true; if (mFirstRun) { /* Resize the whole log-viewer to fit 80 symbols in * text-browser for the first time started */ QTextEdit *firstPage = currentLogPage(); if (firstPage) { int fullWidth = firstPage->fontMetrics().width (QChar ('x')) * 80 + firstPage->verticalScrollBar()->width() + firstPage->frameWidth() * 2 + /* mLogList margin */ 10 * 2 + /* CentralWidget margin */ 10 * 2; resize (fullWidth, height()); mFirstRun = false; } } /* Make sure the log view widget has the focus */ QWidget *w = currentLogPage(); if (w) w->setFocus(); }
void PeersDialog::insertChat() { if (!rsMsgs->chatAvailable()) { // std::cerr << "no chat available." << std::endl ; return; } std::list<ChatInfo> newchat; if (!rsMsgs->getNewChat(newchat)) { std::cerr << "could not get new chat." << std::endl ; return; } std::cerr << "got new chat." << std::endl ; QTextEdit *msgWidget = ui.msgText; std::list<ChatInfo>::iterator it; /** A RshareSettings object used for saving/loading settings */ RshareSettings settings; uint chatflags = settings.getChatFlags(); /* add in lines at the bottom */ for(it = newchat.begin(); it != newchat.end(); it++) { std::string msg(it->msg.begin(), it->msg.end()); #ifdef PEERS_DEBUG std::cerr << "PeersDialog::insertChat(): " << msg << std::endl; #endif /* are they private? */ if (it->chatflags & RS_CHAT_PRIVATE) { PopupChatDialog *pcd = getPrivateChat(it->rsid, it->name, chatflags); pcd->addChatMsg(&(*it)); continue; } std::ostringstream out; QString currenttxt = msgWidget->toHtml(); QString extraTxt; QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss"); QString name = QString::fromStdString(it->name); QString line = "<span style=\"color:#C00000\">" + timestamp + "</span>" + "<span style=\"color:#2D84C9\"><strong>" + " " + name + "</strong></span>"; //std::cerr << "PeersDialog::insertChat(): 1.11\n"; historyKeeper.addMessage(name, "THIS", QString::fromStdWString(it->msg)); //std::cerr << "PeersDialog::insertChat(): 1.12\n"; extraTxt += line; extraTxt += QString::fromStdWString(it->msg); /* add it everytime */ currenttxt += extraTxt; QHashIterator<QString, QString> i(smileys); while(i.hasNext()) { i.next(); foreach(QString code, i.key().split("|")) currenttxt.replace(code, "<img src=\"" + i.value() + "\" />"); } msgWidget->setHtml(currenttxt); QScrollBar *qsb = msgWidget->verticalScrollBar(); qsb -> setValue(qsb->maximum()); } }
int main(int argc, char **argv) { Vals vals; /* the application */ QApplication app(argc, argv); app.setApplicationName(APP_NAME); app.setWindowIcon(QIcon(":/icon")); Window window; /* translations */ QTranslator qtr; if (qtr.load("qt_" + QLocale::system().name(), QTR_PATH)) app.installTranslator(&qtr); QTranslator htr; if (htr.load("H4KvT_" + QLocale::system().name(), ":/")) app.installTranslator(&htr); /* display information */ QTextEdit *text = new QTextEdit; text->setReadOnly(true); text->setLineWrapMode(QTextEdit::NoWrap); text->setToolTip(Window::tr("Drop any file for hashing")); QObject::connect(&window, &Window::idle, text, &QWidget::setEnabled); /* compare hash */ QLineEdit *test = new QLineEdit; HexVal hexval; test->setValidator(&hexval); test->installEventFilter(&window); test->setToolTip(Window::tr("Compare hashes")); QObject::connect(test, &QLineEdit::textChanged, [&](const QString &newValue) { if (vals.name != "") { std::string hashVal = newValue.toStdString(); std::transform(hashVal.begin(), hashVal.end(), hashVal.begin(), ::tolower); std::stringstream html; if (hashVal != "") html << "<style>.h" << hashVal << "{color:green}</style>"; html << "<div style='margin-bottom:2; font-size:27px'><i><b>" << vals.name << "</b></i></div>"; html << "<div style='margin-bottom:7; margin-left:23; font-size:13px'>" << vals.path << "</div>"; if (!ALL(vals,"")) { html << "<div style='font-size:13px'><table>"; if (vals.md5 != "") html << "<tr><td>md5: </td><td class='h" << vals.md5 << "'>" << vals.md5 << "</td</tr>"; if (vals.sha1 != "") html << "<tr><td>sha1: </td><td class='h" << vals.sha1 << "'>" << vals.sha1 << "</td</tr>"; if (vals.sha224 != "") html << "<tr><td>sha224: </td><td class='h" << vals.sha224 << "'>" << vals.sha224 << "</td</tr>"; if (vals.sha256 != "") html << "<tr><td>sha256: </td><td class='h" << vals.sha256 << "'>" << vals.sha256 << "</td</tr>"; if (vals.sha384 != "") html << "<tr><td>sha384: </td><td class='h" << vals.sha384 << "'>" << vals.sha384 << "</td</tr>"; if (vals.sha512 != "") html << "<tr><td>sha512: </td><td class='h" << vals.sha512 << "'>" << vals.sha512 << "</td</tr>"; html << "</table></div>"; } int horizontal = text->horizontalScrollBar()->value(); int vertical = text->verticalScrollBar()->value(); text->setHtml(QString::fromStdString(html.str())); text->horizontalScrollBar()->setValue(horizontal); text->verticalScrollBar()->setValue(vertical); test->setStyleSheet(ANY(vals,hashVal) ? "color:green" : ""); }}); /* error messages */ QLabel *error = new QLabel; error->setStyleSheet("color:red"); /* test or error */ QStackedWidget *stack = new QStackedWidget; delete stack->layout(); stack->setLayout(new Stack); stack->addWidget(error); stack->addWidget(test); stack->setCurrentIndex(1); /* toggle test or error */ QPushButton *hash = new QPushButton(QIcon(":/icon"), ""); hash->setCheckable(true); hash->setChecked(true); hash->setToolTip(Window::tr("Compare hashes")); QObject::connect(hash, &QPushButton::toggled, stack, &QStackedWidget::setCurrentIndex); /* store method */ QSettings settings("H4KvT", "H4KvT"); /* more methods */ bool more; try { settings.setValue("MoreHashingMethods", more = moreOrLess()); } catch (...) { more = settings.value("MoreHashingMethods", false).toBool(); } /* hashing method */ QComboBox *meth = new QComboBox; meth->addItem("md5"); meth->addItem("sha1"); if (more) meth->addItem("sha224"); meth->addItem("sha256"); if (more) meth->addItem("sha384"); meth->addItem("sha512"); meth->setToolTip(Window::tr("Hashing method")); meth->setCurrentText(settings.value("HashingMethod", "md5").toString()); QObject::connect(&window, &Window::idle, meth, &QWidget::setEnabled); QObject::connect(meth, &QComboBox::currentTextChanged, [&](const QString &text) { settings.setValue("HashingMethod", text); }); /* toolbar */ QHBoxLayout *pane = new QHBoxLayout; pane->addWidget(hash, 0, Qt::AlignLeft); pane->addWidget(stack); pane->addWidget(meth, 0, Qt::AlignRight); /* main layout */ QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(text); layout->addLayout(pane); /* the window */ window.centralWidget()->setLayout(layout); window.show(); /* future hashing */ QFutureWatcher<Vals> zu; QObject::connect(&zu, &QFutureWatcher<Vals>::finished, [&]() { Vals valsi = zu.future().result(); window.idle(true); if (valsi.path == "") { error->setText(QString::fromStdString(valsi.name)); hash->setChecked(false); } else { error->clear(); vals += valsi; test->textChanged(test->text()); } }); QObject::connect(meth, &QComboBox::currentTextChanged, [&](const QString &text) { if (vals.name != "") { window.idle(false); zu.setFuture(QtConcurrent::run(&update, text, vals)); }}); QObject::connect(&window, &Window::fileDroped, [&](const QString &name, const QString &path) { window.idle(false); zu.setFuture(QtConcurrent::run(&update, meth->currentText(), Vals(name, path))); }); /* hashing info */ QGraphicsBlurEffect blur; blur.setBlurHints(QGraphicsBlurEffect::AnimationHint); QPropertyAnimation anim(&blur, "blurRadius"); anim.setDuration(2000); anim.setLoopCount(-1); anim.setKeyValueAt(0, 0); anim.setKeyValueAt(0.5, 3); anim.setKeyValueAt(1, 0); QLabel *hashing = new QLabel; hashing->setPixmap(QPixmap(":/icon")); hashing->setAttribute(Qt::WA_TransparentForMouseEvents); hashing->setGraphicsEffect(&blur); hashing->hide(); (new QHBoxLayout(text))->addWidget(hashing, 0, Qt::AlignCenter); QObject::connect(&blur, &QGraphicsBlurEffect::blurRadiusChanged, hashing, static_cast<void(QWidget::*)()>(&QWidget::update)); QObject::connect(&window, &Window::idle, [&](bool idle) { if (idle) { hashing->hide(); anim.stop(); } else { hashing->show(); anim.start(); } }); /* about app */ QMenu about; QAction *action = about.addAction(QIcon(":/icon"), Window::tr("About %1").arg(APP_NAME)); action->setMenuRole(QAction::AboutRole); QObject::connect(action, &QAction::triggered, &window, &Window::about); error->setContextMenuPolicy(Qt::NoContextMenu); hash->setContextMenuPolicy(Qt::NoContextMenu); meth->setContextMenuPolicy(Qt::NoContextMenu); window.setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(&window, &QWidget::customContextMenuRequested, [&about, &window](const QPoint &pos) { about.popup(window.mapToGlobal(pos)); }); text->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(text, &QWidget::customContextMenuRequested, [action, text](const QPoint &pos) { if (QMenu *m = text->createStandardContextMenu()) { m->insertAction(m->insertSeparator(m->actions()[0]), action); m->popup(text->mapToGlobal(pos)); } }); test->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(test, &QWidget::customContextMenuRequested, [action, test](const QPoint &pos) { if (QMenu *m = test->createStandardContextMenu()) { m->insertAction(m->insertSeparator(m->actions()[0]), action); m->popup(test->mapToGlobal(pos)); } }); return app.exec(); }