Theory of the spectral line shape and gain in quantum wells with intersubband transitions