Im trying to highlight all the match words on the editor and can't seem to figure out how to properly highlight the text. I can successfully loop through all the found matches but cant seem to find the right call to highlight it. Here is my code:
bool found = true;
while(found)
{
editor->getCursorPosition(&line, &index);
qDebug() << "line: " << line << " index: " << index;
found = editor->findFirst(pattern, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);
if(found)
{
int start = editor->positionFromLineIndex(line, index);
int end = editor->positionFromLineIndex(line, index + pattern.length());
qDebug() << "line: " << line << " start: " << start << " end: " << end;
// Attempts to highlight
editor->SendScintilla(QsciScintilla::SCI_INDICGETSTYLE, QsciScintilla::INDIC_BOX);
editor->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);
//child[0]->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, start, end - start);
editor->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, start, end - start);
editor->setIndicatorForegroundColor(QColor(159, 144, 0));
// editor->setColor(QColor(159, 144, 0));**
}
}
my qDebug()'s is showing that its going through each line and finding the matches and the position of the occurance of the word. But code under the comment // Attempts to highlight is where I cant seem to figure out. Any advice?
You can try it.
SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE,0, INDIC_BOX);
QString docText = text();
int end = docText.lastIndexOf(findText);
int cur = -1;
if(end != -1) {
while(cur != end) {
cur = docText.indexOf(findText,cur+1);`
SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE,cur,
findText.length());
}
}
Related
I am trying to get the length unit conversion factor in OpenCASCADE, when importing a STEP format CAD file. In my test file the entity #184 sets the length to meters and during import will be converted to milimeters used by OpenCASCADE internally by default
...
#184=(
LENGTH_UNIT()
NAMED_UNIT(*)
SI_UNIT($,.METRE.)
);
...
I belive the function below is how it should be done, but no matter what i try the "Length_Unit" STEP entity is not matched, and therefore I can't get the scaling factor.
void step_unit_scaling(std::string const &file_name) {
STEPControl_Reader reader;
reader.ReadFile( file_name.c_str() );
const Handle(Interface_InterfaceModel) Model = reader.Model();
Handle(StepData_StepModel) aSM = Handle(StepData_StepModel)::DownCast(Model);
Standard_Integer NbEntities = Model->NbEntities();
for (int i=1; i<=NbEntities; i++) {
Handle(Standard_Transient) enti = aSM->Entity(i);
if (enti->IsKind (STANDARD_TYPE(StepBasic_LengthMeasureWithUnit))) {
Handle(StepBasic_LengthMeasureWithUnit) MWU = Handle(StepBasic_LengthMeasureWithUnit)::DownCast(enti);
Standard_Real scal_mm = MWU->ValueComponent();
std::cout << " --- !!! MATCH !!! --- scal_mm = " << scal_mm << std::endl;
}
}
}
Does anyone know if this is the correct approach, or if there perhaps is a better way.
If you search for an entity of a given type, you should check the types to find an error. The following line will show the actual entity type.
std::cout << "Entity type " << enti->DynamicType()->Name() << std::endl;
When I play with STEP files here, I see that your STEP line leads to an entity of type StepBasic_SiUnitAndLengthUnit. With this code I can test for some expected SI units:
if (enti->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)))
{
Handle(StepBasic_SiUnitAndLengthUnit) unit =
Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(enti);
if (unit->HasPrefix() &&
(StepBasic_SiUnitName::StepBasic_sunMetre == unit->Name()) &&
(StepBasic_SiPrefix::StepBasic_spMilli == unit->Prefix()))
{
std::cout << "SI Unit is millimetre." << std::endl;
}
else if (!unit->HasPrefix() &&
(StepBasic_SiUnitName::StepBasic_sunMetre == unit->Name()))
{
std::cout << "SI Unit is metre." << std::endl;
}
else
{
std::cout << "I did not understand that unit..." << std::endl;
}
}
I'm trying to replace the text of a specific line, but got no success. (i'd searched a lot, but i don't found nothing)
something like:
hello
my
friend!
replacing line 2 to some text:
hello
AEEEHO NEW LINE TEXT
friend!
I created a QStringList and tried to read the text line by line and add to this list by changing just the line, but without success.
int line = 1; // to change the second line
QString newline = "my new text";
QStringList temp;
int i = 0;
foreach(QString curlineSTR, internalCode.split('\n'))
{
if(line == i)
temp << newline;
else
temp << curlineSTR;
i++;
}
internalCode = "";
foreach(QString txt, temp)
internalCode.append(QString("%1\n").arg(txt));
I belive that you are looking for QRegExp to deal with newline and do something like this:
QString internalcode = "hello\nmy\nfriend!";
int line = 1; // to change the second line
QString newline = "another text";
// Split by newline command
QStringList temp = internalcode.split(QRegExp("\n|\r\n|\r"));
internalcode.clear();
for (int i = 0; i < temp.size(); i++)
{
if (line == i)
internalcode.append(QString("%0\n").arg(newline));
else
internalcode.append(QString("%0\n").arg(temp.at(i)));
}
//Use this to remove the last newline command
internalcode = internalcode.trimmed();
qDebug() << internalcode;
And the output:
"hello
another text
friend!"
In SQLite if I prepare a SELECT statement and begin stepping through it, then before the last row of the results is reached I execute another statement that has an effect on the SELECT statement that I am stepping through, what is the expected result?
I can't find anything in the SQLite documentation about what is supposed to happen but it seems like an extremely common case when programming in a multi-threaded environment.
Below is a c++ file that can be compiled and run on Windows to demonstrate the situation.
#include "stdafx.h"
#include "sqlite3.h"
#include <Windows.h>
#include <iostream>
#include <Knownfolders.h>
#include <Shlobj.h>
#include <wchar.h>
#include <comdef.h>
using namespace std;
int exec_sql(sqlite3 *db, const char* sql)
{
char *errmsg;
int result = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
if (result != SQLITE_OK) {
cout << errmsg << endl;
return -1;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Running jsqltst with SQLite version: ";
cout << sqlite3_libversion();
cout << endl;
PWSTR userhome;
if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Profile, NULL, NULL, &userhome))) {
cout << "Failed getting user home dir\n";
return -1;
}
wcout << "User home: " << userhome << endl;
wchar_t *ws1 = userhome, *ws2 = L"\\test.sqlite";
wstring dbpath_str(ws1);
dbpath_str += wstring(ws2);
_bstr_t dbpath(dbpath_str.c_str());
cout << "DB path: " << dbpath << endl;
sqlite3 *db;
int result = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);
if (result != SQLITE_OK) {
cout << sqlite3_errmsg(db) << endl;
return -1;
}
const char * create_stmt = "CREATE TABLE IF NOT EXISTS atable (id INTEGER PRIMARY KEY, name TEXT, number INTEGER);";
if (exec_sql(db, create_stmt) != 0) {
return -1;
}
const char * delete_stmt = "DELETE FROM atable;";
if (exec_sql(db, delete_stmt) != 0) {
return -1;
}
const char * insert_stmt = "INSERT INTO atable (name,number) VALUES ('Beta',77),('Alpha',99);";
if (exec_sql(db, insert_stmt) != 0) {
return -1;
}
sqlite3_stmt* select_ss;
const char * select_stmt = "SELECT * FROM atable;";
result = sqlite3_prepare_v2(db, select_stmt, -1, &select_ss, NULL);
if (result != SQLITE_OK) {
cout << sqlite3_errmsg(db) << endl;
return -1;
}
int i = 0;
boolean gotrow;
do {
result = sqlite3_step(select_ss);
gotrow = result == SQLITE_ROW;
if (gotrow) {
i++;
cout << "I got a row!" << endl;
if (i == 1) {
if (exec_sql(db, insert_stmt) != 0) {
return -1;
}
}
}
} while (gotrow);
cout << "Last result: " << result << ", errstr: " << sqlite3_errstr(result) << endl;
result = sqlite3_finalize(select_ss);
if (result != SQLITE_OK) {
cout << sqlite3_errmsg(db) << endl;
return -1;
}
return 0;
}
SQLite's behaviour for concurrent statements in the same transaction is neither documented nor defined.
As you have seen, newly inserted records might be seen when a SELECT's cursor has not yet reached that part of the table.
However, if SQLite needed to create a temporary result table for sorting or grouping, later changes in the table will not appear in that result.
Whether you have a temporary table or not might depend on decisions made by the query optimizer, so this is often not predictable.
If multiple threads access the same connection, SQLite will lock the DB around each sqlite3_step call.
This prevent data corruption, but you will still have the problem that automatic transaction end when their last active statement ends, and that explicit transaction will fail the COMMIT if there is some other active statement.
Multi-threaded programs are better off using (at least) one connection per thread.
I update segment tree with such function. Profiling says here's the bottleneck:
void update (int tree[], int root, int left, int right, int pos, double val)
{
if (left == right)
{
data[tree[root]] = val;
}
else
{
int middle = (left + right) / 2;
if (pos <= middle)
update(tree, root*2, left, middle, pos, val);
else
update(tree, root*2+1, middle+1, right, pos, val);
tree[root] = indexOfMax(tree, tree[root*2], tree[root*2+1]); // simple comparations
}
}
// indexOfMax is just a simple comparation
int indexOfMax(int tree[], int a, int b)
{
//cout << data[tree[a]] << " > " << data[tree[b]] << " ? " << tree[a] << " : " << tree[b] << endl;
return data[a] > data[b] ? a : b;
}
And while memory operations are fast, I'm wondering if it is caused by recursion overhead, while the depth of it is usually not more 20.
What I get from my primitive profiler is:
4.39434ms - average time for a singe binary search over data
2642.94ms - time from a single update
19.9097ms - time for a single RMQ-query.
So.. The time spent on a single update is dramatic :).
Answer : one hidden std::find over map was found.
I am trying to run code of copying files in other thread so that it may not freeze the GUI of the application.
I found that it does not seem to work in a separate thread.
Why is it not working ?
void CopyOperation::run()
{
CopyFilesToFolder(list,sFolder);
}
bool CopyOperation::CopyFilesToFolder(const QStringList &oFileList,const QString
&sTargetFolder)
{
if(sTargetFolder.isEmpty())
{
status = false;
return false;
}
QDir dir(sTargetFolder);
if(!dir.exists()) dir.mkdir(sTargetFolder);
QString sOldDirPath = dir.currentPath();
//if(!dir.setCurrent(sTargetFolder)) return false;
QFile file;
status = true;
foreach(QString sFileName,oFileList)
{
file.setFileName(sFileName);
QFileInfo info(sFileName);
QString newfile = sTargetFolder + "/" + info.fileName();
qDebug() << "\n name = " << newfile;
if(!QFile::copy(sFileName,newfile))
//if(!file.copy(newfile))
{
qDebug() << "\n File copy failed .. " + file.fileName() + " Error : " + file.errorString();
status = false;
break;
}
}
qDebug() << "\n Result .. " << file.errorString() << "code " << file.error();
//dir.setCurrent(sOldDirPath);
return status;
}
Since you didn't post code, I just can try to guess what is the problem. Qt has a static function:
bool copy ( const QString & fileName, const QString & newName )
There is also a copy which is not static:
bool copy ( const QString & newName )
Both of them will fail if file defined by newName already exists, ie. existing file will not be overwritten. Also, maybe path doesn't exists. Without some portion of code is difficult to guess what is the problem.