bool LiteEditorFile::reloadByCodec(const QString &codecName) { setTextCodec(codecName); return open(m_fileName,m_mimeType,false); }
pFileDialog::pFileDialog( QWidget* parent, const QString& caption, const QString& directory, const QString& filter, bool textCodecEnabled, bool openReadOnlyEnabled ) : QFileDialog( parent, caption, directory, filter ) { setFileMode( QFileDialog::AnyFile ); setOption( QFileDialog::DontUseNativeDialog ); // get grid layout glDialog = qobject_cast<QGridLayout*>( layout() ); // assert on gridlayout Q_ASSERT( glDialog ); // relook the dialog to be more friendly QLabel* lLookIn = findChild<QLabel*>( "lookInLabel" ); QComboBox* cbLookIn = findChild<QComboBox*>( "lookInCombo" ); QToolButton* tbNewFolder = findChild<QToolButton*>( "newFolderButton" ); QAbstractItemView* sidebar = findChild<QAbstractItemView*>( "sidebar" ); QFrame* fFrame = findChild<QFrame*>( "frame" ); QBoxLayout* hLayout = 0; // search layout containing tbNewFolder foreach ( QLayout* layout, findChildren<QLayout*>() ) { if ( layout->indexOf( tbNewFolder ) != -1 ) { hLayout = qobject_cast<QBoxLayout*>( layout ); break; } } if ( lLookIn ) { lLookIn->setVisible( false ); } if ( hLayout ) { hLayout->setSpacing( 3 ); hLayout->insertStretch( hLayout->indexOf( tbNewFolder ) ); } if ( cbLookIn && fFrame ) { QBoxLayout* vLayout = qobject_cast<QBoxLayout*>( fFrame->layout() ); if ( vLayout ) { vLayout->setSpacing( 3 ); vLayout->insertWidget( 0, cbLookIn ); if ( hLayout ) { glDialog->removeItem( hLayout ); hLayout->setParent( 0 ); vLayout->insertLayout( 0, hLayout ); } } } if ( sidebar ) { QWidget* viewport = sidebar->viewport(); QPalette pal = viewport->palette(); pal.setColor( viewport->backgroundRole(), QColor( Qt::transparent ) ); viewport->setPalette( pal ); sidebar->setFrameStyle( QFrame::NoFrame | QFrame::Plain ); sidebar->setIconSize( QSize( 16, 16 ) ); } // text codec mTextCodecEnabled = true; lCodec = new QLabel( tr( "Codec:" ), this ); cbCodec = new QComboBox( this ); cbCodec->addItems( pCoreUtils::textCodecs() ); setTextCodec( QTextCodec::codecForLocale()->name() ); glDialog->addWidget( lCodec, 4, 0 ); glDialog->addWidget( cbCodec, 4, 1 ); // read only mOpenReadOnlyEnabled = true; cbOpenReadOnly = new QCheckBox( tr( "Open in read only." ), this ); glDialog->addWidget( cbOpenReadOnly, 5, 1 ); // configuration setTextCodecEnabled( textCodecEnabled ); setOpenReadOnlyEnabled( openReadOnlyEnabled ); }
bool LiteEditorFile::reloadTextByCodec(const QString &codecName, QString &outText) { setTextCodec(codecName); return loadFileHelper(m_fileName,m_mimeType,false,outText); }
bool TextBuffer::load (const QString &filename, bool &encodingErrors, bool &tooLongLinesWrapped) { // fallback codec must exist Q_ASSERT (m_fallbackTextCodec); // codec must be set! Q_ASSERT (m_textCodec); /** * first: clear buffer in any case! */ clear (); /** * check if this is a normal file or not, else exit */ KDE_struct_stat sbuf; if (KDE::stat(filename, &sbuf) != 0 || !S_ISREG(sbuf.st_mode)) return false; /** * construct the file loader for the given file, with correct prober type */ Kate::TextLoader file (filename, m_encodingProberType); /** * triple play, maximal three loading rounds * 0) use the given encoding, be done, if no encoding errors happen * 1) use BOM to decided if unicode or if that fails, use encoding prober, if no encoding errors happen, be done * 2) use fallback encoding, be done, if no encoding errors happen * 3) use again given encoding, be done in any case */ for (int i = 0; i < 4; ++i) { /** * kill all blocks beside first one */ for (int b = 1; b < m_blocks.size(); ++b) { TextBlock* block = m_blocks.at(b); block->m_lines.clear (); delete block; } m_blocks.resize (1); /** * remove lines in first block */ m_blocks.last()->m_lines.clear (); m_lines = 0; /** * try to open file, with given encoding * in round 0 + 3 use the given encoding from user * in round 1 use 0, to trigger detection * in round 2 use fallback */ QTextCodec *codec = m_textCodec; if (i == 1) codec = 0; else if (i == 2) codec = m_fallbackTextCodec; if (!file.open (codec)) { // create one dummy textline, in any case m_blocks.last()->appendLine (TextLine (new TextLineData())); m_lines++; return false; } // read in all lines... encodingErrors = false; while ( !file.eof() ) { // read line int offset = 0, length = 0; bool currentError = !file.readLine (offset, length); encodingErrors = encodingErrors || currentError; // bail out on encoding error, if not last round! if (encodingErrors && i < 3) { kDebug (13020) << "Failed try to load file" << filename << "with codec" << (file.textCodec() ? file.textCodec()->name() : "(null)"); break; } // get unicode data for this line const QChar *unicodeData = file.unicode () + offset; /** * split lines, if too large */ do { /** * calculate line length */ int lineLength = length; if ((m_lineLengthLimit > 0) && (lineLength > m_lineLengthLimit)) { /** * search for place to wrap */ int spacePosition = m_lineLengthLimit-1; for (int testPosition = m_lineLengthLimit-1; (testPosition >= 0) && (testPosition >= (m_lineLengthLimit - (m_lineLengthLimit/10))); --testPosition) { /** * wrap place found? */ if (unicodeData[testPosition].isSpace() || unicodeData[testPosition].isPunct()) { spacePosition = testPosition; break; } } /** * wrap the line */ lineLength = spacePosition+1; length -= lineLength; tooLongLinesWrapped = true; } else { /** * be done after this round */ length = 0; } /** * construct new text line with content from file * move data pointer */ TextLine textLine = TextLine (new TextLineData(QString (unicodeData, lineLength))); unicodeData += lineLength; /** * ensure blocks aren't too large */ if (m_blocks.last()->lines() >= m_blockSize) m_blocks.append (new TextBlock (this, m_blocks.last()->startLine() + m_blocks.last()->lines())); /** * append line to last block */ m_blocks.last()->appendLine (textLine); ++m_lines; } while (length > 0); } // if no encoding error, break out of reading loop if (!encodingErrors) { // remember used codec, might change bom setting setTextCodec (file.textCodec ()); break; } } // remember if BOM was found if (file.byteOrderMarkFound ()) setGenerateByteOrderMark (true); // remember eol mode, if any found in file if (file.eol() != eolUnknown) setEndOfLineMode (file.eol()); // remember mime type for filter device m_mimeTypeForFilterDev = file.mimeTypeForFilterDev (); // assert that one line is there! Q_ASSERT (m_lines > 0); // report CODEC + ERRORS kDebug (13020) << "Loaded file " << filename << "with codec" << m_textCodec->name() << (encodingErrors ? "with" : "without") << "encoding errors"; // report BOM kDebug (13020) << (file.byteOrderMarkFound () ? "Found" : "Didn't find") << "byte order mark"; // report filter device mime-type kDebug (13020) << "used filter device for mime-type" << m_mimeTypeForFilterDev; // emit success emit loaded (filename, encodingErrors); // file loading worked, modulo encoding problems return true; }