Images size limit for dsl dictionaries

This commit is contained in:
Abs62 2012-12-07 15:59:29 +04:00
parent 4ab213e178
commit faad6d2581
10 changed files with 117 additions and 20 deletions

View file

@ -268,6 +268,21 @@ sptr< Dictionary::DataRequest > ArticleMaker::makeEmptyPage() const
return r;
}
sptr< Dictionary::DataRequest > ArticleMaker::makePicturePage( string const & url ) const
{
string result = makeHtmlHeader( tr( "(picture)" ), QString() )
+ "<img src=\"" + url + "\" />"
+ "</body></html>";
sptr< Dictionary::DataRequestInstant > r =
new Dictionary::DataRequestInstant( true );
r->getData().resize( result.size() );
memcpy( &( r->getData().front() ), result.data(), result.size() );
return r;
}
void ArticleMaker::setExpandOptionalParts( bool expand )
{
needExpandOptionalParts = expand;

View file

@ -60,6 +60,9 @@ public:
/// Creates an 'untitled' page. The result is guaranteed to be instant.
sptr< Dictionary::DataRequest > makeEmptyPage() const;
/// Create page with one picture
sptr< Dictionary::DataRequest > makePicturePage( std::string const & url ) const;
/// Set auto expanding optional parts of articles
void setExpandOptionalParts( bool expand );

View file

@ -202,6 +202,14 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource(
}
}
if ( url.scheme() == "gdpicture" )
{
contentType = "text/html";
QUrl imgUrl ( url );
imgUrl.setScheme( "bres" );
return articleMaker.makePicturePage( imgUrl.toEncoded().data() );
}
return sptr< Dictionary::DataRequest >();
}

View file

@ -637,6 +637,11 @@ void ArticleView::linkHovered ( const QString & link, const QString & , const QS
msg = tr( "Audio" );
}
else
if ( url.scheme() == "gdpicture" )
{
msg = tr( "Picture" );
}
else
if (url.scheme() == "gdlookup" || url.scheme().compare( "bword" ) == 0)
{
QString def = url.path();
@ -696,6 +701,9 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref,
{
qDebug() << "clicked" << url;
if( url.scheme().compare( "gdpicture" ) == 0 )
ui.definition->load( url );
else
if ( url.scheme().compare( "bword" ) == 0 )
{
showDefinition( url.path(),

View file

@ -744,6 +744,9 @@ Class load() throw( exError )
if ( !root.namedItem( "editDictionaryCommandLine" ).isNull() )
c.editDictionaryCommandLine = root.namedItem( "editDictionaryCommandLine" ).toElement().text();
if ( !root.namedItem( "maxPictureWidth" ).isNull() )
c.maxPictureWidth = root.namedItem( "maxPictureWidth" ).toElement().text().toInt();
return c;
}
@ -1373,6 +1376,9 @@ void save( Class const & c ) throw( exError )
opt.appendChild( dd.createTextNode( c.editDictionaryCommandLine ) );
root.appendChild( opt );
opt = dd.createElement( "maxPictureWidth" );
opt.appendChild( dd.createTextNode( QString::number( c.maxPictureWidth ) ) );
root.appendChild( opt );
}
QByteArray result( dd.toByteArray() );

View file

@ -20,6 +20,10 @@ using std::vector;
/// Dictionaries which are temporarily disabled via the dictionary bar.
typedef QSet< QString > MutedDictionaries;
#ifdef Q_OS_WIN
#pragma pack(push,4)
#endif
/// A path where to search for the dictionaries
struct Path
{
@ -401,16 +405,23 @@ struct Class
unsigned short maxDictionaryRefsInContextMenu;
int maxPictureWidth; // Maximum picture width
QString editDictionaryCommandLine; // Command line to call external editor for dictionary
Class(): lastMainGroupId( 0 ), lastPopupGroupId( 0 ),
pinPopupWindow( false ), showingDictBarNames( false ),
usingSmallIconsInToolbars( false ), maxDictionaryRefsInContextMenu( 20 )
usingSmallIconsInToolbars( false ), maxDictionaryRefsInContextMenu( 20 ),
maxPictureWidth( 0 )
{}
Group * getGroup( unsigned id );
Group const * getGroup( unsigned id ) const;
};
#ifdef Q_OS_WIN
#pragma pack(pop)
#endif
/// Configuration-specific events. Some parts of the program need to react
/// to specific changes in configuration. The object of this class is used
/// to emit signals when such events happen -- and the listeners connect to

75
dsl.cc
View file

@ -153,11 +153,13 @@ class DslDictionary: public BtreeIndexing::BtreeDictionary
int optionalPartNom;
quint8 articleNom;
int maxPictureWidth;
public:
DslDictionary( string const & id, string const & indexFile,
vector< string > const & dictionaryFiles );
vector< string > const & dictionaryFiles,
int maxPictureWidth_ );
virtual void deferredInit();
@ -235,14 +237,16 @@ private:
DslDictionary::DslDictionary( string const & id,
string const & indexFile,
vector< string > const & dictionaryFiles ):
vector< string > const & dictionaryFiles,
int maxPictureWidth_ ):
BtreeDictionary( id, dictionaryFiles ),
idx( indexFile, "rb" ),
idxHeader( idx.read< IdxHeader >() ),
dz( 0 ),
deferredInitRunnableStarted( false ),
optionalPartNom( 0 ),
articleNom( 0 )
articleNom( 0 ),
maxPictureWidth( maxPictureWidth_ )
{
// Read the dictionary name
@ -745,22 +749,20 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
if ( node.tagName == GD_NATIVE_TO_WS( L"s" ) )
{
string filename = Utf8::encode( node.renderAsText() );
string n =
getDictionaryFilenames()[ 0 ] + ".files" +
FsEncoding::separator() +
FsEncoding::encode( filename );
if ( Filetype::isNameOfSound( filename ) )
{
// If we have the file here, do the exact reference to this dictionary.
// Otherwise, make a global 'search' one.
string n =
FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() +
FsEncoding::encode( filename );
bool search =
!File::exists( n ) && !File::exists( getDictionaryFilenames()[ 0 ] + ".files" +
FsEncoding::separator() +
FsEncoding::encode( filename ) ) &&
!File::exists( n ) && !File::exists( FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() +
FsEncoding::encode( filename ) ) &&
( !resourceZip.isOpen() ||
!resourceZip.hasFile( Utf8::decode( filename ) ) );
@ -784,8 +786,47 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
result += string( "<img src=\"" ) + url.toEncoded().data()
+ "\" alt=\"" + Html::escape( filename ) + "\"/>";
vector< char > imgdata;
bool resize = false;
try
{
File::loadFromFile( n, imgdata );
}
catch( File::exCantOpen & )
{
// Try reading from zip file
if ( resourceZip.isOpen() )
{
Mutex::Lock _( resourceZipMutex );
resourceZip.loadFile( Utf8::decode( filename ), imgdata );
}
}
catch(...)
{
}
if( !imgdata.empty() )
{
QImage img = QImage::fromData( (unsigned char *) &imgdata.front(),
imgdata.size() );
resize = maxPictureWidth > 0
&& img.width() > maxPictureWidth;
}
if( resize )
{
string link( url.toEncoded().data() );
link.replace( 0, 4, "gdpicture" );
result += string( "<a href=\"" ) + link + "\">"
+ "<img src=\"" + url.toEncoded().data()
+ "\" alt=\"" + Html::escape( filename ) + "\""
+ "width=" + QString::number( maxPictureWidth).toStdString() + "/>"
+ "</a>";
}
else
result += string( "<img src=\"" ) + url.toEncoded().data()
+ "\" alt=\"" + Html::escape( filename ) + "\"/>";
}
else
{
@ -1424,7 +1465,8 @@ static void findCorrespondingFiles( string const & ifo,
vector< sptr< Dictionary::Class > > makeDictionaries(
vector< string > const & fileNames,
string const & indicesDir,
Dictionary::Initializing & initializing )
Dictionary::Initializing & initializing,
int maxPictureWidth )
throw( std::exception )
{
vector< sptr< Dictionary::Class > > dictionaries;
@ -1861,7 +1903,8 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
dictionaries.push_back( new DslDictionary( dictId,
indexFile,
dictFiles ) );
dictFiles,
maxPictureWidth ) );
}
catch( std::exception & e )
{

3
dsl.hh
View file

@ -15,7 +15,8 @@ using std::string;
vector< sptr< Dictionary::Class > > makeDictionaries(
vector< string > const & fileNames,
string const & indicesDir,
Dictionary::Initializing & )
Dictionary::Initializing &,
int maxPictureWidth )
throw( std::exception );
}

View file

@ -38,7 +38,8 @@ using std::vector;
LoadDictionaries::LoadDictionaries( Config::Class const & cfg ):
paths( cfg.paths ), soundDirs( cfg.soundDirs ), hunspell( cfg.hunspell ),
transliteration( cfg.transliteration ),
exceptionText( "Load did not finish" ) // Will be cleared upon success
exceptionText( "Load did not finish" ), // Will be cleared upon success
maxPictureWidth( cfg.maxPictureWidth )
{
// Populate name filters
@ -130,7 +131,7 @@ void LoadDictionaries::handlePath( Config::Path const & path )
{
vector< sptr< Dictionary::Class > > dslDictionaries =
Dsl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
Dsl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this, maxPictureWidth );
dictionaries.insert( dictionaries.end(), dslDictionaries.begin(),
dslDictionaries.end() );

View file

@ -23,6 +23,7 @@ class LoadDictionaries: public QThread, public Dictionary::Initializing
Config::Transliteration const & transliteration;
std::vector< sptr< Dictionary::Class > > dictionaries;
std::string exceptionText;
int maxPictureWidth;
public: